JUnit tests pass in Eclipse but fail in Maven Surefire

前端 未结 17 1853
臣服心动
臣服心动 2020-11-29 17:28

I have written some JUnit tests using JUnit 4 and spring-test libraries. When I run the tests inside Eclipse then run fine and pass. But when I run them using Maven (during

相关标签:
17条回答
  • 2020-11-29 17:55

    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

    0 讨论(0)
  • 2020-11-29 17:57

    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.7.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.

    0 讨论(0)
  • 2020-11-29 18:00

    I have the similar problem, but with IntelliJ IDEA + Maven + TestNG + spring-test. (spring-test is essential of course :) ) It was fixed when I've change config of maven-surefire-plugin to disable run tests in parallel. Like this:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.9</version>
        <configuration>
            <skipTests>${maven.test.skip}</skipTests>
            <trimStackTrace>false</trimStackTrace>
            <!--<parallel>methods</parallel>-->
            <!-- to skip integration tests -->
            <excludes>
                <exclude>**/IT*Test.java</exclude>
                <exclude>**/integration/*Test.java</exclude>
            </excludes>
        </configuration>
        <executions>
            <execution>
                <id>integration-test</id>
                <phase>integration-test</phase>
                <goals>
                    <goal>test</goal>
                </goals>
                <configuration>
                    <skipTests>${maven.integration-test.skip}</skipTests>
                    <!-- Make sure to include this part, since otherwise it is excluding Integration tests -->
                    <excludes>
                        <exclude>none</exclude>
                    </excludes>
                    <includes>
                        <include>**/IT*Test.java</include>
                        <include>**/integration/*Test.java</include>
                    </includes>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
    0 讨论(0)
  • 2020-11-29 18:01

    I had a similar problem: JUnit tests failed in Maven Surefire but passed in Eclipse when I used JUnit library version 4.11.0 from SpringSource Bundle Repository. Particulary:

    <dependency>
        <groupId>org.junit</groupId>
        <artifactId>com.springsource.org.junit</artifactId>
        <version>4.11.0</version>
    </dependency>
    

    Then I replaced it with following JUnit library version 4.11 and everything works fine.

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-29 18:06

    I had this problem today testing a method that converted an object that contained a Map to a JSON string. I assume Eclipse and the Maven surefire plugin were using different JREs which had different implementations of HashMap ordering or something, which caused the tests run through Eclipse to pass and the tests run through surefire to fail (assertEquals failed). The easiest solution was to use an implementation of Map that had reliable ordering.

    0 讨论(0)
提交回复
热议问题