如何结合 JUnit4 @RunWith(Parameterized.class) 和 JUnit5 @ParameterizedTest

How to combine JUnit4 @RunWith(Parameterized.class) and JUnit5 @ParameterizedTest

我正在尝试将来自 JUnit4 的参数化运行程序的概念与 JUnit5 参数化测试相结合。本质上,我想在同一组数据上测试两个独立的函数。

我知道我可以将函数作为另一个参数添加到参数化测试本身,但我正在尝试更改或添加新函数以方便测试。

我可以利用嵌套测试 类 来实现吗?我不确定最好的方法。

@RunWith(Parameterized::class)
class RomanNumeralTest(val func: (Int) -> String) {

    @ParameterizedTest(name = "{index} - Expect [{0}] should return [{1}]")
    @MethodSource("testData")
    fun `Test roman numeral from integer values`(num: Int, expected: String) =
            assertEquals(expected, func(num))

    companion object {

        @JvmStatic
        @Parameterized.Parameters
        fun data(): Collection<Array<(Int) -> String>> {
            return listOf(
                    arrayOf({num -> roman(num)}),
                    arrayOf({num -> num.toRomanNumeral()})
            )
        }

        @JvmStatic
        private fun testData() = sequenceOf(
                arrayOf(1, "I"),
                arrayOf(2, "II"),
                arrayOf(3, "III"),
                arrayOf(4, "IV"),
                arrayOf(5, "V")
        ).asStream()
    }
}

I tried the same but in the end I came to the conclusion that: you can't.

您无法使用 JUnit 4 Parameterized 测试运行器使用 JUnit 5 附带的注释。

您需要迁移到 JUnit 5 才能使用 TestFactory 等最新功能或 here

中描述的参数化测试的所有强大注释