问题
I want to test a class using JUnit5 but at the time gradle runs the app:kaptDebugAndroidTestKotlin task I get the issue.
I have already tried the solutions in the following links but nothing helps so far:
NonExistentClass cannot be converted to Annotation
error: incompatible types: NonExistentClass cannot be converted to Annotation @error.NonExistentClass()
my project build.gradle file:
buildscript {
ext.kotlin_version = '1.3.40'
ext.anko_version='0.10.8'
ext.junit_version='5.5.0'
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "io.realm:realm-gradle-plugin:5.11.0"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
my module build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.hays.test"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
androidExtensions {
experimental = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
}
realm {
kotlinExtensionsEnabled = true
}
kapt {
correctErrorTypes = true
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit_version"
testImplementation "org.junit.jupiter:junit-jupiter-engine:$junit_version"
testImplementation "org.junit.vintage:junit-vintage-engine:$junit_version"
testImplementation 'io.mockk:mockk:1.9.3'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.9'
implementation 'io.reactivex.rxjava2:rxkotlin:2.3.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.jakewharton.rxbinding2:rxbinding:2.2.0'
implementation 'com.jakewharton.rxbinding2:rxbinding-appcompat-v7:2.2.0'
implementation 'com.android.support:support-annotations:28.0.0'
implementation group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-android', version: '1.3.0-M1'
implementation "org.jetbrains.anko:anko:$anko_version"
}
and my gradle.properties file:
kotlin.code.style=official
org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
android.enableJetifier=true
android.databinding.enableV2=true
I tried to run the test but what I get while compiling is the following:
C:\project_path\app\build\tmp\kapt3\stubs\debugAndroidTest\com\package\test\module\search\SearchPresenterTest.java:20: error: incompatible types: NonExistentClass cannot be converted to Annotation @error.NonExistentClass()
The file under build folder has some of the following:
@error.NonExistentClass()
public final void setUpMocks() {
}
@error.NonExistentClass()
public final void onSearch() {
}
While the original kotlin test file is like the following lines:
@BeforeEach
fun setUpMocks() {
clearAllMocks()
}
@Test
fun onSearch() {
verify { view.onSearch() }
}
回答1:
Seems that I mixed some JUnit4 dependencies with those of JUnit5 so by removing the following lines (like the AndroidJUnitRunner class) in the dependencies I got it working:
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
kapt("org.jetbrains.kotlin.kapt:org.jetbrains.kotlin.kapt.gradle.plugin:1.3.31:pom")
And I added back again those of the jupiter packages:
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit_version"
testImplementation "org.junit.jupiter:junit-jupiter-engine:$junit_version"
testImplementation "org.junit.vintage:junit-vintage-engine:$junit_version"
Also the org.junit classes in all the tests have to be replaced by their equivalent of the org.junit.jupiter package. That made it compile the test without issues.
来源:https://stackoverflow.com/questions/56863896/nonexistentclass-cannot-be-converted-to-annotation-appkaptdebugandroidtestkot