在创建图表的时候匕首抛出NoSuchMethodException [英] Dagger throw a NoSuchMethodException when creating the graph

查看:185
本文介绍了在创建图表的时候匕首抛出NoSuchMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用匕首的依赖注入framwork。它的工作很好,到目前为止,但我在使用匕首Android的单元测试和想不通为什么(可能是因为不正确使用匕首)有一个问题。

I am using Dagger as dependency injection framwork. It's working well so far but I am having an issue while using Dagger for Android unit testing and can't figure out why (Probably because of an incorrect use of Dagger).

我有以下异常

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

在code产生异常如下:

The code generating the exception is the following:

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 在应用程序的生产版本中使用,并且效果很好。

Note that the module CSApplication$ProdModule is used in the production version of the app and works well.

推荐答案

您需要给ProdModule一个无参非私有构造函数。而类需要是静态的。如果没有这把匕首不能构造你的模块。

You need to give ProdModule a no-args non-private constructor. And the class needs to be static. Without this Dagger can't construct your module.

这篇关于在创建图表的时候匕首抛出NoSuchMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆