DaggerTestAppComponent

DaggerTestAppComponent

本文介绍了dagger2和Kotlin的Android单元测试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在android的测试文件夹中创建一个dagger组件接口,但是在Dagger完成生成的类后,其显示错误为无法访问TestComponent,它是com.example.animals.di的超类型。 DaggerTestAppComponent。请检查您的模块类路径中是否缺少依赖项或发生冲突的依赖项。能否请我帮助解决此问题。

I am trying to create a dagger component interface in the test folder of android,but after Dagger is done with the generated class its show an error as "Cannot access TestComponent which is a supertype of com.example.animals.di.DaggerTestAppComponent.Check your module classpath for missing or conflicting dependency".Can you please help me to fix this issue.

gradle dependecies:
def lifeCycleExtensionVersion = '1.1.1'
def supportVersion = '28.0.0'
def retrofitVersion = '2.3.0'
def glideVersion = '4.9.0'
def rxJavaVersion = '2.1.1'
def navVersion = '2.1.0-alpha05'
def daggerVersion = '2.14.1'
def mockitoVersion = '2.11.0'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation "com.android.support:design:$supportVersion"
    implementation "android.arch.lifecycle:extensions:$lifeCycleExtensionVersion"

    implementation "androidx.navigation:navigation-fragment-ktx:$navVersion"
    implementation "androidx.navigation:navigation-ui-ktx:$navVersion"

    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofitVersion"

    implementation "io.reactivex.rxjava2:rxjava:$rxJavaVersion"
    implementation "io.reactivex.rxjava2:rxandroid:$rxJavaVersion"

    implementation "com.github.bumptech.glide:glide:$glideVersion"

    implementation "com.android.support:palette-v7:$supportVersion"


    implementation "com.google.dagger:dagger:$daggerVersion"
    implementation "com.google.dagger:dagger-android-support:$daggerVersion"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    kapt "com.google.dagger:dagger-compiler:$daggerVersion"
    kapt "com.google.dagger:dagger-android-processor:$daggerVersion"


    testImplementation "org.mockito:mockito-inline:$mockitoVersion"
    testImplementation "android.arch.core:core-testing:1.1.1"
    kaptTest "com.google.dagger:dagger-compiler:$daggerVersion"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
 }

TestAppComponent:
   package com.example.animals.di

 import com.example.animals.base.BaseTest
 import dagger.Component
 import javax.inject.Singleton

@Singleton
@Component(modules = [])
interface TestAppComponent {
   fun inject(baseTest: BaseTest)
}

BaseTest:
包com.example.animals.base

BaseTest: package com.example.animals.base

 import android.app.Application
 import com.example.animals.di.DaggerTestAppComponent
 import org.junit.Before
 import org.mockito.Mockito

open class BaseTest {

    val application = Mockito.mock(Application::class.java)

    @Before
     fun setUp() {
     configureDi()

    }

    private fun configureDi() {
       var testAppComponent = DaggerTestAppComponent.builder().build()

        testAppComponent.inject(this)
    }
 }


推荐答案

在这里,我在上设置测试时遇到了同样的问题。
在中看到示例时,我注意到我们生成的源没有包含在源集中。

I had the same problem here setting up my tests on our repo here.When seeing examples from Tivi, I noticed that our generated sources weren't being included to the source sets.

在项目的某处添加gradle脚本应该可以解决您的问题。

Adding a gradle script somewhere in your project should fix your problem.

例如:(与Tivi相同)。

Ex: https://github.com/toggl/android/blob/768f01b55c47ffd3bb7c4a82dea4abf50eac9e1c/database-android/build.gradle#L46-L63 (same as Tivi).

main.java.srcDirs += 'src/main/kotlin'
debug.java.srcDirs += 'src/debug/kotlin'
release.java.srcDirs += 'src/release/kotlin'
test.java.srcDirs += 'src/test/kotlin'

// For kapt stubs
main.java.srcDirs += [file("$buildDir/generated/source/kapt/main")]
debug.java.srcDirs += [file("$buildDir/generated/source/kapt/debug")]
release.java.srcDirs += [file("$buildDir/generated/source/kapt/release")]
test.java.srcDirs += [file("$buildDir/generated/source/kapt/test")]

// For kotlin code gen during kapt
main.java.srcDirs += [file("$buildDir/generated/source/kaptKotlin/main")]
debug.java.srcDirs += [file("$buildDir/generated/source/kaptKotlin/debug")]
release.java.srcDirs += [file("$buildDir/generated/source/kaptKotlin/release")]
test.java.srcDirs += [file("$buildDir/generated/source/kaptKotlin/test")]

这篇关于dagger2和Kotlin的Android单元测试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:13