小编Fai*_*led的帖子

使用纯 Kotlin 函数作为 Junit5 方法源

我很好奇在 Kotlin 中,在 Junit5 的参数化测试中,我是否可以使用类外的方法作为 @MethodSource。

我知道在 Kotlin 中使用 @MethodSource 的 2 种方法 - 伴随对象和 @TestInstance(TestInstance.Lifecycle.PER_CLASS)。我想知道是否可以通过不同的方式来完成,例如通过在类之外声明一个方法并使用一些注释?我试图这样做,但它不起作用,我想知道是否可以做类似的事情。

class GenerationStatusTest {

    @ParameterizedTest
    @MethodSource("provideStatusesToTest")
    internal fun shouldStatusesHaveExpectedAbilities(generationStatus: GenerationStatus, assertions:(GenerationStatus)->Unit) {
        assertions(generationStatus)
    }
}

fun provideStatusesToTest(): Stream<Arguments> {
    return Stream.of(
            Arguments.of(WAITING, canDoNothing),
            Arguments.of(PROCESSING, canDoNothing)
    )
}
Run Code Online (Sandbox Code Playgroud)
org.junit.platform.commons.JUnitException: Could not find factory method [provideStatusesToTest] in class [com.test.enums.GenerationStatusTest]
    at org.junit.jupiter.params.provider.MethodArgumentsProvider.lambda$getMethod$4(MethodArgumentsProvider.java:83)
Run Code Online (Sandbox Code Playgroud)

kotlin junit5 parametrized-testing

7
推荐指数
2
解决办法
2564
查看次数

标签 统计

junit5 ×1

kotlin ×1

parametrized-testing ×1