samedi 17 août 2019

Can't mock final class in test using Mockito with Kotlin

I'm trying to run a simple instrumentation test:

class DefaultTest {

    private val networkHelper = Mockito.mock(NetworkHelperImpl::class.java)

    @Test fun inter() {
        given(networkHelper.isConnectedToNetwork()).willReturn(true)
        assertFalse { networkHelper.isConnectedToNetwork(false) }
    }
}

But i cant bacause of error:

Mockito cannot mock/spy because :
- final class

How can i avoid it?

As this guide says:

https://antonioleiva.com/mockito-2-kotlin/

I'm create file:

enter image description here

With this line:

mock-maker-inline

But nothing changes.

Gradle is bad(i'm learning), but it must work. I use unit test too, so now i have:

//Tests
testImplementation 'junit:junit:4.13-beta-3'

testImplementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.41'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit:1.3.41'

testImplementation 'org.mockito:mockito-core:2.8.9'
androidTestImplementation 'org.mockito:mockito-android:2.24.5'
testImplementation "com.nhaarman:mockito-kotlin:1.4.0"

testImplementation 'org.amshove.kluent:kluent:1.14'

androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:rules:1.2.0'

Aucun commentaire:

Enregistrer un commentaire