当我尝试使用daggerMock替换依赖项时,出现以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.test.espresso.intent.Intents.internalRelease()' on a null object reference
at android.support.test.espresso.intent.Intents.release(Intents.java:140)
at android.support.test.espresso.intent.rule.IntentsTestRule.afterActivityFinished(IntentsTestRule.java:68)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:260)
这是我的Espresso Intents规则:
@Rule public IntentsTestRule<MyActivity> activityRule =
new IntentsTestRule<MyActivity>(MyActivity.class, true, false);
这是我的考试
@Test
public void shouldDisplayToolbarOnStart() {
startActivity();
onView(withId(R.id.toolbar)).check(matches(isDisplayed()));
}
private MyActivity startActivity() {
return activityRule.launchActivity(null);
}
我的Espresso自定义规则:
public class EspressoDaggerMockRule extends DaggerMockRule<RootComponent> {
public EspressoDaggerMockRule() {
super(RootComponent.class, new MainModule(getApp()));
set(new DaggerMockRule.ComponentSetter<RootComponent>() {
@Override public void setComponent(RootComponent component) {
getApp().setComponent(component);
}
});
}
private static App getApp() {
return (App) InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext();
}
}
临时解决方法
找到了一种停止崩溃的方法:
@Rule public IntentsTestRule<MyActivity> activityRule =
new IntentsTestRule<MyActivity>(MyActivity.class, true, false);
替换为
@Rule public ActivityTestRule<MyActivity> activityRule =
new ActivityTestRule<MyActivity>(MyActivity.class, true, false);