为什么我不能将 AndroidJUnit4 和 ActivityTestRule 导入我的单元测试 class?

Why can't I import AndroidJUnit4 and ActivityTestRule into my unit test class?

我在导入一些 Android UI 测试框架时遇到问题 classes - 我就是不知道出了什么问题!

这是我的 class:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ExampleUnitTest {

@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);

@Test
public void listGoesOverTheFold() {
    onView(withText("Hello world!")).check(matches(isDisplayed()));
  }
}

但由于某种原因,我收到错误 'cannot find symbol ActivityTestRule' 和 'cannot find symbol AndroidJUnit4'。我尝试导入它们,但找不到它们。

build.gradle中的依赖设置为:

compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
androidTestCompile 'com.android.support:support-annotations:23.4.0'

androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'

所以我想我已经设置了所有依赖项 - 我已经尝试了很多东西但没有运气。

有人有什么想法吗?

您可以在 Android

中设置两种不同类型的测试

单元测试

  • 这些 运行 直接在 JVM 上,无法访问 Android 框架 classes.
  • 它们保存在 test/java 包中
  • 需要在build.gradle文件中使用命令testCompile
  • 添加依赖项
  • 您通常使用 Mockito、Robolectric 和 JUnit 进行这些测试

仪器测试

  • 这些 运行 在 Android 模拟器上并且可以完全访问所有 Android classes
  • 它们保存在 androidTest/java 包中
  • 需要将依赖项添加到 build.gradle 和 androidTestCompile
  • 您通常使用 Espresso 和 JUnit 进行这些测试

据我所知,您正在尝试使用 Espresso 编写仪器测试,但您的测试位于用于单元测试的 test/java 包中。在这种情况下,您需要将测试 class 移至 androidTest/java 包。

需要这个添加依赖项

 testCompile 'com.android.support.test:rules:0.5'
 testCompile 'com.android.support.test:runner:0.5'

添加依赖。

androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test:runner:0.5'

在较新的版本中添加这些:

androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'

添加:

androidTestImplementation 'com.android.support.test:rules:1.0.2'

解决了问题,但不要忘记将项目与 gradle 文件同步。只有这样更改才会生效。

如果您迁移到 AndroidX,请使用:

androidTestImplementation 'androidx.test:rules:1.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'

用于编写 UI 测试((在 Android Device/Emulator 上测试 运行))在 Android, 确保

  1. 测试 类 在 androidTest 包中而不是 测试包.

  2. 确保在 build.gradle

    中添加以下依赖项
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
testImplementation 'junit:junit:4.13'

对于单元测试(在 JVM 上测试 运行)确保

1.Test 类 在 test package

2.Make 确保在 build.gradle

中进行以下依赖
testImplementation 'junit:junit:4.13'
testImplementation 'org.mockito:mockito-core:2.23.0'

从 androidX 使用

androidTestImplementation 'androidx.test:rules:1.1.1'

androidTestImplementation 'androidx.test:runner:1.1.1'

在应用级别 build.gradle 文件的依赖项部分

例如:

 dependencies {

     androidTestImplementation 'androidx.test:rules:1.1.1'
     androidTestImplementation 'androidx.test:runner:1.1.1'
 }

然后导入

导入androidx.test.rule.ActivityTestRule;

使用 ActivityScenarioRule 对我有用。

    @Rule
public ActivityScenarioRule<MainActivity> mActivityRule = new ActivityScenarioRule(MainActivity.class);