无法在Mockito和Kotlin中模拟最后的测试课程 [英] Can't mock final class in test using Mockito with Kotlin

查看:141
本文介绍了无法在Mockito和Kotlin中模拟最后的测试课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个简单的仪器测试:

class DefaultTest {

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

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

但是我不能因为错误:

Mockito cannot mock/spy because :
- final class

我该如何避免呢?

如本指南所述:

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

我正在创建文件:

此行:

mock-maker-inline

但是什么都没有改变.

Gradle不好(我正在学习),但必须有效.我也使用单元测试,所以现在有了:

//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. 执行以下步骤: https://antonioleiva.com/mockito-2-kotlin/
  2. 更新库并将androidTestImplementation替换为 testImplementation .就我而言:

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

    testImplementation'org.mockito:mockito-core:3.0.0'(更新)

    testImplementation'org.mockito:mockito-android:2.24.5'(用testImplementation代替androidTestImplementation)

  3. 从每个测试类中删除 MockitoAnnotations.initMocks(this).

已通过Android Studio 3.5测试

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. 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

这篇关于无法在Mockito和Kotlin中模拟最后的测试课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆