Corey Wu Corey Wu - 1 year ago 115
Java Question

JUnit @Parameterized function is run before @BeforeClass in a test suite?

I am using a JUnit test suite to run a few tests, one of which is run multiple times using @Parameterized. I am finding that when I run my tests, the @Parameterized function is run before @BeforeClass. Is this expected behavior or is something else happening? I would have expected that @BeforeClass would run before any of the tests are started.

Here is my test suite:

@SuiteClasses({ Test1.class, Test2.class })
public class TestSuite {

public static void setup() throws Exception {
// setup, I want this to be run before anything else


Test1 uses @Parameterized:

public class Test1 {

private String value;

// @Parameterized function which appears to run before @BeforeClass setup()
public static Collection<Object[]> configurations() throws InterruptedException {

// Code which relies on setup() to be run first


public Test1(String value) {
this.value = value;

public void testA() {
// Test

How can I fix this to run the @BeforeClass setup() function before running anything else?

Answer Source

This is, unfortunately, working as intended. JUnit needs to enumerate all of the test cases before starting the test, and for parameterized tests, the method annotated with @Parameterized.Parameters is used to determine how many tests there are.