运行 ActivityInstrumentationTestCase2 中的浓缩咖啡
Run espresso in ActivityInstrumentationTestCase2
我写了一些测试用例,但是当我尝试在其中包含浓缩咖啡时,它显示 java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
我知道我应该迁移到 junit 4,但我真的不想更改所有测试。
这里是测试class:
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()));
}
}
以下是我包含的依赖项:
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,您仍然必须使用测试支持库 runner 才能使用测试支持库中的内容,如下所示:
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
http://developer.android.com/tools/testing-support-library/index.html
我写了一些测试用例,但是当我尝试在其中包含浓缩咖啡时,它显示 java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
我知道我应该迁移到 junit 4,但我真的不想更改所有测试。
这里是测试class:
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()));
}
}
以下是我包含的依赖项:
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,您仍然必须使用测试支持库 runner 才能使用测试支持库中的内容,如下所示:
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
http://developer.android.com/tools/testing-support-library/index.html