You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

最新Spring Boot中@QuarkusIntegrationTest的等价方案是什么?

Spring Boot 独立进程集成测试方案(等价于Quarkus @QuarkusIntegrationTest)

在最新版Spring Boot中,要实现类似Quarkus的@QuarkusIntegrationTest效果——即以独立Java进程启动服务并运行集成测试,核心是通过Spring Boot Maven插件配合Failsafe插件完成,具体步骤如下:

1. 配置Maven插件

在pom.xml中添加Failsafe插件配置,并绑定Spring Boot插件的启动/停止目标到集成测试生命周期:

<build>
    <plugins>
        <!-- Spring Boot Maven插件:用于启动/停止独立应用进程 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>pre-integration-test</id>
                    <goals>
                        <goal>start</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-integration-test</id>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <!-- Failsafe插件:用于执行集成测试(默认识别**IT结尾的测试类**) -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.2.5</version> <!-- 使用最新稳定版 -->
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2. 编写集成测试类

创建以IT结尾的测试类(Failsafe自动扫描这类文件),测试逻辑通过HTTP客户端调用独立运行的服务接口,无需启动Spring上下文:

import org.junit.jupiter.api.Test;
import org.springframework.web.client.RestTemplate;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DemoApplicationIT {

    private static final String BASE_URL = "http://localhost:8080";

    @Test
    public void testHelloEndpoint() {
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(BASE_URL + "/hello", String.class);
        assertEquals("Hello World!", response);
    }
}

3. 执行集成测试

在终端运行以下Maven命令,会自动完成:启动Spring Boot应用 → 运行所有集成测试 → 停止应用:

mvn verify

方案等价性说明

该方案和Quarkus的@QuarkusIntegrationTest核心逻辑一致:

  • 启动独立的应用进程(而非内嵌在测试上下文)
  • 测试代码与应用进程完全隔离,更贴近生产环境
  • 测试完成后自动终止应用进程

内容的提问来源于stack exchange,提问作者Guido

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 01:52:08