我对在Android应用程序中使用Dagger 2感到非常新鲜。 当前,我们有应用程序将其中的某些部分作为依赖项注入。但是,我无法使用以下代码在Espresso测试中监视HomeActivity:
@LargeTest
@RunWith(AndroidJUnit4.class)
Public class ConnectionRobotTests {
private HomeRobot homeRobot = new HomeRobot();
private NetworkResults networkResults = new NetworkResults();
HomeActivity object;
SingleActivityFactory<HomeActivity> activityFactory =
new SingleActivityFactory<HomeActivity>(HomeActivity.class) {
@Override
protected HomeActivity create(Intent intent) {
object = spy(getActivityClassToIntercept());
return object;
}
};
@Rule public ActivityTestRule<HomeActivity> testRule =
new ActivityTestRule<>(activityFactory, true, true);
@AfterClass
public static void afterTest() {
User.logout();
Storage.clearAllPreferences();
}
@Test
public void connectAndDisconnectViaQuickConnect() {
MockConnectionHelper.mockConnection(testRule);
networkResults.isServerAvailable();
homeRobot.connect().isConnectedTo();
ConnectionResult result = homeRobot.disconnect();
result.isDisconnected();
}
这是TestAppComponent.class:
@Singleton
@Component(modules = {AndroidSupportInjectionModule.class,
ActivityBuilder.class, MockAppModule.class})
public interface TestAppComponent extends AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(TestApplication application);
TestAppComponent build();
}
}
MockAppModule.class:
@Module
public class MockAppModule {
@Singleton
@Provides
Gson provideGson() {
return new Gson();
}
}
TestApplication.class:
public class TestApplication extends MyApplication {
@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
return DaggerTestAppComponent.builder().application(this).build();
}
}
运行测试时出现错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.android/com.application.android.ui.home.HomeActivity}: java.lang.IllegalArgumentException: No injector factory bound for Class<com.application.android.ui.home.HomeActivity$MockitoMock$1418108435>. Injector factories were bound for supertypes of com.application.android.ui.home.HomeActivity$MockitoMock$1418108435: [com.application.android.ui.home.HomeActivity]. Did you mean to bind an injector factory for the subtype?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IllegalArgumentException: No injector factory bound for Class<com.application.android.ui.home.HomeActivity$MockitoMock$1418108435>. Injector factories were bound for supertypes of com.application.android.ui.home.HomeActivity$MockitoMock$1418108435: [com.application.android.ui.home.HomeActivity]. Did you mean to bind an injector factory for the subtype?
at dagger.android.DispatchingAndroidInjector.inject(DispatchingAndroidInjector.java:104)
at dagger.android.AndroidInjection.inject(AndroidInjection.java:59)
at com.application.android.ui.home.HomeActivity.onCreate(HomeActivity.java:96)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:667)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
是否缺少我要添加的内容?还是应该以不同的方式对待HomeActivity的间谍? 我试图为HomeActivity添加Injector工厂,但是它没有做任何更改。有没有人建议改变间谍HomeActivity?
答案 0 :(得分:0)
监视活动的问题在于,您只能为超类(在本例中为HomeActivity)提供AndroidInjector.Factory,但Mockito在运行时会创建一个子类HomeActivity $ MockitoMock $...。
到目前为止,我发现-无法为子类创建AndroidInjector.Factory。
可以模拟AndroidInjector.Factory,但Dagger开发人员本身认为这是一种不良做法。
UPD :我最终使用Robolectric测试了活动\服务的生命周期,并检查它是否也满足您的需求。