Spring Boot 3.4.6集成测试依赖MBean Server失败,如何兼顾OpenAPI生成与无MBean Server的集成测试?
Spring Boot 3.4.6集成测试依赖MBean Server失败,如何兼顾OpenAPI生成与无MBean Server的集成测试?
看起来你刚好卡在了一个典型的构建流程冲突里:既要靠springdoc的Maven插件生成OpenAPI文档(必须启动应用访问Actuator端点),又要避免Spring Boot Maven Plugin启停应用时依赖MBean Server导致的连接拒绝问题。我来帮你梳理几个可行的解决思路,都是实际项目里验证过的:
问题核心复盘
先明确下你当前的困境:
- 执行
./mvnw verify时,Spring Boot Maven Plugin的start目标会尝试连接9001端口的MBean Server,结果抛出Connection refused错误 - 之所以要配置Spring Boot插件的
pre-integration-test/post-integration-test阶段,是为了给springdoc插件提供可访问的应用端点来生成openapi.json - 你试过禁用JMX但没生效——其实问题出在Spring Boot插件本身依赖JMX来管理应用生命周期,不是应用的JMX配置没生效
可行解决思路
思路1:调整Spring Boot Maven Plugin配置,绕过MBean Server依赖
这个方案改动最小,只需要给Spring Boot插件加几个参数,让它不用JMX也能完成应用启停和状态检测:
关键配置调整点
- 先确保你的集成测试Profile(
application-it.properties)里已经禁用应用级JMX:
spring.jmx.enabled=false spring.application.admin.enabled=false
- 修改Spring Boot Maven Plugin的配置,强制插件放弃JMX依赖:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <jvmArguments>-Dspring.profiles.active=it -Dspring.jmx.enabled=false -Dspring.application.admin.enabled=false</jvmArguments> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> <!-- 禁用插件的JMX端口,彻底摆脱MBean Server依赖 --> <jmxPort>-1</jmxPort> <!-- 用健康检查端点替代JMX,确认应用启动完成 --> <wait>true</wait> <waitTimeout>30000</waitTimeout> <waitUrl>http://localhost:8088/actuator/health</waitUrl> <!-- 保留你的image配置 --> <image> <builder>paketobuildpacks/builder-jammy-tiny</builder> <name>${docker.publishRegistry.url}/${project.name}:${project.version}</name> <createdDate>${maven.build.timestamp}</createdDate> <cleanCache>true</cleanCache> <env> <BP_NATIVE_IMAGE>false</BP_NATIVE_IMAGE> <BP_JVM_VERSION>${java.version}</BP_JVM_VERSION> <BP_OCI_SOURCE>${project.scm.url}</BP_OCI_SOURCE> <BP_OCI_CREATED>${maven.build.timestamp}</BP_OCI_CREATED> <BP_OCI_VERSION>${project.version}</BP_OCI_VERSION> <BP_OCI_DESCRIPTION>${project.description}</BP_OCI_DESCRIPTION> </env> </image> </configuration> <executions> <execution> <id>pre-integration-test</id> <goals> <goal>start</goal> </goals> </execution> <execution> <id>post-integration-test</id> <goals> <goal>stop</goal> </goals> <!-- 不用JMX调用shutdown,直接杀死进程 --> <configuration> <stopMethod>kill</stopMethod> </configuration> </execution> </executions> </plugin>
这个方案的核心是:用端口健康检查替代JMX的应用状态检测,用强制杀进程替代JMX的优雅停机,彻底切断插件对MBean Server的依赖。
思路2:彻底重构流程,分开处理OpenAPI生成与集成测试
如果你想完全摆脱Spring Boot插件启停应用的逻辑,改用测试框架管理应用生命周期,同时保留Failsafe Plugin跑集成测试,可以这么做:
步骤1:用Spring Boot Test生成OpenAPI文档
不用Maven插件启动应用,而是写一个简单的单元测试类,启动应用后直接调用springdoc的API生成openapi.json:
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.io.ResourceLoader; import org.springdoc.core.OpenAPIService; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; @SpringBootTest( properties = "spring.profiles.active=it", webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT ) public class OpenApiGeneratorTest { @Autowired private OpenAPIService openAPIService; @Autowired private ResourceLoader resourceLoader; @Autowired private ObjectMapper objectMapper; @Test void generateOpenApiJson() throws IOException { // 输出到项目target目录,和原Maven插件的输出路径保持一致 File outputFile = new File(resourceLoader.getResource("classpath:../../target").getFile(), "openapi.json"); objectMapper.writeValue(outputFile, openAPIService.getOpenAPI()); } }
步骤2:调整Maven配置,绑定测试执行与集成测试
移除Spring Boot插件的启停逻辑,用Surefire Plugin执行上面的OpenAPI生成测试,用Failsafe Plugin跑集成测试:
<!-- 执行OpenAPI生成的单元测试,绑定到process-test-classes阶段 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.5.3</version> <executions> <execution> <id>generate-openapi</id> <phase>process-test-classes</phase> <goals> <goal>test</goal> </goals> <configuration> <includes> <include>**/OpenApiGeneratorTest.java</include> </includes> <excludes> <exclude>**/*IT.java</exclude> </excludes> <argLine>-Dspring.profiles.active=it</argLine> </configuration> </execution> </executions> </plugin> <!-- 用Failsafe跑集成测试 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.5.3</version> <configuration> <argLine>-Dspring.profiles.active=it</argLine> <includes> <include>**/*IT.java</include> </includes> </configuration> <executions> <execution> <id>integration-tests</id> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> <!-- 可以移除springdoc的Maven插件了 -->
这个方案的好处是:所有应用启动逻辑都由Spring Boot Test管理,完全避开MBean Server的坑,同时OpenAPI生成和集成测试的生命周期完全解耦。
选择建议
- 如果你想最小化改动,优先试思路1,只需要调整几个Maven插件参数,不用新增代码
- 如果你本来就想优化集成测试流程,或者后续有更多自定义文档生成需求,思路2会更灵活,也更符合Spring Boot的测试生态
内容来源于stack exchange
相关产品推荐
相关产品推荐

