让Mockito与RoboGuice和Robolectric合作

时间:2014-08-21 21:02:41

标签: android testing mockito robolectric roboguice

我几乎绝望地让Mockito与RoboGuice和Robolectric合作。

我有一个gradle多项目,其中包含一个使用RoboGuice设计的Android应用程序。现在我有一个测试用例,我想覆盖默认的Robuguice模块以注入一些模拟。但遗憾的是我无法让它发挥作用。

到目前为止,这是我的方法:

@RunWith(RobolectricTestRunner.class)
public class NetworkUtilTest {

    @Inject
    private NetworkUtil networkUtil;

    private ErrorDialog errorDialog = mock(ErrorDialog.class, Mockito.RETURNS_DEEP_STUBS);

    private FragmentActivity activity;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        activity = Robolectric.buildActivity(MainActivity.class).create().get();
                RoboGuice.setBaseApplicationInjector(Robolectric.application, RoboGuice.DEFAULT_STAGE, Modules.override(RoboGuice.newDefaultRoboModule(Robolectric.application)).with(new MyTestModule()));

        RoboInjector injector = RoboGuice.getInjector(activity);
        injector.injectMembersWithoutViews(this);
    }

    @Test
    public void testNetworkInfoNull() {
        ConnectivityManager manager = (ConnectivityManager) Robolectric.application.getSystemService(Context.CONNECTIVITY_SERVICE);
        ShadowConnectivityManager shadowConnectivityManager = shadowOf(manager);
        shadowConnectivityManager.setActiveNetworkInfo(null);

        networkUtil.isNetworkAvailable();

        verify(errorDialog).show(R.string.network_error);
    }

    private class MyTestModule extends AbstractModule {
        @Override
        protected void configure() {
            bind(ErrorDialog.class).toInstance(errorDialog);
        }
    }
}

许多说明正在使用     RoboGuice.getInjector(Robolectric.application) 但是我没有使用模拟器的绑定进行测试就可以使用上面的方法。所以它似乎是正确的方式?

如果我运行测试,我收到以下错误:

1) null returned by binding at roboguice.config.DefaultRoboModule.configure(DefaultRoboModule.java:131)
        but roboguice.inject.FragmentManagerProvider.activity is not @Nullable
while locating roboguice.inject.NullProvider<android.app.Activity>
        at roboguice.config.DefaultRoboModule.configure(DefaultRoboModule.java:131)
        while locating android.app.Activity
        for field at roboguice.inject.FragmentManagerProvider.activity(Unknown Source)
        at roboguice.inject.FragmentManagerProvider.class(Unknown Source)
        while locating roboguice.inject.FragmentManagerProvider
        while locating android.support.v4.app.FragmentManager
        for field at com.example.test.dialog.ErrorDialog.fragmentManager(Unknown Source)
        at com.example.test.util.NetworkUtilTest$MyTestModule.configure(NetworkUtilTest.java:80)

        1 error
        com.google.inject.CreationException: Guice creation errors:

        1) null returned by binding at roboguice.config.DefaultRoboModule.configure(DefaultRoboModule.java:131)
        but roboguice.inject.FragmentManagerProvider.activity is not @Nullable
while locating roboguice.inject.NullProvider<android.app.Activity>
        at roboguice.config.DefaultRoboModule.configure(DefaultRoboModule.java:131)
        while locating android.app.Activity
        for field at roboguice.inject.FragmentManagerProvider.activity(Unknown Source)
        at roboguice.inject.FragmentManagerProvider.class(Unknown Source)
        while locating roboguice.inject.FragmentManagerProvider
        while locating android.support.v4.app.FragmentManager
        for field at com.example.test.dialog.ErrorDialog.fragmentManager(Unknown Source)
        at com.example.test.util.NetworkUtilTest$MyTestModule.configure(NetworkUtilTest.java:80)

        1 error
        at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:435)
        at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:175)
        at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:109)
        at com.google.inject.Guice.createInjector(Guice.java:95)
        at com.google.inject.Guice.createInjector(Guice.java:83)
        at roboguice.RoboGuice.setBaseApplicationInjector(RoboGuice.java:94)
        at com.example.test.util.NetworkUtilTest.setUp(NetworkUtilTest.java:58)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
        at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:250)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
        at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
        at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
        at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
        at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
        at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
        at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
        at $Proxy2.processTestClass(Unknown Source)
        at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
        at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
        at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
        at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:722)

有人知道这个问题的解决方案吗? Mockito,RoboGuice和Robolectric有很多高级Android测试指南,所以我认为这有用吗?

我已经看了一下astroboy的例子,但它没有让我更进一步。

此致 托拜厄斯

1 个答案:

答案 0 :(得分:0)

感谢您的回答! 我最后通过在我的测试模块中添加以下行来修复它:

bind(FragmentManager.class).toInstance(activity.getSupportFragmentManager());

然后测试成功运行。我希望没有其他问题。我浪费了足够的时间: - )