SpringJUnit4ClassRunner - Reloads Context for every test even though same context used

853 Views Asked by At

This is the base class of our unit tests:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring/test-context.xml")
public abstract class BaseUnitTest {}

All unit tests extend this class.

When I run the tests locally in eclipse (using Run As > Unit Test) , the tests run in about 5 seconds since the same context is shared by all tests and it is loaded only once.

However, when I run them using the mvn test target it takes about 5 minutes. After looking at the logs, I see that the application context is being loaded for every test. It takes the same time (5 mins) when we run it on our Jenkins CI server.

Not sure what's going on. In spring docs, it states that the appContext should be reused even with maven, but thats not the case here.

Any help would be appreciated.

UPDATE : I ran mvn with debug flags on and I see that a new JVM is spawned for each test:

Forking command line: cmd.exe /X /C "java -Xverify:none -jar S:\git\picaxo21\picaxo\picaxoService\target\surefire\surefirebooter8169952914558366417.jar S:\git\picaxo21\picaxo\picaxoService\target\surefire\surefire8550033206398936560tmp S:\git\picaxo21\picaxo\picaxoService\target\surefire\surefire_05655453605766528120tmp"

Forking command line: cmd.exe /X /C "java -Xverify:none -jar S:\git\picaxo21\picaxo\picaxoService\target\surefire\surefirebooter4002024477779069323.jar S:\git\picaxo21\picaxo\picaxoService\target\surefire\surefire6735432532690834115tmp S:\git\picaxo21\picaxo\picaxoService\target\surefire\surefire_17783676008756503456tmp"

Forking command line: cmd.exe /X /C "java -Xverify:none -jar S:\git\picaxo21\picaxo\picaxoService\target\surefire\ 7874269889863176184.jar S:\git\picaxo21\picaxo\picaxoService\target\surefire\surefire2050758518148174678tmp S:\git\picaxo21\picaxo\picaxoService\target\surefire\surefire_27591156970671336255tmp"

I am using forkCount=1 and reuseForks=true so I am not sure why this is happening. any clues?

Parent POM :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    .....
</parent>

<groupId>...</groupId>
<artifactId>picaxo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>.....</name>
<description>Build All Modules</description>

<modules>
    <module>picaxoService</module>
</modules>

<scm>
    ....
</scm>

<properties>
    <pmd.include.tests>true</pmd.include.tests>
    <findbugs.plugin.version>3.0.0</findbugs.plugin.version>
    <fb.threshold>Low</fb.threshold>
    <fb.includeTests>true</fb.includeTests>
    <fb.effort>Max</fb.effort>
    <fb.failOnError>false</fb.failOnError>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <skipTests>true</skipTests>
</properties>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <testFailureIgnore>true</testFailureIgnore>
                <skipTests>${skipTests}</skipTests>
                <reuseForks>true</reuseForks>
                <forkCount>1</forkCount>
                <excludes>
                    <exclude>**/*IntegrationTest.java</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <phase>integration-test</phase>
                    <configuration>
                        <excludes>
                            <exclude>none</exclude>
                        </excludes>
                        <includes>
                            <include>**/*IntegrationTest.java</include>
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
</build>

1

There are 1 best solutions below

0
On

Found the problem. Apparently forkMode for surefire plugin was defaulting to pertest hence was spawning a new JVM for each test. Explicitly Setting it to once in the plugin configuration solved the issue.