Android Studio中无法运行单元测试 求助排查Gradle配置问题
单元测试执行失败排查:JUnit引擎缺失问题
问题概述
在Android Studio中运行单元测试时始终失败,相同测试代码在新项目可正常执行,怀疑当前项目Gradle配置存在问题。
当前Gradle依赖配置
dependencies { implementation project(':base') implementation "androidx.compose.ui:ui:$compose" implementation "androidx.activity:activity-compose:1.4.0" implementation "androidx.compose.material:material:$compose" implementation "androidx.compose.ui:ui-tooling:$compose" implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.6.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3' implementation 'androidx.navigation:navigation-ui-ktx:2.4.2' implementation "com.google.code.gson:gson:$gson_version" implementation "androidx.room:room-ktx:$room_version" implementation "androidx.swiperefreshlayout:swiperefreshlayout:$swipe_refresh" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:$coroutines_version" implementation "androidx.paging:paging-runtime-ktx:$paging_version" implementation "androidx.fragment:fragment-ktx:1.5.5" implementation "com.google.dagger:hilt-android:2.44.2" kapt "androidx.hilt:hilt-compiler:1.0.0" kapt "com.google.dagger:hilt-android-compiler:2.44.2" kapt "androidx.room:room-compiler:$room_version" implementation "io.coil-kt:coil:$coil_version" implementation "io.coil-kt:coil-compose:$coil_version" // Architectural Components implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1" implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.5.1" // Coroutines implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4" // Coroutine Lifecycle Scopes implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1" implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.1" // ktor implementation "io.ktor:ktor-client-okhttp:1.6.5" implementation "io.ktor:ktor-client-json:1.6.5" implementation "io.ktor:ktor-client-logging-jvm:1.6.5" implementation "io.ktor:ktor-client-serialization-jvm:1.6.5" implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2" implementation "androidx.activity:activity-ktx:1.5.1" implementation "androidx.core:core-splashscreen:$splash_version" testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.4' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' }
测试代码
import org.junit.Assert import org.junit.Test class ExampleUnitTest { @Test fun addition_isCorrect() { Assert.assertEquals(4, 2 + 2) } }
执行失败输出
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:testDebugUnitTest'. > There were failing tests. See the report at: file:///Users/develop/CineView4/app/build/reports/tests/testDebugUnitTest/index.html * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/7.0.2/userguide/command_line_interface.html#sec:command_line_warnings BUILD FAILED in 1s
核心错误信息
org.gradle.api.internal.tasks.testing.TestSuiteExecutionException: Could not complete execution for Gradle Test Executor 5. ...(省略非核心栈帧) Caused by: org.junit.platform.commons.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath at org.junit.platform.commons.util.Preconditions.condition(Preconditions.java:296) at org.junit.platform.launcher.core.DefaultLauncher.<init>(DefaultLauncher.java:48) ...(省略后续栈帧)
解决方案
原因分析
核心错误表明JUnit Platform无法找到可用的TestEngine。当前项目仅依赖了JUnit 4的核心库,但Gradle默认使用JUnit Platform(JUnit 5的测试运行器)执行测试,而JUnit 4需要通过junit-vintage-engine才能在JUnit Platform上运行。
修复方案(二选一)
- 添加JUnit Vintage Engine依赖
在dependencies块中添加TestEngine依赖:
testImplementation 'org.junit.vintage:junit-vintage-engine:5.9.2'
版本可根据项目使用的JUnit Platform版本调整,确保与Gradle内置的JUnit Platform版本兼容。
- 指定Gradle使用JUnit 4运行测试
在模块的build.gradle中添加测试配置,强制使用JUnit 4运行器:
test { useJUnit() }
额外建议
清理依赖中的重复项,例如项目中重复声明了androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1,移除重复项可减少潜在冲突。
内容的提问来源于stack exchange,提问作者Nicote Ool
相关产品推荐
相关产品推荐

