I would like to make a data-driven parametrized method in JUnit.
The examples that I see, parametrize the whole class.
E.g.
@RunWith(Parameterized.class)
public class PrimeNumberCheckerTest {
However, I would like to parametrize a single test method, not the whole class.
In TestNG, something like that seems to look like this:
@DataProvider(name = "test1")
public static Object[][] primeNumbers() {
return new Object[][] {{2, true}, {6, false}, {19, true}, {22, false}, {23, true}};
}
// This test will run 4 times since we have 5 parameters defined
@Test(dataProvider = "test1")
public void testPrimeNumberChecker(Integer inputNumber, Boolean expectedResult) {
System.out.println(inputNumber + " " + expectedResult);
Assert.assertEquals(expectedResult,
primeNumberChecker.validate(inputNumber));
}
Is such a thing possible in JUnit?
EDIT:
The most elegant/DRY way would be to have a single method-level annotation - which is what I'm hoping for.
I've found a library:
https://github.com/Pragmatists/junitparams
Example:
@RunWith(JUnitParamsRunner.class)
public class PersonTest {
@Test
@Parameters({"17, false",
"22, true" })
public void personIsAdult(int age, boolean valid) throws Exception {
assertThat(new Person(age).isAdult(), is(valid));
}
}
The question still remains - is there something like that built into JUnit?
If not, then what is the closest built-in thing that would require the least amount of boiler-plate code or class-level contortions?