I would go with something like that:
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import java.util.Arrays;
@RunWith(Parameterized.class)
public class Foo {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Parameter
public Class<?> input;
/**
* Test of isCompramised method, of class MyClass.
*/
@Test
public void testIsCompramised() {
this.expectedException.expect(IllegalArgumentException.class);
final MyClass instance = new MyClass();
instance.isCompramised(input);
}
@Parameters(name = "test for {0}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] { {null}, {Object.class} });
}
}
(or two test methods, one for null
and one for Object
)
EDIT: some complements (see comments)
The methods annotated with @Parameters
returns an Iterable
containing Object[]
. Each of those Object[]
is bound to a @Parameter
annotated field (using the value of @Parameter
as index [default: 0]).
JUnit Parameterized
runner will iterate over the @Parameters
data and for each array, set fields values and then run every tests in the class.
See also: Parameterized
javadoc