【问题标题】:Test class can only have one constructor error while running JUnit 5 tests运行 JUnit 5 测试时,测试类只能有一个构造函数错误
【发布时间】: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?

标签: kotlin junit junit5


【解决方案1】:

你可以看看one of the JUnit 5 samples,它使用了 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"
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    相关资源
    最近更新 更多