jam jam - 1 year ago 63
Java Question

How to test all combinations of bad parameters?

Let's assume a simple method which throws an


for an invalid pair of indices (2d array).

How would I test, that the exception is thrown for all combinations of bad
indices ?

(of course this test will not continue if
one call throws an exception)

@Test(expected = Exception.class)
public void validateIndices(){
check(0,COLS + 1);
check(ROWS + 1, 0);

Is there a common way to test
variations of parameters for a method ?


In your case, I would Parameterized my unit test to test all combinations with the same test more info here.

In your case it would look like this:

public class MyTest {
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
            { 0, -1 }, { -1, 0 }

    public int row;

    @Parameterized.Parameter(value = 1)
    public int col;

    @Test(expected = IndexOutOfBoundsException.class)
    public void validateIndices(){
        check(row, col);