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

旧城冷巷雨未停 提交于 2021-02-07 08:45:10

问题


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:

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'

androidTestImplementation 'org.mockito:mockito-core:3.0.0'
androidTestImplementation 'org.mockito:mockito-android:2.24.5'
androidTestImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.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'

回答1:


  1. Follow this steps: https://antonioleiva.com/mockito-2-kotlin/
  2. Update the libraries and replace androidTestImplementation with testImplementation. In my case:

    testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0 (update)

    testImplementation 'org.mockito:mockito-core:3.0.0' (update)

    testImplementation 'org.mockito:mockito-android:2.24.5' (replace androidTestImplementation with testImplementation)

  3. Remove MockitoAnnotations.initMocks(this) from each Test class.

Tested with Android Studio 3.5




回答2:


Simply adding

testImplementation 'org.mockito:mockito-inline:2.8.47'

resolved the issue.




回答3:


Have you tried mockito-kotlin? Add this to your dependencies:

testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0"




回答4:


I think the problem here is that you're compiling Mockito only for Instrumentation tests - the ones inside androidTest. However, you want to do this for the unit tests according to where you've put the Mockito extension configuration.

When you use androidTestImplementation you're compiling that dependency for instrumentation tests. Using testImplementation will compile for unit tests. Replace:

androidTestImplementation 'org.mockito:mockito-core:3.0.0'
androidTestImplementation 'org.mockito:mockito-android:2.24.5'
androidTestImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0"

With:

testImplementation 'org.mockito:mockito-core:3.0.0'
testImplementation 'org.mockito:mockito-android:2.24.5'
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0"



回答5:


Annotate your test class by @RunWith(MockitoJUnitRunner::class). So your class will looked like this:

@RunWith(MockitoJUnitRunner::class)
class DefaultTest {

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

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

For me, I worked everything as like as you did and failed. I was missed just that annotation part. After adding annotation it's working as expected.



来源:https://stackoverflow.com/questions/57536196/cant-mock-final-class-in-test-using-mockito-with-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!