I want to use parameterized JUnit tests on a play! framework (1.2.5) application.
This is my very simple test example:
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import play.test.FunctionalTest;
@RunWith(Parameterized.class)
public class ParameterizedExampleTest extends FunctionalTest {
private int i;
@Parameters
public static List<Object[]> parameters() {
return Arrays.asList(new Object[][] {{1},{2},{3}});
}
public ParameterizedExampleTest(int i) {
this.i = i;
}
@Test
public void someTest() {
System.out.println("i is " + i);
}
}
When I run the test, I get an IllegalArgumentException telling me "Test class can only have one constructor". I perfectly agree with that as FunctionalTest extends BaseTest which has a @RunWith(PlayJUnitRunner.class) annotation and the PlayJUnitRunner has a constructor.
Any help welcome!
I found a rather nice solution:
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import play.test.FunctionalTest;
@RunWith(Parameterized.class)
public class ParameterizedExampleTest extends FunctionalTest {
@Parameter(0)
public int i;
@Parameters
public static List<Object[]> parameters() {
return Arrays.asList(new Object[][] {{1},{2},{3}});
}
@Test
public void someTest() {
System.out.println("i is " + i);
}
}
You have to mark the parameters with the @Parameter(...) Annotation and the number of the parameter in the parameters array. No constructor is needed hence it runs smoothly with play.
Drawback: You will need JUnit 4.11 as this feature is not implemented in 4.10 which is what play (1.2.5) comes with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With