【问题标题】:Kotlin + MockIto + Android Instrumentation TestKotlin + MockIto + Android Instrumentation 测试
【发布时间】:2020-04-25 06:17:10
【问题描述】:

我正在尝试在 Android 中使用 Mockito 和 Kotlin 运行 Instrumentation 测试 (androidTest)。

  • 我的依赖项中有核心库。 'org.mockito:mockito-core:3.3.3'
  • 我的项目是用Kotlin 编写的,所以我已经org.mockito:mockito-inline:3.3.3 来解决final class 问题。
  • 因为我想在 Android 中运行模拟,所以我的依赖项中也有 org.mockito:mockito-android:3.3.3。

但在编译代码时出现以下错误

发现多个文件具有独立于操作系统的路径 'mockito-extensions/org.mockito.plugins.MockMaker'

这是重现该问题的示例测试代码。将此测试作为仪器测试运行。 (androidTest)。

MainActivityTest

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations

class Calculator {
    fun add(x: Int, y: Int): Int {
        return x + y
    }
}

@RunWith(AndroidJUnit4::class)
class MainActivityTest {

    @Mock
    lateinit var calculator: Calculator

    @Before
    fun before() {
        MockitoAnnotations.initMocks(this)
    }

    @Test
    fun test() {
        `when`(calculator.add(10, 10)).thenReturn(20)
        assertEquals(calculator.add(10, 10), 20)
    }
}

app/build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
}

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "com.theapache64.mockitosample"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'org.mockito:mockito-core:3.3.3'
    androidTestImplementation 'org.mockito:mockito-android:3.3.3'
    androidTestImplementation 'org.mockito:mockito-inline:3.3.3'
}

难道不能使用Kotlin 和Mockito 运行Android instrumentation test 吗?

注意:如果您认为这是一个重复的问题,我已经在整个互联网上进行了搜索。类似的堆栈跟踪还有其他问题,但我的情况不同。

任何帮助将不胜感激

【问题讨论】:

    标签: android kotlin mockito


    【解决方案1】:

    我遇到了同样的问题,我的解决方案是这个答案https://stackoverflow.com/a/41594789/2286422,即使用依赖于 Mockito 的 dexmaker 库。

    移除其他Mockito androidTestImplementation依赖,只添加以下一个(可以找到最新版本here):

    androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito:2.28.1'
    

    请注意,此解决方案仅适用于搭载 Android P 或更高版本 (official documentation) 的设备。

    然后在实际测试中,我通过以下方式使用 Mockito:

    @RunWith(MockitoJUnitRunner::class) // use Mockito runner to be able to use @Mock annotations
    class MockitoTest {
    
        @Mock
        private lateinit var generalPref: GeneralPreferences
    
        @Before
        fun setup() {
            // make setup
        }
    
        @After
        @Throws(IOException::class) {
            // make cleanup
        }
    
        @Test
        fun testUser() {
            val repo: UserLocalRepository =
                UserLocalRepositoryImpl(generalPref) // generalPref is a mock
            val user = repo.getUser()
            Truth.assertThat(user).isNotNull()
        }
    }
    

    【讨论】:

      【解决方案2】:

      我使用了这些依赖项,它们对我有用

      androidTestImplementation("androidx.test.ext:junit:1.1.3")
      androidTestImplementation("com.linkedin.dexmaker:dexmaker-mockito-inline:2.28.1")
      testImplementation("junit:junit:4.13.2")
      testImplementation("androidx.test:core:1.4.0")
      testImplementation("org.mockito:mockito-android:4.2.0")
      testImplementation("org.mockito:mockito-inline:4.2.0")
      testImplementation("org.mockito:mockito-core:4.2.0")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多