解决mvn打包报There are test failures.的问题

报错内容

我的spring-boot项目,使用mvn启动时是OK的,但是用mvn打包时失败。报错:
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 13.047 s <<< FAILURE! - in com.amwalle.walle.WalleApplicationTests
[ERROR] contextLoads(com.amwalle.walle.WalleApplicationTests) Time elapsed: 0.216 s <<< ERROR!
java.lang.NoSuchMethodError: 'org.springframework.core.annotation.AnnotationAttributes org.springframework.core.annotation.AnnotatedElementUtils.getAnnotationAttributes(java.lang.reflect.AnnotatedElement, java.lang.String)'

详细报错内容如下:

2020-01-19 15:58:08.395  WARN 2236 --- [           main] o.s.test.context.TestContextManager      : Caught exception while invoking 'beforeTestMethod' callback on TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@50378a4] for test method [public void com.amwalle.walle.WalleApplicationTests.contextLoads()] and test instance [com.amwalle.walle.WalleApplicationTests@68a305eb]

java.lang.NoSuchMethodError: 'org.springframework.core.annotation.AnnotationAttributes org.springframework.core.annotation.AnnotatedElementUtils.getAnnotationAttributes(java.lang.reflect.AnnotatedElement, java.lang.String)'
	at org.springframework.transaction.annotation.SpringTransactionAnnotationParser.parseTransactionAnnotation(SpringTransactionAnnotationParser.java:42)
	at org.springframework.transaction.annotation.AnnotationTransactionAttributeSource.determineTransactionAttribute(AnnotationTransactionAttributeSource.java:154)
	at org.springframework.transaction.annotation.AnnotationTransactionAttributeSource.findTransactionAttribute(AnnotationTransactionAttributeSource.java:133)
	at org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource.computeTransactionAttribute(AbstractFallbackTransactionAttributeSource.java:149)
	at org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource.getTransactionAttribute(AbstractFallbackTransactionAttributeSource.java:100)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:169)
	at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:291)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365)
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273)
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159)
	at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:379)
	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:340)
	at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125)
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:413)

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 13.047 s <<< FAILURE! - in com.amwalle.walle.WalleApplicationTests
[ERROR] contextLoads(com.amwalle.walle.WalleApplicationTests)  Time elapsed: 0.216 s  <<< ERROR!
java.lang.NoSuchMethodError: 'org.springframework.core.annotation.AnnotationAttributes org.springframework.core.annotation.AnnotatedElementUtils.getAnnotationAttributes(java.lang.reflect.AnnotatedElement, java.lang.String)'

2020-01-19 15:58:08.710  INFO 2236 --- [       Thread-3] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@4bc28c33: startup date [Sun Jan 19 15:58:00 GMT+08:00 2020]; root of context hierarchy
2020-01-19 15:58:08.716  INFO 2236 --- [       Thread-3] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 2147483647
2020-01-19 15:58:08.716  INFO 2236 --- [       Thread-3] org.quartz.core.QuartzScheduler          : Scheduler schedulerFactoryBean_$_NON_CLUSTERED paused.
2020-01-19 15:58:08.719  INFO 2236 --- [       Thread-3] o.s.s.quartz.SchedulerFactoryBean        : Shutting down Quartz Scheduler
2020-01-19 15:58:08.719  INFO 2236 --- [       Thread-3] org.quartz.core.QuartzScheduler          : Scheduler schedulerFactoryBean_$_NON_CLUSTERED shutting down.
2020-01-19 15:58:08.719  INFO 2236 --- [       Thread-3] org.quartz.core.QuartzScheduler          : Scheduler schedulerFactoryBean_$_NON_CLUSTERED paused.
2020-01-19 15:58:08.720  INFO 2236 --- [       Thread-3] org.quartz.core.QuartzScheduler          : Scheduler schedulerFactoryBean_$_NON_CLUSTERED shutdown complete.
[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   WalleApplicationTests.contextLoads » NoSuchMethod 'org.springframework.core.an...
[INFO] 
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  34.854 s
[INFO] Finished at: 2020-01-19T15:58:09+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.21.0:test (default-test) on project walle: There are test failures.

解决办法(1)

跳过打包时的测试出错

方法一

在执行打包命令时加入参数:-Dmaven.test.failure.ignore=true

mvn package -Dmaven.test.failure.ignore=true

参考链接

Maven has its own arguments to deal with this issue:
-Dmaven.test.failure.ignore=true ignores any failure that occurs during test execution
-Dmaven.test.skip=true would not compile the tests
-fn, -fae never fails the build regardless of test results

方法二

在项目的pom.xml文件中加入如下配置:

<build>
  <plugins>
     <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-surefire-pluginartifactId>
        <configuration>
        <testFailureIgnore>truetestFailureIgnore> configuration>
    plugin>
 plugins>
build>

解决办法(2)

出现如上报错,在我的项目中,根因是我应用的spring frame work包版本冲突。所以需要将冲突的包版本改为一致。如果有类似问题的,那么就需要仔细检查spring相关包版本,看是否有版本不一致的包。如果有将其改为一致版本

可以看到,我的项目中,spring-tx 的版本是 4.1.6.RELEASE,但是其他包的版本都是 5.0.9.RELEASE。于是我在pom.xml文件中,将spring-tx的版本也修改为 5.0.9.RELEASE,从而解决了该问题。
解决mvn打包报There are test failures.的问题_第1张图片

你可能感兴趣的:(Tools,java,开发FAQ)