#kotlin #junit #junit5
#kotlin #junit #junit5
Вопрос:
Кто-нибудь может объяснить, почему я получаю эту ошибку при выполнении приведенного ниже кода? получите ту же ошибку и с другими аннотациями (например, @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, а также прикрепленный скриншот ошибки. у меня нет ссылки на junit 4 в build.gradle, почему он выдает ошибку для 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"
Комментарии:
1. Обратите внимание, что вы используете Junit4, а не Junit5! Взгляните на трассировку стека.
2. Я полагаю
AndroidJUnit4ClassRunner
, что имеет какую-то другую зависимость (возможно, ‘androidx.test: rules: 1.3.0’ или ‘androidx.test.ext: junit: 1.1.1’). Я не вижу никаких упоминаний о доступности JUnit5 на платформе Android в официальных документах . Может быть, это пока невозможно, и лучший вариант — использовать JUnit4?
Ответ №1:
Вы можете взглянуть на один из примеров JUnit 5, в котором используются Gradle и Kotlin. Краткое описание соответствующего кода:
build.gradle.kts
dependencies {
// [...]
testImplementation(platform("org.junit:junit-bom:5.7.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
}
tasks.test {
useJUnitPlatform()
}
Calculator.kt
class Calculator {
fun add(a: Int, b: Int): Int = a b
}
CalculatorTests.kt
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
class CalculatorTests {
@ParameterizedTest(name = "{0} {1} = {2}")
@CsvSource(
"1, 2, 3",
"1, 100, 101"
)
fun add(first: Int, second: Int, expectedResult: Int) {
val calculator = Calculator()
assertEquals(expectedResult, calculator.add(first, second)) {
"$first $second should equal $expectedResult"
}
}
}