为什么不能将AndroidJUnit4和ActivityTestRule导入到单元测试类中? [英] Why cannot I import AndroidJUnit4 and ActivityTestRule into my unit test class?
问题描述
我无法导入一些Android UI测试框架案例-我只是不知道出了什么问题!
I'm having trouble importing some of the Android UI testing framework clases - 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 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
- These run directly on the JVM and do not have access to the Android framework classes.
- They are kept in the
test/java
package - Dependencies need to added in the build.gradle file with the command
testCompile
- You generally use Mockito, Robolectric & JUnit for these tests
仪器测试
- 它们在Android模拟器上运行,并具有对所有Android类的完全访问权限
- 它们保存在
androidTest/java
包中 - 需要使用
androidTestCompile
将依赖项添加到build.gradle中
- 您通常使用Espresso和JUnit进行这些测试
- These run on an Android emulator and have full access to all the Android classes
- They are kept in the
androidTest/java
package - Dependencies need to be added to build.gradle with
androidTestCompile
- You generally use Espresso and JUnit for these tests
据我所知,您正在尝试使用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屋!