Question

For testing REST services I have built a base class 'RestTestBase' that fires up an embedded Jetty server:

class RestTestBase {
    protected static AnnotationConfigWebApplicationContext rootCtx;
    protected static AnnotationConfigWebApplicationContext webCtx;
    private static Server jettyServer;

    @BeforeClass
    public static void initSpringAndJetty() {
      if(jettyServer == null) {
        // init jetty and spring
      }
    }

    @AfterClass
    public static void shutdownJetty() {
      if(jettyServer!=null && jettyServer.isRunning()) {
        // shutdown jetty and stop spring contexts
      }
    }
}

When I run this in a single-threaded environment from the Eclipse IDE, all is fine. Jetty and Spring are initialized only once, then all my tests run, then jetty is shut down.

However, when I run it in a multithreaded environment with Maven Surefire, the Jetty server is reinitialized for every single test. Why is that?

This is my surefire configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <forkCount>0</forkCount>
                <reuseForks>true</reuseForks>
            </configuration>
        </plugin>
    </plugins>
</build>
Was it helpful?

Solution

OK, seems like this questions kind of misses the point, since the behaviour of @BeforeClass and @AfterClass is as expected.

I added a follow-up question to solve my real problem:

How can I initialize a Spring applicationContext just once for all tests

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top