替换spring-boot-starter-parent后集成测试无法运行的问题
解决移除Spring Boot Starter Parent后集成测试无法启动的问题
当移除spring-boot-starter-parent作为父POM后,丢失了它默认提供的Maven插件预设配置,这是导致集成测试无法启动Spring Boot上下文的核心原因。以下是具体修复方案:
1. 手动添加Spring Boot Maven插件
spring-boot-starter-parent默认绑定了spring-boot-maven-plugin的start/stop目标到集成测试生命周期阶段,负责启动/停止Spring Boot应用。你需要在POM中手动配置该插件:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.7.14</version> <executions> <execution> <id>pre-integration-test</id> <goals> <goal>start</goal> </goals> <phase>pre-integration-test</phase> </execution> <execution> <id>post-integration-test</id> <goals> <goal>stop</goal> </goals> <phase>post-integration-test</phase> </execution> </executions> </plugin> </plugins> </build>
2. 配置Maven Failsafe插件
集成测试由maven-failsafe-plugin执行(区别于单元测试用的maven-surefire-plugin),spring-boot-starter-parent默认对其做了适配,现在需要手动配置以识别你的集成测试类:
<build> <plugins> <!-- 保留上述spring-boot-maven-plugin配置 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.0.0-M7</version> <!-- 适配JUnit 5的版本 --> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> <configuration> <!-- 配置集成测试类识别规则,可根据你的命名习惯调整 --> <includes> <include>**/*IT.java</include> <include>**/*IntegrationTest.java</include> </includes> <!-- 适配JUnit 5执行环境 --> <providerRef>junit-platform</providerRef> </configuration> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>1.9.3</version> </dependency> </dependencies> </plugin> </plugins> </build>
3. 确保Assemblage Profile兼容
如果你的assemblage profile有自定义构建配置,需确保上述插件配置在该profile中生效:
- 若仅需在
assemblageprofile启用集成测试,可将插件配置包裹在profile节点内; - 若希望全局生效,直接放在根
build节点下即可。
示例profile内配置:
<profiles> <profile> <id>assemblage</id> <build> <plugins> <!-- 放入上述两个插件的完整配置 --> </plugins> </build> </profile> </profiles>
验证修复
执行以下命令测试效果:
mvn integration-test -Passemblage
内容的提问来源于stack exchange,提问作者Marc Le Bihan
相关产品推荐
相关产品推荐

