java - NoClassDefFoundError : NestedIOException when running tests with Spring Boot 3. 0.0-M4/Spring 6.0.0-M5

标签 java spring-boot maven spring-framework-beans milestone

我有一个使用 Spring Boot 的 Java-17 项目,我正在尝试对其进行模块化。为了充分利用模块化,我将其移植到 Spring Boot 3 里程碑 4 和 Spring 6 里程碑 5。

该项目由我的 Maven 管理。我能够编译该项目。但是,它无法执行 Spring Boot 生成的标准 contextLoads() 测试。具体错误信息为:

[main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Could not load default TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes available.
java.lang.NoClassDefFoundError: jakarta/servlet/ServletContext

使用不同的 TestExecutionListener 实现(即 TransactionalTestExecutionListener 和 SqlScriptsTestExecutionListener),此错误消息会重复两次,但 Spring 最终会加载多个 TestExecutionListener 并尝试执行测试。但是,测试失败:

java.lang.NoClassDefFoundError: org/springframework/beans/factory/aot/BeanFactoryInitializationAotProcessor
    [...series of java.base frames...]
    at <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90e3e0e2f9fef7bef3fffee4f5e8e4d0a6bea0bea0bddda5" rel="noreferrer noopener nofollow">[email protected]</a>/org.springframework.context.annotation.AnnotationConfigUtils.registerAnnotationConfigProcessors(AnnotationConfigUtils.java:165)
    at <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddaeadafb4b3baf3beb2b3a9b8a5a99debf3edf3edf090e8" rel="noreferrer noopener nofollow">[email protected]</a>/org.springframework.context.annotation.AnnotationConfigUtils.registerAnnotationConfigProcessors(AnnotationConfigUtils.java:138)
    at <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5d6d5d7cccbc28bc6cacbd1c0ddd1e5938b958b9588e890" rel="noreferrer noopener nofollow">[email protected]</a>/org.springframework.context.annotation.AnnotatedBeanDefinitionReader.<init>(AnnotatedBeanDefinitionReader.java:88)
    at <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="631013110a0d044d000c0d17061b1723554d534d534e2e56" rel="noreferrer noopener nofollow">[email protected]</a>/org.springframework.context.annotation.AnnotatedBeanDefinitionReader.<init>(AnnotatedBeanDefinitionReader.java:71)
    at <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d2e2d2f34333a733e3233293825291d6b736d736d701068" rel="noreferrer noopener nofollow">[email protected]</a>/org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:69)
    at <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="394a494b50575e175b56564d790a1709170914740d" rel="noreferrer noopener nofollow">[email protected]</a>/org.springframework.boot.ApplicationContextFactory.lambda$static$0(ApplicationContextFactory.java:55)
    at org.springframework.boot.test.context.SpringBootContextLoader.lambda$loadContext$1(SpringBootContextLoader.java:120)
    at <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="354645475c5b521b575a5a4175061b051b05187801" rel="noreferrer noopener nofollow">[email protected]</a>/org.springframework.boot.SpringApplication.createApplicationContext(SpringApplication.java:566)
    at <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d7e7d7f64636a236f6262794d3e233d233d204039" rel="noreferrer noopener nofollow">[email protected]</a>/org.springframework.boot.SpringApplication.run(SpringApplication.java:309)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:132)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:123)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:248)
    at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$8(ClassBasedTestDescriptor.java:363)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:368)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$9(ClassBasedTestDescriptor.java:363)
    [...many more frames, mostly from junit and surefire...]
Caused by: java.lang.ClassNotFoundException: org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)

这个问题可能是由我的 module-info.java 中的一些错误引起的。我使用的 Spring 模块是:

    requires spring.beans;
    requires spring.context;
    requires spring.boot.autoconfigure;
    requires spring.core;
    requires spring.boot;
    requires spring.oxm;
    requires spring.web;
    requires spring.ws.core;
    [...]
    opens [my module] to spring.core;
    exports [my module] to spring.beans, spring.context;

我错过了什么?该应用程序可以在 Spring 5.3/Spring Boot 2.7 上编译并正常工作,但我需要对其进行模块化以便能够创建安装程序。

最佳答案

该问题似乎与 org.springframework:spring-beans:5.3.25 有关,它尚未包含所需的类。无意中,我们在迁移到 Spring 3.0 时将此版本固定在 build.gradle 中。这应该不再是必要的了。

因此,您可以尝试从 build.gradlepom.xml 中删除 spring-beans,具体取决于您是否使用 Gradle 或梅文。这应该已经足够了。如果没有,您可以尝试 gradle dependencymvn dependency:tree (已安装相关插件)来查明是否/在何处拉入旧版本。或者,您可以在 build.gradlepom.xml 中明确包含最新版本。

关于java - NoClassDefFoundError : NestedIOException when running tests with Spring Boot 3. 0.0-M4/Spring 6.0.0-M5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73947252/

相关文章:

java - 您将 pom.xml 文件上传到哪里?

java - SpringBoot 1.5.x + 安全性 + OAuth2

java - 如何排除父pom对子pom的依赖?

java - 设置 Spring Archetype 时重复出现的问题

java - Joshua Bloch 在 Effective Java 中解释的枚举类型

java - 具有多个服务器的 JPA

java - 从对象创建实例的通用方法

spring-boot - 将Spring Boot应用程序连接到Docker容器中的Oracle数据库12.2.0.1

java - LazyInitializationException 使用带有 @Basic(fetch = FetchType.LAZY) 的 hibernate 增强

java - Maven 依赖与 javadocs