为什么我不能将 AndroidJUnit4 和 ActivityTestRule 导入我的单元测试类? [英] Why can't I import AndroidJUnit4 and ActivityTestRule into my unit test class?

查看:32
本文介绍了为什么我不能将 AndroidJUnit4 和 ActivityTestRule 导入我的单元测试类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having trouble importing some of the Android UI testing framework classes - I just can't figure out what is going wrong!

这是我的课:

@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".我尝试导入它们,但找不到.

But for some reason I get the errors 'cannot find symbol ActivityTestRule' and 'cannot find symbol AndroidJUnit4'. I've tried to import them but they cannot be found.

build.gradle 中的依赖项设置为:

The dependencies in build.gradle are set to:

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'

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

So I think I have all the dependencies setup - I've been trying many things but with no luck.

有人有什么想法吗?

推荐答案

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

There are two different types of tests you can set up in 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 包.

From what I can tell you are trying to write instrumentation tests with Espresso but have your test in the test/java package which is for unit tests. In that case you need to move your test class to the androidTest/java package.

这篇关于为什么我不能将 AndroidJUnit4 和 ActivityTestRule 导入我的单元测试类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆