Search code examples
javajunit4

JUnit 4.11 -> 4.12 introduced @Rule bug, doesn't executing tests


I switched from JUnit 4.11 to 4.12. Now I have a bug in the @Rule annotated method execution. My rule method looks like this:

@Rule
public WireMockRule testServer() {
    // ... other stuff gets created ...
    return new WireMockRule(
            wireMockConfig().extensions(...parameterized-extensions...).port(PORT), true);
}

Now, after starting, the rule method gets executed (fine), no test gets started (not fine), but immediately the testServer() method gets executed again, without finishing the first one. So the test fails because the port got already bind and is still bound.

No problems in 4.11 with that. No configuration setup to run my tests in parallel. I should mention that my Test-Class is parametrized with @RunWith(Parameterized.class), but was executing in-order in 4.11.


Solution

  • I found the cause of the error and a simple fix. Just saw it with downloading the source-code of WireMockRule and debugging it. It turns out that in JUnit 4.11 the testServer() method gets called once per parameter before the rule gets the apply(...) call to run the tests, in 4.12 it gets called always twice before the rule gets the apply(...) call to run the tests. So the solution was simple:

    private WireMockRule testServer;
    
    @Rule
    public WireMockRule testServer() {
        if (testServer != null) {
            return testServer;
        }
        // ... other stuff gets created ...
        testServer = new WireMockRule(
            wireMockConfig().extensions(...parameterized-extensions...).port(PORT), true);
        return testServer;
    }