Kotlin + MockIto + Android Контрольно-измерительные приборы - PullRequest
3 голосов
/ 25 апреля 2020

Я пытаюсь запустить Instrumentation тест (androidTest) с использованием Mockito в Android с Kotlin.

  • У меня есть базовая библиотека в моих зависимостях. 'org.mockito:mockito-core:3.3.3'
  • Мой проект написан на Kotlin, поэтому я должен org.mockito:mockito-inline:3.3.3 исправить последнюю проблему класса.
  • Поскольку я хочу запустить макеты в Android, я 've 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'
}

Не возможно запустить Android instrumentation test с использованием Kotlin с Mockito?

ПРИМЕЧАНИЕ. Если вы считаете, что это дублирующий вопрос, я провел поиск по всему rnet. Существуют и другие проблемы с аналогичной трассировкой стека, но мой случай другой.

Любая помощь будет высоко оценена

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...