Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Dagger 2-如果没有@Inject构造函数或@Providers注释方法,则无法提供_Java_Android_Kotlin_Dagger 2 - Fatal编程技术网

Java Dagger 2-如果没有@Inject构造函数或@Providers注释方法,则无法提供

Java Dagger 2-如果没有@Inject构造函数或@Providers注释方法,则无法提供,java,android,kotlin,dagger-2,Java,Android,Kotlin,Dagger 2,我对Dagger 2依赖注入有问题。我在Android项目中使用它,但在java库模块和单元测试中使用它 ApiTestModule.kt @Module class ApiTestModule { @Provides @Named("testDatasource") fun provideGithubTestDatasource(): GithubDatasourceImpl { return GithubDatasourceImpl(Mockito.

我对Dagger 2依赖注入有问题。我在Android项目中使用它,但在java库模块和单元测试中使用它

ApiTestModule.kt

@Module
class ApiTestModule {

    @Provides
    @Named("testDatasource")
    fun provideGithubTestDatasource(): GithubDatasourceImpl {
        return GithubDatasourceImpl(Mockito.mock(GithubService::class.java))
    }
}
TestComponent.kt

@Singleton
@Component(modules = [ApiTestModule::class])
interface TestComponent {
    fun inject(test: GithubDatasourceImplTest)
}
GithubDatasourceImplTest.kt

class GithubDatasourceImplTest {

    @set:[Inject Named("testDatasource")]
    lateinit var datasource: GithubDatasourceImpl

    @Before
    fun setUp() {
        val component = DaggerTestComponent.builder()
                .apiTestModule(ApiTestModule())
                .build()
        component.inject(this)
    }

    @Test
    fun test_create() {
        checkNotNull(datasource)
    }
}
模块构建.gradle

apply plugin: 'java-library'
apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'

kapt {
    generateStubs = true
}

dependencies {
    rootProject.ext.apiGithubDependencies.each {
        add(it.configuration, it.dependency)
    }
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"
我的错误

错误:

[Dagger/MissingBinding] hr.thekarlo95.api.github.GithubDatasourceImpl cannot be provided without an @Inject constructor or an @Provides-annotated method.
    public abstract void inject(@org.jetbrains.annotations.NotNull()
                         ^
      hr.thekarlo95.api.github.GithubDatasourceImpl is injected at
          hr.thekarlo95.api.github.GithubDatasourceImplTest.setDatasource(p0)
      hr.thekarlo95.api.github.GithubDatasourceImplTest is injected at
          hr.thekarlo95.api.github.di.TestComponent.inject(hr.thekarlo95.api.github.GithubDatasourceImplTest)

我无法让它工作,错误消息并没有真正的帮助。如果我从
TestComponent
中删除
fun inject(test:GithubDatasourceImplTest)
,所有的东西都会编译,但是我不能将依赖项注入到我的单元测试中。

这是在androidTest源集中的插入测试中,还是说测试源集中的单元测试?如果是后者,则不应使用Dagger,而应直接创建模拟对象。在Android项目的Java库模块中使用Dagger也没有什么意义。这只是java库模块中的普通JUnit(4.12)测试,因此在这个模块中不能完成任何与Android相关的工作。我想我可以为我的api服务创建一个工厂,但我想在整个应用程序堆栈中使用Dagger。理论上,Dagger不是Android库,所以我看不出有任何理由不在这里使用它。我想直接为测试目的创建它是一种选择。我只是感到困惑,即使我以Daggers文档描述的方式这样做了,但它仍然不起作用。当然,您也可以将Dagger与vanilla Java一起使用。在这种情况下,没有理由这么做。您创建这个大型测试工具是为了注入一个可以在测试中创建的模拟。您是否尝试过使用
@field
而不是
@set