0

i am currently working on a Project with Tests in JUnit 4 and i should migrate them to JUnit 5. Now i have the Problem that i have a Parameterized test in JUnit 4. Lets say my test looks like this:

@RunWith(Parameterized.class)
public class FibonacciTest {
 
  @Parameters 
  public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] {
      {0,0},{1,1},{2,1},{3,2} })};
 
  private int input, expected;
 
  public FibonacciTest(int input, int expected) {
    this.input = input; this.expected = expected;
  }
 
  @Test
  public void test() {
    assertEquals(expected, Fibonacci.compute(input));
  }
}

I search already alot and also tried with a Stream Stream<Arguments> data() and than i added my params with Arguments.of(1,1) But Nothing really works for me.

Can i write JUnit4 tests like this in JUnit5 without changing the whole class?

javaTopics
  • 11
  • 3

1 Answers1

0

It should work with @MethodSource and @ParameterizedTest like this:

class FibonacciTest {

     @MethodSource("data")
     @ParameterizedTest
     void testSomething(int input, int expected) {
         assertEquals(expected, Fibonacci.compute(input));
     }

    public static Stream<Arguments> data() {
        return Stream.of(
                Arguments.of(0, 0),
                Arguments.of(1, 1),
                Arguments.of(2, 1),
                Arguments.of(3, 2)
        );
    }
}
Willem
  • 992
  • 6
  • 13