java.lang.NoSuchMethodError: 'void org.springframework.util.Assert.state(boolean, java.util.function.Supplier)'

zaw*_*udo 3 java junit spring unit-testing maven

我尝试将项目从 JDK8 升级到 JDK11,整个业务逻辑部分运行良好,但是当 Maven 到达测试类时,所有这些都失败并出现完全相同的异常\n例如

\n\n
 initializationError(com.foo.bar.etc.utils.TimeUtilsTest)  Time elapsed: 0 sec  <<< ERROR!\njava.lang.NoSuchMethodError: 'void org.springframework.util.Assert.state(boolean, java.util.function.Supplier)'\n        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.ensureSpringRulesAreNotPresent(SpringJUnit4ClassRunner.java:120)\n        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:141)\n        at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)\n        at jdk.internal.reflect.GeneratedConstructorAccessor6.newInstance(Unknown Source)\n        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)\n        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)\n        at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)\n        at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)\n        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)\n        at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)\n        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)\n        at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)\n        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)\n        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)\n        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)\n        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)\n        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)\n        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)\n        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)\n
Run Code Online (Sandbox Code Playgroud)\n\n

一旦一切完成,它只是用列表总结所有测试类

\n\n
Tests in error:\n  BaseSpringTest.initializationError \xc2\xbb NoSuchMethod 'void org.springframework.ut...\n  EmailAttachmentDaoImplTest.initializationError \xc2\xbb NoSuchMethod 'void org.spring...\n  EmailEventDaoImplTest.initializationError \xc2\xbb NoSuchMethod 'void org.springframe...\n  EmailMappingDaoImplTest.initializationError \xc2\xbb NoSuchMethod 'void org.springfra...\n  EmailRequestDaoImplTest.initializationError \xc2\xbb NoSuchMethod 'void org.springfra...\n  EmailWindowsDaoImplTest.initializationError \xc2\xbb NoSuchMethod 'void org.springfra...\n  ServiceClientDaoImplTest.initializationError \xc2\xbb NoSuchMethod 'void org.springfr...\n  TemplateContextDaoImplTest.initializationError \xc2\xbb NoSuchMethod 'void org.spring...\n  TemplateMappingDaoImplTest.initializationError \xc2\xbb NoSuchMethod 'void org.spring...\n  TemplateMetadataDaoImplTest.initializationError \xc2\xbb NoSuchMethod 'void org.sprin...\n  VarMetadataDaoImplTest.initializationError \xc2\xbb NoSuchMethod 'void org.springfram...\n  TemplateContextStorageTest.initializationError \xc2\xbb NoSuchMethod 'void org.spring...\n  ApiRequestFilterTest.initializationError \xc2\xbb NoSuchMethod 'void org.springframew...\n  TemplateResolutionServiceTest.initializationError \xc2\xbb NoSuchMethod 'void org.spr...\n  AwsS3ClientTest.initializationError \xc2\xbb NoSuchMethod 'void org.springframework.u...\n  TimeUtilsTest.initializationError \xc2\xbb NoSuchMethod 'void org.springframework.uti...\n
Run Code Online (Sandbox Code Playgroud)\n\n

会不会是某个版本的问题?我知道它需要添加一些 jar,或者将 JDK 之外的其他东西升级到版本 11

\n\n

顺便说一句,测试在 JDK8 上运行良好,我认为这是一个重要的细节。

\n

zaw*_*udo 5

将 spring-core 从版本 4 移动到版本 5 后问题得到解决。出于某种未知的原因,除了 spring-core 之外,所有 spring jar 都是版本 5,更奇怪的是它是一个 spring boot 应用程序,所以整个 spring 包jars 是版本 5(除了 spring-core)。