JUnit tests pass in Eclipse but fail in Maven Surefire

前端 未结 17 1856
臣服心动
臣服心动 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:39

    This has helped me in troubleshooting my problem. I had a similar symptoms in that maven would fail however running junit tests runs fine.

    As it turns out my parent pom.xml contains the following definition:

        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.9</version>
          <configuration>
            <forkMode>pertest</forkMode>
            <argLine>-Xverify:none</argLine>
          </configuration>
        </plugin>
    

    And in my project I override it to remove the argLine:

        <plugin>
           <artifactId>maven-surefire-plugin</artifactId>
           <configuration>
                <forkMode>pertest</forkMode>
                <argLine combine.self="override"></argLine>
              </configuration>
        </plugin>
    

    Hopefully this will help someone in troubleshooting surefire plugin.

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

    In my case the reason was a bug in the code. The test relied on a certain order of elements in a HashSet, which turned out to be different when run either in Eclipse or in Maven Surefire.

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

    This doesn't exactly apply to your situation, but I had the same thing -- tests that would pass in Eclipse failed when the test goal from Maven was run.

    It turned out to be a test earlier in my suite, in a different package. This took me a week to solve!

    An earlier test was testing some Logback classes, and created a Logback context from a config file.

    The later test was testing a subclass of Spring's SimpleRestTemplate, and somehow, the earlier Logback context was held, with DEBUG on. This caused extra calls to be made in RestTemplate to log HttpStatus, etc.

    It's another thing to check if one ever gets into this situation. I fixed my problem by injecting some Mocks into my Logback test class, so that no real Logback contexts were created.

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

    [I am not sure that this is an answer to the original question, since the stacktrace here looks slightly different, but it may be useful to others.]

    You can get tests failing in Surefire when you are also running Cobertura (to get code coverage reports). This is because Cobertura requires proxies (to measure code use) and there is some kind of conflict between those and Spring proxies. This only occurs when Spring uses cglib2, which would be the case if, for example, you have proxy-target-class="true", or if you have an object that is being proxied that does not implement interfaces.

    The normal fix to this is to add an interface. So, for example, DAOs should be interfaces, implemented by a DAOImpl class. If you autowire on the interface, everything will work fine (because cglib2 is no longer required; a simpler JDK proxy to the interface can be used instead and Cobertura works fine with this).

    However, you cannot use interfaces with annotated controllers (you will get a runtime error when trying to use the controller in a servlet) - I do not have a solution for Cobertura + Spring tests that autowire controllers.

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

    You don't need to inject a DataSource in the JpaTransactionManager since the EntityManagerFactory already has a datasource. Try the following:

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
       <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    
    0 讨论(0)
  • 2020-11-29 17:44

    I had the same problem, and the solution for me was to allow Maven to handle all dependencies, including to local jars. I used Maven for online dependencies, and configured build path manually for local dependencies. Thus, Maven was not aware of the dependencies I configured manually.

    I used this solution to install the local jar dependencies into Maven:

    How to add local jar files in maven project?

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