fabioCollini / DaggerMock

A JUnit rule to easily override Dagger 2 objects
Apache License 2.0
1.16k stars 91 forks source link

AndroidX: throw org.mockito.exceptions.base.MockitoException #91

Closed roman-yu closed 5 years ago

roman-yu commented 5 years ago

Hi Fabio Collini,

Thanks for creating such a great library for testing with Dagger on android! I tried to modify RealWorldAppKotlinAllOpen with androidx like below: daggermock-kotlin module build.gradle changes:

compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

to

compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

RealWorldAppKotlinAllOpen module build.gradle changes:

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

to

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
dependencies {
    testImplementation 'junit:junit:4.12'
    testImplementation "org.mockito:mockito-core:$MOCKITO_VERSION"
    implementation 'androidx.appcompat:appcompat:1.0.2' //updated
    implementation 'com.google.android.material:material:1.1.0-alpha02' //updated

    implementation "com.google.dagger:dagger:$DAGGER_VERSION"
    implementation 'javax.annotation:jsr250-api:1.0'
    kapt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
    testAnnotationProcessor "com.google.dagger:dagger-compiler:$DAGGER_VERSION"

    testImplementation project(':daggermock')
    androidTestImplementation project(':daggermock')

    testImplementation 'org.assertj:assertj-core:2.5.0'

    testImplementation "org.robolectric:robolectric:4.1"
    testImplementation "androidx.test:runner:1.1.0-alpha4" //updated
    testImplementation "androidx.test:rules:1.1.0-alpha4" //updated

    androidTestImplementation 'androidx.test:core:1.1.0' //updated
    androidTestImplementation "androidx.test:runner:1.1.0-alpha4" //updated
    androidTestImplementation "androidx.test:rules:1.1.0-alpha4" //updated
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0') { //updated
        exclude group: 'javax.inject', module: 'javax.inject'
        exclude group: 'com.squareup', module: 'javawriter'
    }
    androidTestImplementation "org.mockito:mockito-android:$MOCKITO_VERSION" //new
    androidTestImplementation project(':daggermock-kotlin')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

After that, running MainActivityTest androidTest got below error:

org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class it.cosenonjaviste.daggermock.realworldappkotlin.services.RestService.

Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.

...

I didn't change any source code(only import) of RealWorldAppKotlinAllOpen. Hanging around for a while, but still not able to make it work. Hope you could give some hints, thanks!

fabioCollini commented 5 years ago

Hi, I have just updated all the examples to AndroidX and Kotlin 1.3.11 and it works. Have you changed the compileSkd and targetSdk to 28?

roman-yu commented 5 years ago

Hi @fabioCollini, Cool! I didn't update Kotlin version, it works after upgrading to 1.3.11, do you what the magic to make it work behind upgrading Kotlin?

fabioCollini commented 5 years ago

Hi, that example uses Kotlin all open plugin https://kotlinlang.org/docs/reference/compiler-plugins.html Unfortunately I wasn't able to reproduce the issue so I am not sure what was the cause of it. It seems like something related to Kotlin so it's strange that it happened after upgrading to AndroidX.