在ActivityInstrumentationTestCase2中运行浓缩咖啡 [英] Run espresso in ActivityInstrumentationTestCase2
问题描述
我已经写了一些测试用例,但是当我尝试在其中包含espresso时,它会显示java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
I have written some test cases but when I try to include espresso in it, it shows java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
我知道我应该迁移到junit 4,但是我真的不想更改所有测试.
I know I should migrate to junit 4 but I really dont want to change all the tests.
这是测试班:
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
private Activity activity;
public MainActivityTest() {
super(MainActivity.class);
}
@Before
protected void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
activity = getActivity();
}
@Test
public void testEmptyField() {
onView(withId(R.id.editText)).perform(typeText(""));
onView(withId(R.id.buttonVerify)).perform(click());
onView(withText("Please input text")).check(matches(isDisplayed()));
}
}
以下是我包含的依赖项:
Here are the dependencies I've included:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':UniversalImageLoader')
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
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'
}
推荐答案
即使您使用的是ActivityInstrumentationTestCase2,仍必须使用测试支持库运行器来使用测试支持库中的内容,像这样:
Even if you are using ActivityInstrumentationTestCase2, you still must use the test support lib runner to use the things from the test support lib, like this:
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
http://developer.android.com/tools/testing- support-library/index.html
这篇关于在ActivityInstrumentationTestCase2中运行浓缩咖啡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!