11

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:

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

    @BeforeClass
    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()
    @Parameterized.Parameters
    public static Collection<Object[]> configurations() throws InterruptedException {

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

    }

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

    @Test
    public void testA() {
        // Test  
    }
}

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

Corey Wu
  • 1,209
  • 1
  • 22
  • 39

5 Answers5

11

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.

NamshubWriter
  • 23,549
  • 2
  • 41
  • 59
  • Okay thanks, do you have any suggestions on how to implement the functionality that I am looking for? – Corey Wu Mar 14 '14 at 15:49
  • @CoreyWu could you let us know what exact problem you are trying to solve? – NamshubWriter Mar 22 '14 at 22:11
  • I have this problem, too. I'm trying to do database prep before I set up my tests. My @Parameters method queries the database to find test values. I'd like to set up the database before getting my test values. No, this is not strictly unit testing.... – bobanahalf May 18 '15 at 18:07
  • @bobanahalf You could try setting up the database in your `@Parameters` method, but if it fails I'm not sure if JUnit will make the root cause of the error obvious. Personally I wouldn't use `Parameterized` if my parameters were from a data source that could fail or was slow to query. – NamshubWriter May 19 '15 at 14:40
  • @NamshubWriter, do you know another way to dynamically discover test data? We're running our tests over different databases. Even if it was all one database, I wouldn't really be able to count on today's data being the same as next month's. For now, I've got my discovery code in a static initializer. I'm sure there are undesirable side-affects if my initializer doesn't behave as I hope. – bobanahalf May 28 '15 at 18:34
  • @bebanahalf I don't know of another way to dynamically discover data. I recommend never calling code that could fail in a static initializer. – NamshubWriter May 29 '15 at 05:24
9

Although beeing a bit different solution, a static block does the trick. Also note, that it must be in the Test1.class. But beside of that it works ;-)

@RunWith(Parameterized.class)
public class Test1 {

    static{
        System.out.println("Executed before everything");
    }

    private String value;

    // @Parameterized function which appears to run before @BeforeClass setup()
    @Parameterized.Parameters
    public static Collection<Object[]> configurations() throws InterruptedException {
        // Code which relies on setup() to be run first
    }

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

    @Test
    public void testA() {
        // Test  
    }
}
Community
  • 1
  • 1
Lonzak
  • 9,334
  • 5
  • 57
  • 88
5

Recently ran into similar issue and solved problem using Function. Example below.

@RunWith(Parameterized.class)
public class MyClassTest {

    @Parameterized.Parameters
    public static Iterable functions() {
        return Arrays.<Object, Object>asList(
            param -> new Object()
        );
    }

    Object param;
    Function function;

    public MyClassTest(Function f) {
        this.function = f;
    }

    @Before
    public void before() {
        // construct dependency
        param = "some value";
    }

    @Test
    public void test() {
        assertThat(myClass.doSomething(function.apply(param)), is(equalTo(expectedValue)));
    }
}

In your scenario, do database setup in @Before or @BeforeClass then inject into function

Derek
  • 51
  • 1
  • 2
0

I found a hack to force a code segment to run before all other methods that are annotated with @Parameterized.Parameters. Just create a parameterized dummy test as follows:

@RunWith(Parameterized.class)
public class DummyInitTest
{
  @Parameterized.Parameters
  public static Collection<?> constructorFeeder()
  {
    // Your setup here. This will run before anything else.
    // Return empty list so no tests will be executed for this test class.
    return ImmutableList.of();
  }
}

Then in your test suite, add this test first:

@RunWith(Suite.class)
@SuiteClasses({ DummyInitTest.class, Test1.class, Test2.class })
public class TestSuite {
  // ...
}
Liran Funaro
  • 2,750
  • 2
  • 22
  • 33
0

Hoping this can help someone, I simply did it this way:

    //@BeforeClass does not run before Parameters annotation
    public static void beforeClassSetup() throws IOException {
       InputStream is = Test.class.getResourceAsStream("/TestData.json");
       // load data...
    }
       
    @Parameters(name = "testProductFunctionality")
    public static Collection<Object[]> data() throws IOException {
        beforeClassSetup();
        // create parameters...
     }

    @Test
    public void testProductFunctionality() {
    //...
Martin P.
  • 654
  • 8
  • 18