我正在为 Spring 应用程序编写一些 JUnit,所使用的 Spring 版本是
<spring.version>3.2.8.RELEASE</spring.version>
依赖关系定义为
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
然后在我的 JUnit
测试中我使用注释作为
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:config/test-application-context-datasource.xml")
当我尝试执行测试用例时,它无法初始化,即它甚至没有进入 @Before
方法并抛出以下异常:
java.lang.NoClassDefFoundError: org/springframework/core/type/AnnotatedTypeMetadata
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:118)
我相信 AnnotatedTypeMetadata 仅在 spring 版本 4.10.0 之后可用,但由于我没有在应用程序中的任何地方使用它,并且我的 spring-test 和 spring-core 的版本是相同的,为什么这甚至被引用。
有什么想法吗?
最佳答案
您的类路径上显然有各种 Spring JAR 的不兼容版本。
要确定类路径中存在哪些不兼容版本(即 3.2.8.RELEASE
除外),请在项目文件夹中执行 mvn dependency:tree
。
然后确保避免在类路径上出现不兼容的版本 - 例如,通过将排除添加到 Maven POM 中,以防止将不需要的版本作为传递依赖项拉入的任何依赖项。
关于java - 带 spring-test 的 Spring Jar 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27311142/