我使用的匕首依赖注入framwork。 它的工作很好,到目前为止,但我同时使用匕首Android的单元测试和无法弄清楚,为什么(可能是因为不正确使用匕首)有一个问题。
我有以下异常
java.lang.IllegalArgumentException: Failed to construct com.couchsurfing.mobile.android.CSApplication$ProdModule
at dagger.internal.plugins.reflect.ReflectiveModuleAdapter.newModule(ReflectiveModuleAdapter.java:94)
at dagger.internal.RuntimeAggregatingPlugin.getModuleAdapter(RuntimeAggregatingPlugin.java:99)
at dagger.internal.RuntimeAggregatingPlugin.collectIncludedModulesRecursively(RuntimeAggregatingPlugin.java:85)
at dagger.internal.RuntimeAggregatingPlugin.getAllModuleAdapters(RuntimeAggregatingPlugin.java:71)
at dagger.ObjectGraph.makeGraph(ObjectGraph.java:115)
at dagger.ObjectGraph.create(ObjectGraph.java:103)
at com.couchsurfing.mobile.android.core.MessageManagerTest.setUp(MessageManagerTest.java:34)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)
Caused by: java.lang.NoSuchMethodException: <init> []
at java.lang.Class.getConstructorOrMethod(Class.java:460)
at java.lang.Class.getDeclaredConstructor(Class.java:588)
at dagger.internal.plugins.reflect.ReflectiveModuleAdapter.newModule(ReflectiveModuleAdapter.java:88)
... 15 more
码生成的例外的是以下内容:
public class MessageManagerTest extends InstrumentationTestCase {
@Inject
MessageManager mMessageManager;
@Inject
MessageOperations.Factory mMOFactory;
@Inject
Context mAppContext;
@Override
public void setUp() {
ObjectGraph.create(new TestModule()).inject(this);
}
@Module(
includes = CSApplication.ProdModule.class,
entryPoints = MessageManagerTest.class,
overrides = true)
static class TestModule {
@Provides
MessageOperations.Factory provideMessageOperationsFactory() {
return Mockito.mock(MessageOperations.Factory.class);
}
@Provides
Context provideAppContext() {
return Mockito.mock(Context.class);
}
}
public void testCreateMessage() throws RemoteException, OperationApplicationException {
...
}
}
注意模块CSApplication $ ProdModule在应用的正式版的使用和效果很好。