Previously in JUnit4 you could do something like this:
@RunWith(Parameterized.class)
public class MyTest
{
private final int number;
public MyTest(int number) {
this.play = play;
}
@Test
public void testIsEven() {
assertEquals(true, number % 2 == 0);
}
@Test
public void testIsNotOdd() {
assertEquals(false, number % 2 != 0);
}
@Parameterized.Parameters
public static int[] data() {
return new int[] { 2, 4, 6 }
}
}
This would go trough the array, instantiate MyTest with each of the values and then run all of the tests on each of those instances. See the Parameterized docs for more details.
Now in JUnit5 things have changed, according to the new docs you'd have to write the same tests like this:
public class MyTest {
@ParameterizedTest
@MethodSource("data")
public void testIsEven(int number) {
assertEquals(true, number % 2 == 0);
}
@ParameterizedTest
@MethodSource("data")
public void testIsNotOdd(int number) {
assertEquals(false, number % 2 != 0);
}
public static int[] data() {
return new int[] { 2, 4, 6 }
}
}
You have to repeat the parameter and the data source for each individual test. Is there a way to do something similar as in JUnit4, where the parameterized tests work on instances of the class instantiated with the different parameters?