I'm using "parametrized" feature of junit 4 and I noticed that @parameters method is executed before @beforeclass method. This is creating a problem for me because the parameters i'm passing to the test cases via @parameters depends on the code initialize in the @beforeclass method. For example
@RunWith(Parameterized.class)
public class TestOtherClass {
String argument;
private static boolean initializeThis;
public TestOtherClass(String parameter) throws Exception {
argument=parameter;
}
@BeforeClass
public static void doSetup() {
System.out.println("Doing setup before class...");
initializeThis=true; // true or false, based on some condition
}
@Test
public void otherTest() {
System.out.println("Other test: " + argument);
}
@Parameters
public static Collection<Object[]> getData(){
System.out.println("Inside parameter");
String addThis;
if(initializeThis)
addThis="adding true";
else
addThis="adding false";
Object[] para1 = new Object[]{"First parameter :: " + addThis};
Object[] para2 = new Object[]{"Second parameter :: " + addThis};
Collection<Object[]> classNames = new ArrayList<Object[]>();
classNames.add(para1);
classNames.add(para2);
return classNames;
}
}
Now, I'm initializing variable "initializeThis" to true in @beforeclass method but (surprisingly) when I executed the test case it prints
Other test: First parameter :: adding false
Other test: Second parameter :: adding false
That is something not expected.
My question is; is there any way to execute the @beforeclass method before @parameters, can we do this is in junit 4?