![](/img/trans.png)
[英]Android Instrumentation test using Mockito: mocking not working
[英]Kotlin + MockIto + Android Instrumentation Test
我正在嘗試使用Mockito
在 Android 中使用Kotlin
運行Instrumentation
測試(androidTest)
。
'org.mockito:mockito-core:3.3.3'
Kotlin
編寫的,所以我有org.mockito:mockito-inline:3.3.3
來修復最終的 class 問題。Android
中運行模擬,所以我的依賴項中也有org.mockito:mockito-android:3.3.3
。但是在編譯代碼時出現錯誤
找到多個文件,其獨立於操作系統的路徑為“mockito-extensions/org.mockito.plugins.MockMaker”
這是重現該問題的示例測試代碼。 將此測試作為儀器測試運行。 (androidTest)
。
主活動測試
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)
}
}
應用程序/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
?
注意:如果您認為這是一個重復的問題,我已經在整個互聯網上進行了搜索。 類似的堆棧跟蹤還有其他問題,但我的情況不同。
任何幫助將不勝感激
我有同樣的問題,我的解決方案是這個答案https://stackoverflow.com/a/41594789/2286422 , - 即使用 dexmaker 庫,它依賴於 Mockito。
刪除其他 Mockito androidTestImplementation
依賴項,只添加以下一項(您可以在此處找到最新版本):
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito:2.28.1'
請注意,此解決方案僅適用於具有 Android P 或更高版本( 官方文檔)的設備。
然后在實際測試中,我通過以下方式使用 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()
}
}
我使用了這些依賴項,它們為我工作
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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.