Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate JUnit 4 Parameterized test to JUnit 5 ParameterizedTest?

Tags:

junit5

I have a JUnit 4 test like following, I'm trying to upgrade JUnit to JUnit 5. I did some research about how to migrate JUnit 4 test to JUnit 5, but cannot find any useful information about how to migrate following case.

Anyone knows how to convert this test to 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));
  }
}
like image 876
Kenneth Avatar asked Sep 01 '25 16:09

Kenneth


1 Answers

Found a solution:

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));
     }
}
like image 86
Kenneth Avatar answered Sep 06 '25 04:09

Kenneth