如何将 JUnit 4 Parameterized 测试迁移到 JUnit 5 ParameterizedTest?
How to migrate JUnit 4 Parameterized test to JUnit 5 ParameterizedTest?
我有一个如下所示的 JUnit 4 测试,我正在尝试将 JUnit 升级到 JUnit 5。我做了一些关于如何将 JUnit 4 测试迁移到 JUnit 5 的研究,但找不到任何关于如何迁移的有用信息以下案例。
有人知道如何将此测试转换为 JUnit 5 吗?
@RunWith(Parameterized.class)
public class FibonacciTest {
@Parameters
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
}
@Parameter(0)
public int fInput;
@Parameter(1)
public int fExpected;
@Test
public void test() {
assertEquals(fExpected, Fibonacci.compute(fInput));
}
}
找到解决方案:
public class FibonacciTest {
public static Stream<Arguments> data() {
return Stream.of(
Arguments.arguments( 0, 0 ),
Arguments.arguments( 1, 1 ),
Arguments.arguments( 2, 1 ),
Arguments.arguments( 3, 2 ),
Arguments.arguments( 4, 3 ),
Arguments.arguments( 5, 5 ),
Arguments.arguments( 6, 8 )
);
}
@ParameterizedTest
@MethodSource("data")
public void test(int fInput, int fExpected) {
assertEquals(fExpected, Fibonacci.compute(fInput));
}
}
我有一个如下所示的 JUnit 4 测试,我正在尝试将 JUnit 升级到 JUnit 5。我做了一些关于如何将 JUnit 4 测试迁移到 JUnit 5 的研究,但找不到任何关于如何迁移的有用信息以下案例。
有人知道如何将此测试转换为 JUnit 5 吗?
@RunWith(Parameterized.class)
public class FibonacciTest {
@Parameters
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
}
@Parameter(0)
public int fInput;
@Parameter(1)
public int fExpected;
@Test
public void test() {
assertEquals(fExpected, Fibonacci.compute(fInput));
}
}
找到解决方案:
public class FibonacciTest {
public static Stream<Arguments> data() {
return Stream.of(
Arguments.arguments( 0, 0 ),
Arguments.arguments( 1, 1 ),
Arguments.arguments( 2, 1 ),
Arguments.arguments( 3, 2 ),
Arguments.arguments( 4, 3 ),
Arguments.arguments( 5, 5 ),
Arguments.arguments( 6, 8 )
);
}
@ParameterizedTest
@MethodSource("data")
public void test(int fInput, int fExpected) {
assertEquals(fExpected, Fibonacci.compute(fInput));
}
}