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?
This is old question but I had the same issue recently. It strikes me that none of the solutions seem to go for the most obvious workaround - calling the @BeforeClass method in the @Parameters method. The latter is static and executed only once - before any of the tests have been run. So, it is for all intents and purposes a @BeforeClass method even though it is not annotated as such. More details can be found here: http://feraldeveloper.blogspot.co.uk/2013/12/beforeclass-and-parametrized-junit-tests.html
I would use just plain old java static {..} initializer instead of @BeforeClass, e.g.:
Only drawback I know is that classes inherited from this won't be able to override static initializer, while @BeforeClass gives some freedom in this aspect;
JUnit creates a
Runner
for each item in the parameter list, aRunner
is what encapsulates the test method. So the@Parameters
will always get executed before the @BeforeClass.However, you can combine the @Parameterized with Assume. You always include all of the parameters in your list, whether or not you intend executing it. Then in the test method, add the
assumeTrue()
which tests against theinitializeThis
value.The output from this is:
However, this does not work with TestSuites. Given
This does not work:
The output is
Nor that:
The output is "parameter=1". So the static initializer not executed at all. I found the following workaround. Extend "Suite" and insert the static initializer there:
and use it in your test suite:
The output is
Now it works.