In JUnit5, we can replace @Test with @ParameterizedTest, so that test cases can use different parameters and be executed several times in a loop.
@ValueSource
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
@DisplayName("this is demo 1") @ParameterizedTest @ValueSource(ints = { 1, 2, 3 }) publicvoidtest_Demo1(int arg){ int expected = 2; assertEquals(expected, arg, "You are not such 2"); }
@DisplayName("this is demo 2") @ParameterizedTest(name = "{index} ==> the testcase is running") @ValueSource(strings = {"1", "2", "3,4"}) publicvoidtest_Demo2(String arg){ assertNull(arg, "Can NOT be NULL");
String expected = "2"; assertEquals(expected, arg, "You are not such 2"); }
In @ValueSource, the parameter name can be strings, ints, etc. However, I personally prefer to use strings, because the String type can be implicitly converted to many other types which makes passing arguments more flexible.