You can't do it the way you listed because the test classes need to have a no-arg constructor.
There are 2 options you can do depending on what the tests are like:
Option 1. Make abstract test classes with subclasses that have the parameters:
Make the abstract test classes with all of your tests then have subclasses provide the variable information. The abstract class can take parameters in the constructors, the subclass no-arg constructor calls super(...)
with the appropriate parameters.
public abstract class AbstractCommentingTest{
private Account account;
private ViewMode mode;
public AbstractCommentingTest(Account a, ViewMode v){
this.account=a;
this.viewMode = v;
}
//Put your tests here using the given account and view
@Test
public void foo(){
}
@Test
public void bar(){
}
}
Then your concrete classes
public class AdminViewACommentingTest extends AbstractCommentingTest{
//no-arg constructor for JUnit
public AdminViewACommentingTest(){
super(Accounts.ADMIN, Viewmode.A);
}
}
This works but can get out of hand quickly if there are lots of options
Option 2: Use Junit Parameterized tests to have every option combination:
I assume Accounts and ViewMode are enums ? If so you can easily use the values()
method to create all possible combinations as part of a Parameterized test set.
@RunWith(Parameterized.class)
public class CommentingTest{
@Parameters
public static Collection<Object[]> createData(){
List<Object[]> data = new ArrayList<Object[]>();
for(Accounts account : Accounts.values()){
for(ViewMode view : ViewMode.values()){
data.put(new Object[]{account, view});
}
}
return data;
}
private Account account;
private ViewMode mode;
public CommentingTest(Account a, ViewMode v){
this.account=a;
this.viewMode = v;
}
//Put your tests here using the given account and view
@Test
public void foo(){
}
@Test
public void bar(){
}
}