【发布时间】:2020-10-17 01:34:38
【问题描述】:
谁能解释为什么我在运行以下代码时会出现此错误?使用其他注释也会出现相同的错误(例如 @MethodSource ,@CsvSource)
class TestRunningOnJUnit5 {
@ParameterizedTest
@CsvSource("test,TEST", "tEst,TEST", "Java,JAVA")
fun toUpperCase_ShouldGenerateTheExpectedUppercaseValue(
input: String,
expected: String?
) {
val actualValue = input.toUpperCase()
assertEquals(expected, actualValue)
}
}
这是我的 build.gradle 文件,还附上了错误截图。我在 build.gradle 中没有 junit 4 的参考,为什么它会为 AndridjUnit4ClassRunner
抛出错误implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0
implementation 'androidx.test.espresso:espresso-idling-resource:3.2.0'
// Android Testing Support Libraries's runner and rules
androidTestUtil 'androidx.test:orchestrator:1.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'
// Espresso UI Testing
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
// Android Instrumentation Tests wth JUnit 5
testImplementation "org.junit.jupiter:junit-jupiter-api:5.7.0"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.7.0"
testImplementation "org.junit.jupiter:junit-jupiter-params:5.7.0"
androidTestImplementation "org.junit.jupiter:junit-jupiter-api:5.7.0"
androidTestImplementation "org.junit.jupiter:junit-jupiter-engine:5.7.0"
androidTestImplementation "org.junit.jupiter:junit-jupiter-params:5.7.0"
androidTestImplementation "de.mannodermaus.junit5:android-test-core:1.2.0"
androidTestRuntimeOnly "de.mannodermaus.junit5:android-test-runner:1.2.0"
【问题讨论】:
-
请注意,您使用的是 Junit4,而不是 Junit5!看看你的堆栈跟踪。
-
我相信
AndroidJUnit4ClassRunner带有其他一些依赖项(可能是 'androidx.test:rules:1.3.0' 或 'androidx.test.ext:junit:1.1.1' )。我在official docs 中没有看到任何关于 Android 平台上 JUnit5 可用性的提及。也许现在还不可能,最好的选择是使用 JUnit4?