JUnit tests pass in Eclipse but fail in Maven Surefire JUnit tests pass in Eclipse but fail in Maven Surefire java java

JUnit tests pass in Eclipse but fail in Maven Surefire


I had the same problem (JUnit tests failed in Maven Surefire but passed in Eclipse) and managed to solve it by setting forkMode to always in the maven surefire configuration in pom.xml:

<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-surefire-plugin</artifactId>    <version>2.12</version>    <configuration>        <forkMode>always</forkMode>    </configuration></plugin>

Surefire parameters: http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html

Edit (January 2014):

As Peter Perháč pointed out, the forkMode parameter is deprecated since Surefire 2.14. Beginning from Surefire 2.14 use this instead:

<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-surefire-plugin</artifactId>    <version>2.16</version>    <configuration>        <reuseForks>false</reuseForks>        <forkCount>1</forkCount>    </configuration></plugin>

For more information see Fork Options and Parallel Test Execution


I suddenly experienced this error, and the solution for me was to disable to run tests in parallel.

Your milage may vary, since I could lower number of failing tests by configuring surefire to run parallel tests by ´classes´.:

            <plugin>                <artifactId>maven-surefire-plugin</artifactId>                <version>2.22.2</version>                <configuration>                    <parallel>classes</parallel>                    <threadCount>10</threadCount>                </configuration>            </plugin>

As I wrote first, this was not enough for my test suite, so I completely disabled parallel by removing the <configuration> section.


I had a similar problem, the annotation @Autowired in the test code did not work under using the Maven command line while it worked fine in Eclipse. I just update my JUnit version from 4.4 to 4.9 and the problem was solved.

<dependency>    <groupId>junit</groupId>    <artifactId>junit</artifactId>    <version>4.13.1</version></dependency>