If you're using JUnit 4.0+, you could use parameterized tests.
- Test class with annotation
@RunWith(Parameterized.class)
- Static method with annotation
@Parameters
(this returns a nested array of params) - Tests in the class will be run once for each set of params
Example from the wiki link above:
@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 }, { 4, 3 }, { 5, 5 },{ 6, 8 }
});
}
private int fInput;
private int fExpected;
public FibonacciTest(int input, int expected) {
fInput= input;
fExpected= expected;
}
@Test
public void test() {
assertEquals(fExpected, Fibonacci.compute(fInput));
}
}