我刚刚开始为Android项目的UI测试编译类和模块。一个正在使用di,并且有必要在我的测试中注入一些依赖性。立即解决我的测试组件时遇到了一些麻烦。为了简化问题,我创建了一个空测试和一个空测试组件。尽管如此,该组件仍然无法解析,而同一构造已在另一个android项目中成功解析。
在我看来,使用这样的空组件可以切断大多数可以隐藏错误的地方,但gradle文件除外(无论如何都是错误的)。
这是组件和测试类:
class LoginTest {
init {
try {
UiThreadStatement.runOnUiThread {
val testTest: TestComponent = DaggerTestComponent
.builder()
.build()
}
} catch (throwable: Throwable) {
throwable.printStackTrace()
}
}
@Test
fun test() {
}
}
---------------------
@Component
interface TestComponent {
}
这是应用程序gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion configuration.compileSdkVersion
buildToolsVersion configuration.buildToolsVersion
defaultConfig {
applicationId configuration.package
minSdkVersion configuration.minSdkVersion
targetSdkVersion configuration.targetSdkVersion
versionCode configuration.versionCode
versionName "${configuration.versionMajor}" +
".${configuration.versionMinor}" +
".${configuration.versionPatch}"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
signingConfigs {
debug {
storeFile file("debug.keystore")
}
release {
storePassword KEYSTORE_PASSWORD
keyAlias KEY_ALIAS
keyPassword KEY_PASSWORD
}
}
buildTypes {
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
flavorDimensions "default"
productFlavors {
mock {
dimension "default"
minSdkVersion configuration.minSdkVersion
ext.enableCrashlytics = false
}
dev {
versionNameSuffix "-dev"
applicationIdSuffix '.dev'
minSdkVersion configuration.minSdkVersion
ext.enableCrashlytics = false
}
prod {
dimension "default"
minSdkVersion configuration.minSdkVersion
}
}
kapt {
generateStubs = true
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
prod.java.srcDirs += 'src/prod/kotlin'
dev.java.srcDirs += 'src/dev/kotlin'
debug.java.srcDirs += 'src/debug/kotlin'
test.java.srcDirs += 'src/test/kotlin'
mock.java.srcDirs += 'src/mock/kotlin'
androidTest.java.srcDirs += 'src/androidTest/kotlin'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$libraries.kotlin_version"
// Support
implementation "com.android.support:appcompat-v7:$libraries.supportVersion"
implementation "com.android.support:design:$libraries.supportVersion"
implementation "com.android.support:recyclerview-v7:$libraries.supportVersion"
// Rx
implementation "io.reactivex.rxjava2:rxjava:$libraries.rxjava"
implementation "io.reactivex.rxjava2:rxandroid:$libraries.rxandroid"
implementation "com.jakewharton.rxbinding2:rxbinding-kotlin:$libraries.rxbinding"
implementation "com.jakewharton.rxbinding2:rxbinding-design-kotlin:$libraries.rxbinding"
implementation "com.jakewharton.rxbinding2:rxbinding-recyclerview-v7-kotlin:$libraries.rxbinding"
// OkHttpVersion
implementation "com.squareup.okhttp3:okhttp:$libraries.okhttpVersion"
implementation "com.squareup.okhttp3:logging-interceptor:$libraries.okhttpVersion"
// Retrofit
implementation "com.squareup.retrofit2:converter-gson:$libraries.retrofit"
implementation "com.squareup.retrofit2:retrofit:$libraries.retrofit"
implementation "com.squareup.retrofit2:adapter-rxjava2:$libraries.retrofit"
// Dagger
implementation "com.google.dagger:dagger:$libraries.dagger"
implementation "com.google.dagger:dagger-android-support:$libraries.dagger"
kapt "com.google.dagger:dagger-android-processor:$libraries.dagger"
kapt "com.google.dagger:dagger-compiler:$libraries.dagger_compiler"
// Junit
testImplementation "junit:junit:$libraries.junitVersion"
// Runner
androidTestImplementation "com.android.support.test:runner:$libraries.testRunner"
// Espresso
androidTestImplementation "com.android.support.test.espresso:espresso-core:$libraries.espressoCore"
// Spoon
androidTestImplementation 'com.squareup.spoon:spoon-client:1.7.1'
}
UPD:该错误非常无用,看起来像:
e: ...\LoginTest.kt: (11, 37): Unresolved reference: DaggerTestComponent
:app:compileMockDebugAndroidTestKotlin FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileMockDebugAndroidTestKotlin'.
> Compilation error. See log for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
答案 0 :(得分:1)
我为谁几次前发布正确答案表示歉意。
要添加到gradle中的键字符串:
kaptAndroidTest "com.google.dagger:dagger-compiler:$libraries.dagger"
kaptAndroidTest "com.google.dagger:dagger-android-processor:$libraries.dagger"