无法解析符号 AndroidJUnit4

Cannot resolve symbol AndroidJUnit4

我正在尝试为我的应用程序添加 loginfacebook。但是当我添加一个需要这样做的存储库时。它导致了一个错误。 AndroidJUnit4 现在无法解析。

ExampleInstrumentedTest.java

package com.example.user.enyatravelbataan;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.*;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("com.example.user.enyatravelbataan", 
appContext.getPackageName());
}
}

这是我的 build:gradle(app)

apply plugin: 'com.android.application'

 android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
useLibrary 'org.apache.http.legacy'


repositories {
    mavenCentral()
}

defaultConfig {
    applicationId "com.example.user.enyatravelbataan"
    minSdkVersion 16
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile(name: 'wikitudesdk', ext: 'aar')
// compile 'org.apache.httpcomponents:httpclient:4.5'
// compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile files('libs/MD5Simply.jar')
compile files('libs/GenAsync.1.2.jar')
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:multidex:1.0.0'
compile 'com.google.android.gms:play-services:9.8.0'
compile 'com.google.android.gms:play-services-location:9.8.0'
compile 'com.google.android.gms:play-services-appindexing:9.8.0'
compile 'com.android.support:cardview-v7:24.2.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:design:24.2.1'
compile 'com.android.volley:volley:1.0.0'
compile 'com.android.support:support-v4:24.2.1'

testCompile 'junit:junit:4.12'
}

repositories {
flatDir {
    dirs 'libs'
}
}
apply plugin: 'com.google.gms.google-services'

尝试

androidTestImplementation 'com.android.support:support-annotations:23.1.0'
androidTestImplementation 'com.android.support.test:runner:0.4.1'
androidTestImplementation 'com.android.support.test:rules:0.4.1'

在上述依赖项部分添加以下内容

configurations.all {
   resolutionStrategy.force 'com.android.support:support-annotations:23.1.0'
}

除上述回答外,请确保您严格遵循以下步骤:

我正在用头撞墙 and/or 我在我面前看到的任何物体都是为了让我的 SQLite 功能单元得到测试。不管我怎么做,只要我网上很多聪明人的建议都没有用。

然后我不得不去 学前班并重新开始意识到我的愚蠢错误。我了解到AndroidJUnit4只能与Instrumentation Test一起使用,而JUnit必须用于本地测试。也就是说,文件夹必须是 src/androidTest/java。我的测试 class 直接在 androidTest 文件夹下,因此我不得不面对那个讨厌的错误。然而,当我将它移到 src/androidTest/java 下时,一切都变得非常清晰,就像“我现在可以清楚地看到雨已经消失了”。

Take a look at this article 这表示...

Run Instrumented Unit Tests To run your instrumented tests, follow these steps:

Be sure your project is synchronized with Gradle by clicking Sync Project in the toolbar. Run your test in one of the following ways: To run a single test, open the Project window, and then right-click a test and click Run . To test all methods in a class, right-click a class or method in the test file and click Run . To run all tests in a directory, right-click on the directory and select Run tests . The Android Plugin for Gradle compiles the instrumented test code located in the default directory (src/androidTest/java/), builds a test APK and production APK, installs both APKs on the connected device or emulator, and runs the tests. Android Studio then displays the results of the instrumented test execution in the Run window.

因此,对于仪器测试,文件夹必须是(不要忘记大小写)

src/androidTest/java

对于本地测试,文件夹必须是

src/test/java

然后您可以让您的包文件夹与您的应用程序包相匹配

希望这对社区有所帮助!

另一件重要的事情(对我来说这个问题已经浮出水面)是检查您是否更改了测试的 构建类型 - 那就是 testBuildType 选项在你模块的 build.gradle.
如果您确实更改了它(就像我所做的那样),那么在编辑任何 Android 测试之前:

  1. 转到您的 Android 工作室 build/flavors 选项卡
  2. Select 与您在 testBuildType Gradle 属性
  3. 中指定的构建类型相同
  4. 同步 Android Studio 与 Gradle

要再次正常 运行 应用程序,我想您显然需要切换回 'debug'。

注意:尽管这解决了我的核心问题,如此处问题所述,但我仍在寻找一种方法来支持 Android 测试的两种自定义构建类型,而且允许调试构建类型;我的最终目标是让 CI 运行 使用特殊构建类型进行测试,但让开发人员 运行 在 'debug' 模式下进行测试。因此,如果有人对如何实现这一目标有任何想法,请在评论部分发表评论。 :)

只需将 compile "com.android.support.test:runner:1.0.1" 添加到您的 Gradle。

对我来说,这是因为某些 androidTestImplementation 导入在 build.gradle 中不是最新的。在我将它们 all 更新到最新版本后,错误消失了。

注:

我的项目有多个模块,我必须在每个 模块中更新它们。

请尝试最新的API喜欢以下

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-ore:3.0.2'

仍然,运气不好,然后尝试 "implementation" 而不是 testImplementation、androidTestImplementation 和 androidTestImplementation 并在 Gradle 中同步。

对我有用的解决方案是更改模拟器(使用您正在使用的 api 的模拟器) 为此,请单击 运行 按钮旁边的模拟器符号(例如:Pixel 2) 然后转到打开 AVD 管理器(添加 api 你需要的(例如 API 29)) 就这些

如果迁移到 AndroidX 后出现此问题。这样做:-

1。正确导入

import androidx.test.runner.AndroidJUnit4

而不是

import android.support.test.runner.AndroidJUnit4

2。正确方法

Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

而不是

Context appContext = InstrumentationRegistry.getTargetContext()