为什么我不能在单元测试类中导入AndroidJUnit4和ActivityTestRule?

87

我遇到了一些问题,无法导入一些 Android UI 测试框架的类 - 我无法弄清楚出了什么问题!

这是我的类:

@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()));
  }
}

但出现了“找不到符号ActivityTestRule”和“找不到符号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'

所以我认为我已经设置好了所有的依赖项 - 我尝试了很多方法,但都没有成功。

有人有什么想法吗?


1
你的测试类在哪个目录下 - test/java 还是 androidTest/java - Jahnold
1
它在test/java目录下。 - Hallupa
4
如果我的测试位于androidTest/java目录下,而我遇到了这个问题,该怎么办? - alayor
10个回答

140

在新版本中添加以下内容:

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

67
如果您迁移到AndroidX,请使用以下内容:
androidTestImplementation 'androidx.test:rules:1.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'

67

在Android中,可以设置两种不同类型的测试:

单元测试

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

仪器化测试

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

从我所了解的情况来看,您正在尝试使用Espresso编写仪器化测试,但将测试放在test/java包中,该包是用于单元测试的。在这种情况下,您需要将测试类移到androidTest/java包中。


3
谢谢,问题已经解决了!我之前没有意识到test/java和androidTest/java之间有区别。 - Hallupa
确保您还测试实现了'com.android.support.test:rules:1.0.2' - GeekWithGlasses

37

添加:

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

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


注意:com.android.support包现已弃用。开发人员应使用对应的androidx包。 - 404pio

24

需要将这些依赖项添加进去

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

'com.android.support.test:rules:0.5'和'com.android.support.test:runner:0.5'是用于androidTest而不是test的。因此应该使用以下代码: androidTestCompile 'com.android.support.test:rules:0.5' androidTestCompile 'com.android.support.test:runner:0.5' - thuongle
Android Studio 3.x 上的 androidTestImplementation。 - Ewoks

14

从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;


11

添加依赖。

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

8

在Android中编写UI测试(在Android设备/模拟器上运行的测试)时,请确保:

  1. 测试类位于androidTest包中,而不是test包中。

  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 的包中。
2. 在 build.gradle 文件中添加以下依赖项。
testImplementation 'junit:junit:4.13'
testImplementation 'org.mockito:mockito-core:2.23.0'

1
添加 "androidTestImplementation" 和 "testImplementation" 对我很有帮助。谢谢! - Tim

0

你需要添加这些依赖项

    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

类似的答案已经存在: https://dev59.com/hFoU5IYBdhLWcg3wG0Oy#50443155 - DearDhruv

0

使用ActivityScenarioRule对我很有帮助。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接