2017 update: JUnit 5 will include parameterized tests through the junit-jupiter-params
extension. Some examples from the documentation:
Single parameter of primitive types (@ValueSource
):
@ParameterizedTest
@ValueSource(strings = { "Hello", "World" })
void testWithStringParameter(String argument) {
assertNotNull(argument);
}
Comma-separated values (@CsvSource
) allows specifying multiple parameters similar to JUnitParams below:
@ParameterizedTest
@CsvSource({ "foo, 1", "bar, 2", "'baz, qux', 3" })
void testWithCsvSource(String first, int second) {
assertNotNull(first);
assertNotEquals(0, second);
}
Other source annotations include @EnumSource
, @MethodSource
, @ArgumentsSource
and @CsvFileSource
, see the documentation for details.
Original answer:
JUnitParams (https://github.com/Pragmatists/JUnitParams) seems like a decent alternative. It allows you to specify test parameters as strings, like this:
@RunWith(JUnitParamsRunner.class)
public class MyTestSuite {
@Test
@Parameters({"1,2", "3,4"})
public testAdd(int fValue1, int fValue2) {
...
}
}
You can also specify parameters through separate methods, classes or files, consult the JUnitParamsRunner api docs for details.