Spring Boot测试终极指南:从环境搭建到分层测试实战
掌握MockMvc与分层测试策略,让你的代码质量提升一个维度
一、环境搭建:Maven依赖深度解析
Spring Boot测试的核心依赖在pom.xml中配置如下:
<dependencies>
<!-- 核心测试库:包含JUnit 5、Mockito、AssertJ等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- MockMvc独立测试支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<!-- AssertJ流式断言库 -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
</dependency>
</dependencies>
关键依赖详解:
- spring-boot-starter-test:测试核心包,包含:
- JUnit Jupiter(JUnit 5测试引擎)
- Mockito(模拟依赖对象)
- JSONPath(JSON数据解析)
- AssertJ(流式断言)
- spring-test:提供MockMvc等Spring测试工具类
- assertj-core:增强版断言库,支持链式调用
为什么选择JUnit 5而不是JUnit 4?
JUnit 5的模块化架构(Jupiter+Platform+Vintage)支持Lambda表达式、参数化测试等新特性,且与Spring Boot 2.2+深度集成:// JUnit 5示例(支持Lambda) @Test @DisplayName("测试商品价格计算") void testPriceCalculation() { assertAll( () -> assertThat(calculator.calculate(10)).isEqualTo(100), () -> assertThatThrownBy(() -> calculator.calculate(-1)) .isInstanceOf(IllegalArgumentException.class) ); }
二、核心概念深度剖析
1. 应用上下文(Application Context)
Spring容器核心,管理Bean的生命周期和依赖注入:
@SpringBootTest
public class ContextLoadTest {
@Autowired
private ApplicationContext ctx; // 注入应用上下文
@Test
void testBeanExistence() {
assertThat(ctx.containsBean("productService")).isTrue();
}
}
通过@SpringBootTest
加载完整上下文,适合集成测试
2. 断言(Assertions)
验证代码行为的检查点,分为:
- 基础断言:验证true/false、相等性等
// JUnit基础断言 assertEquals(expected, actual);
- 流式断言(AssertJ):提升可读性
// AssertJ链式断言 assertThat(product) .hasFieldOrProperty("name") .hasFieldOrPropertyWithValue("price", 99.9) .extracting(Product::getCategory) .isEqualTo("电子产品");
AssertJ的错误信息更直观,支持集合、异常等复杂验证
三、MockMvc全解:Controller层隔离测试
1. 核心组件
- MockMvc:模拟HTTP请求的工具
- standaloneSetup:独立构建控制器,不加载完整上下文
适用于纯Controller逻辑测试,避免加载无关BeanmockMvc = MockMvcBuilders.standaloneSetup(productController).build();
2. HTTP请求模拟链
// 测试商品查询接口
mockMvc.perform(get("/api/products") // ① 模拟GET请求
.param("category", "电子") // 添加查询参数
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()) // ② 验证HTTP状态码
.andExpect(jsonPath("$[0].id").value(1001)) // ③ JSONPath验证
.andDo(print()); // ④ 打印请求详情
- perform():发起HTTP请求(支持GET/POST/PUT/DELETE)
- andExpect():验证响应结果(状态码、Header、Body)
- jsonPath():使用JSONPath语法定位JSON字段
// 验证返回的JSON数组中第一个元素的name字段 .andExpect(jsonPath("$[0].name").value("华为手机"))
- andDo():执行附加操作(如打印日志、保存结果)
3. 请求体处理
// 测试新增商品
ProductQuery query = new ProductQuery("手机", 1000, 2000); // ① 构建查询对象
String json = objectMapper.writeValueAsString(query); // ② 转为JSON
mockMvc.perform(post("/api/products")
.content(json)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated());
ProductQuery
是参数封装对象,用于接收查询条件
四、分层测试策略:Controller vs Service
1. 为什么分层测试?
测试类型 | 测试目标 | 使用注解 | 特点 |
---|---|---|---|
Controller | HTTP接口逻辑 | @WebMvcTest |
模拟HTTP请求,不启动完整服务 |
Service | 业务逻辑正确性 | @SpringBootTest |
加载完整上下文,测试真实逻辑 |
Repository | 数据访问层 | @DataJpaTest |
使用内存数据库 |
分层测试实现关注点分离,避免测试复杂度爆炸
2. Controller层测试示例
@WebMvcTest(ProductController.class) // 只加载Controller相关Bean
public class ProductControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ProductService productService; // 模拟Service
@Test
void testGetProduct() throws Exception {
// 模拟Service返回
when(productService.findById(1001))
.thenReturn(new Product(1001, "测试商品"));
mockMvc.perform(get("/products/1001"))
.andExpect(jsonPath("$.name").value("测试商品"));
}
}
3. Service层测试示例
@SpringBootTest
public class ProductServiceTest {
@Autowired
private ProductService service; // 真实Service
@MockBean
private ProductRepository repo; // 模拟Repository
@Test
void testCreateProduct() {
Product product = new Product("新品");
when(repo.save(any())).thenReturn(product);
Product created = service.create(product);
assertThat(created.getName()).isEqualTo("新品");
}
}
五、高级技巧:数据准备与验证增强
1. @Sql注解:测试数据初始化
@Test
@Sql(scripts = "/init-products.sql", // ① 初始化脚本
config = @SqlConfig(transactionMode = ISOLATED))
@Sql(scripts = "/cleanup.sql", // ② 清理脚本
executionPhase = AFTER_TEST_METHOD)
public void testProductCount() {
int count = service.countProducts();
assertThat(count).isEqualTo(10);
}
- 脚本路径:
src/test/resources/init-products.sql
- 执行阶段:
BEFORE_TEST_METHOD
(默认)或AFTER_TEST_METHOD
2. AssertJ集合断言
List<Product> products = service.search("手机");
assertThat(products)
.hasSize(3)
.extracting(Product::getPrice)
.allMatch(price -> price > 1000);
六、测试架构最佳实践
1. 分层测试金字塔
- 底层测试(Service/Repository)数量最多
- 顶层测试(Controller)覆盖关键接口
2. 测试数据管理策略
方式 | 适用场景 | 示例 |
---|---|---|
内存数据库 | Repository层测试 | H2 + @DataJpaTest |
@Sql初始化 | 固定数据场景 | @Sql("/init-data.sql") |
Mockito动态生成 | 无需持久化的数据 | when(repo.findAll()).thenReturn(list) |
七、Demo 测试类(完整版)
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringBootTest
@AutoConfigureMockMvc
@Sql(scripts = "/test-data.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) // 测试前执行SQL初始化数据[8](@ref)
@Sql(scripts = "/clean-data.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 测试后清理数据[8](@ref)
public class ProductControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ProductService productService; // 模拟Service层
// ========================= 增删改查测试 =========================
@Test
void testCreateProduct() throws Exception {
String jsonBody = "{\"name\":\"MacBook Pro\",\"price\":12999}";
mockMvc.perform(MockMvcRequestBuilders.post("/products")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonBody))
.andExpect(status().isCreated()) // 断言HTTP 201
.andExpect(jsonPath("$.id").exists()) // 验证返回的JSON有id字段[4](@ref)
.andDo(print()); // 打印请求/响应详情[7](@ref)
}
@Test
void testGetProductById() throws Exception {
// 模拟Service返回数据
when(productService.findById(1001L))
.thenReturn(new Product(1001L, "iPhone 15", 7999));
mockMvc.perform(MockMvcRequestBuilders.get("/products/1001"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("iPhone 15")) // JSONPath验证字段值[1](@ref)
.andExpect(jsonPath("$.price").value(7999));
}
@Test
void testBatchCreateProducts() throws Exception {
String jsonArray = """
[
{"name":"iPad Air", "price":4499},
{"name":"Apple Watch", "price":2999}
]
""";
mockMvc.perform(MockMvcRequestBuilders.post("/products/batch")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonArray))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.length()").value(2)); // 验证返回数组长度[4](@ref)
}
// ========================= 文件上传测试 =========================
@Test
void testUploadProductList() throws Exception {
// 构建CSV模拟文件[9,11](@ref)
String csvContent = "id,name,price\n101,Keyboard,199\n102,Mouse,99";
MockMultipartFile file = new MockMultipartFile(
"file", // 参数名(必须与@RequestParam一致)
"products.csv", // 文件名
"text/csv", // 文件类型
csvContent.getBytes() // 文件内容
);
mockMvc.perform(MockMvcRequestBuilders.multipart("/products/upload")
.file(file)
.param("source", "excel")) // 附加普通参数
.andExpect(status().isOk())
.andExpect(content().string("2 records imported"));
}
// ========================= 删除测试 =========================
@Test
void testDeleteProduct() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.delete("/products/1001"))
.andExpect(status().isNoContent()); // 204状态码[1](@ref)
}
}
总结
通过MockMvc实现Controller层隔离测试,配合分层策略和AssertJ断言,可构建高效的测试体系。关键实践:
- 使用
@WebMvcTest + MockMvc
测试Controller,不启动Web服务器 - Service层用
@SpringBootTest + @MockBean
进行集成测试 - 利用JSONPath高效验证复杂JSON响应
- 通过
@Sql
管理测试数据生命周期
避坑指南:避免在Controller测试中加载完整上下文(
@SpringBootTest
),否则会导致测试速度下降10倍以上