gpt4 book ai didi

android - Espresso 测试无法与 Junit5 和 multidex 一起运行

转载 作者:行者123 更新时间:2023-12-04 15:25:46 26 4
gpt4 key购买 nike

我们的 espresso 测试不再运行,而它们曾经使用 Junit5。它们在从 Android Studio 4 运行时起作用,但在使用 ./gradlew app:connectedDebugAndroidTest

从 gradle CLI 运行时不起作用

他们目前失败了:

> Task :app:connectedDebugAndroidTest
Starting 3 tests on Pixel_3a(AVD) - 10

com.bonial.tests.BrochureViewerShelfTrackingTest > initializationError[Pixel_3a(AVD) - 10] FAILED
java.lang.NoSuchMethodError: No static method newKeySet()Lj$/util/concurrent/ConcurrentHashMap$KeySetView; in class Lj$/util/concurrent/ConcurrentHashMap; or its super classes (declaration of 'j$.util.concurrent.ConcurrentHashMap' appears in /data/app/com.bonial.kaufda.test-pEhGL5P8v0MAVR6s2R8e4A==/base.apk!classes4.dex)
at org.junit.platform.commons.logging.LoggerFactory.<clinit>(LoggerFactory.java:36)

com.xxx.tests.OpenBrochureTrackingTest > initializationError[Pixel_3a(AVD) - 10] FAILED
java.lang.IllegalStateException: junit-platform-runner not found on runtime classpath of instrumentation tests; please review your androidTest dependencies or raise an issue.
at de.mannodermaus.junit5.AndroidJUnit5Builder.runnerForClass(RunnerBuilder.kt:72)

com.xxx.tests.OpenBrochureWithOnboardingTrackingTest > initializationError[Pixel_3a(AVD) - 10] FAILED
java.lang.IllegalStateException: junit-platform-runner not found on runtime classpath of instrumentation tests; please review your androidTest dependencies or raise an issue.
at de.mannodermaus.junit5.AndroidJUnit5Builder.runnerForClass(RunnerBuilder.kt:72)

我检查了 classes4.dex 是的,那个方法不在那里,但它在另一个 classes.dex 文件中。

这可能是由于:

  • 迁移到 AGP 4.0
  • 启用 java 8 脱糖
  • 测试 apk 越来越大,因此 dex 文件拆分方式不同

这是我们的设置:

app/build.gradle:

android {
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = "1.8"
[...]
}

defaultConfig {
testInstrumentationRunner "com.bonial.framework.utils.CustomEspressoTestRunner"
testInstrumentationRunnerArgument "runnerBuilder", "de.mannodermaus.junit5.AndroidJUnit5Builder"
[...]
}
[...]
}

dependencies {
[...]
// Espresso
androidTestImplementation "androidx.multidex:multidex:${versions.multidex}"
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
androidTestImplementation "androidx.test:runner:${versions.supportTestVersion}"
androidTestImplementation "androidx.test:rules:${versions.supportTestVersion}"
androidTestImplementation("com.schibsted.spain:barista:${versions.barista}") {
exclude group: 'org.jetbrains.kotlin'
}
androidTestImplementation "com.beust:klaxon:${versions.klaxon}"
androidTestImplementation "org.yaml:snakeyaml:${versions.snakeyaml}"
androidTestImplementation "androidx.test.espresso:espresso-core:${versions.espresso}"
implementation "androidx.test.espresso:espresso-idling-resource:${versions.espresso}"
// To be able to honor espresso-contrib dependencies, our support library dependencies must match espresso's
androidTestImplementation("androidx.test.espresso:espresso-contrib:${versions.espresso}") {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'appcompat-v7'
exclude group: 'com.android.support', module: 'design'
exclude group: 'com.google.code.findbugs', module: 'jsr305'
exclude module: 'recyclerview-v7'
}
androidTestImplementation("org.junit.platform:junit-platform-suite-api:${versions.junitPlatform}")
androidTestImplementation("org.junit.platform:junit-platform-runner:${versions.junitPlatform}")
androidTestImplementation("org.junit.jupiter:junit-jupiter:${versions.junit5}")
androidTestImplementation "org.junit.jupiter:junit-jupiter-api:${versions.junit5}"
androidTestImplementation "de.mannodermaus.junit5:android-test-core:${versions.junit5AndroidTest}"
androidTestRuntimeOnly "de.mannodermaus.junit5:android-test-runner:${versions.junit5AndroidTest}"
debugImplementation "androidx.fragment:fragment-testing:${versions.fragment}"
androidTestImplementation "com.linkedin.dexmaker:dexmaker-mockito-inline:${versions.dexmaker}"
androidTestImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:${versions.mockitoKotlin}"
androidTestImplementation "org.amshove.kluent:kluent-android:${versions.kluent}"
androidTestImplementation "com.jayway.jsonpath:json-path:${versions.jsonPath}"
androidTestImplementation "com.squareup.rx.idler:rx2-idler:${versions.rxIdler}"
androidTestImplementation "androidx.core:core:${versions.coreKtx}"

}

CustomEspressoTestRunner.kt:

@Suppress("unused")
class CustomEspressoTestRunner : AndroidJUnitRunner() {

override fun onCreate(arguments: Bundle?) {
MultiDex.install(getTargetContext())
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().permitAll().build())
super.onCreate(arguments)
}

@Throws(InstantiationException::class, IllegalAccessException::class, ClassNotFoundException::class)
override fun newApplication(cl: ClassLoader?, className: String?, context: Context?): Application? {
return super.newApplication(cl, EspressoTestApplication::class.java.name, context)
}
}

最佳答案

解决方案是显式使用 kotlin 的 jdk8 stdlib:

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${versions.kotlin}"

关于android - Espresso 测试无法与 Junit5 和 multidex 一起运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62286106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com