With JUnit you can use @RunWith(Parameterized.class)
to provide a set of parameters to pass to the test constructor and then run tests with each object.
I'm trying to move as much test logic as possible into data, but there are some tests that won't easily be converted into data-driven tests. Is there a way to use JUnit's Parameterized
runner to run some tests with parameters, and then also add non-data-driven tests that aren't run repeatedly for each test object construction?
My workaround for this was to create a single class and place the programmatic and data-driven tests in two separate sub-classes. A sub-class must be static for JUnit to run its tests. Here's a skeleton:
@RunWith(Enclosed.class) // needed for working well with Ant
public class MyClassTests {
public static class Programmatic {
@Test
public void myTest(){
// test something here
}
}
@RunWith(Parameterized.class)
public static class DataDriven {
@Parameters
public static Collection<Object[]> getParams() {
return Collections.emptyList();
}
private String data;
public DataDriven(String testName, String data){
this.data = data;
}
@Test
public void test() throws AnalyzeExceptionEN{
// test data string here
}
}
}