博客
关于我
SpringBoot单元测试详解
阅读量:587 次
发布时间:2019-03-11

本文共 3187 字,大约阅读时间需要 10 分钟。

诚然,下面是根据您的要求优化的文章内容:


一、Junit 测试

在需要编写简单测试时,可以直接编写 Junit 测试代码,适用于不需要 Spring Boot 功能的单元测试场景。

public class SimpleJunitTest {
@Test
public void testSayHi() {
System.out.println("Hi Junit.");
}
}

二、Spring Boot 测试

Spring Boot 提供了 spring-boot-starter-test 模块,用于执行应用程序的集成测试。使用 @SpringBootTest 注解可以自动识别主配置类,创建测试环境。

@SpringBootApplication
public class MysqlDataTestApplication {
}

如果无法自动识别主配置类,可以手动指定类路径:

@SpringBootTest(classes = {MysqlDataTestApplication.class})
public class MysqlDataTestApplicationTests {
@Autowired
private TOrderInfoService tOrderInfoService;
@Test
public void tOrderInfoServiceSelect() {
TOrderInfo tOrderInfo = tOrderInfoService.selectByPrimaryKey(1L);
Assertions.assertThat(tOrderInfo).isNotNull();
}
}

三、Spring Boot Web 测试

如果需要启动完整的 HTTP 服务器,可以使用 @SpringBootTest 注解并设置 webEnvironment 参数:

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class MysqlDataTestApplicationTests2 {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void test() throws Exception {
String response = restTemplate.getForObject("/query", String.class);
Assertions.assertThat(response).contains("QRC123456789012");
}
}

或者通过 @AutoConfigureMockMvc 假装无服务器环境执行测试:

@Autowired
private MockMvc mockMvc;
@Test
public void test() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/query"))
.andExpect(MockMvcResultMatchers.status().isOk());
}

四、单元测试

1. 单元测试与集成测试的区别

单元测试通常使用 @Test 注解,主要用于验证单个类的行为。如果需要构建完整的 Spring 上下文,可以选择单元测试或集成测试。

2. MyBatis Mapper 测试

使用 @MybatisTest 注解,可以将测试范围限定在 Mapper 层:

@MybatisTest
public class TOrderInfoMapperTests {
@Autowired
private TOrderInfoMapper tOrderInfoMapper;
@Test
public void test3() {
TOrderInfo orderInfo = new TOrderInfo();
orderInfo.setId(2L);
orderInfo.setMerId("133333333");
int i = tOrderInfoMapper.updateByPrimaryKeySelective(orderInfo);
Assertions.assertThat(i).isEqualTo(1);
}
}

3. 任意 Bean 测试

在没有特殊要求的 Bean 测试中,可以使用默认的 @SpringBootTest 注解:

@SpringBootApplication
public class TOrderInfoServiceTest {
@Configuration
static class TOrderInfoServiceConfig {
@Bean
public TOrderInfoService cityService() {
return new TOrderInfoService();
}
}
@Autowired
private TOrderInfoService tOrderInfoService;
@MockBean
private TOrderInfoMapper tOrderInfoMapper;
@Test
public void test() {
TOrderInfo tOrderInfo = new TOrderInfo();
tOrderInfo.setId(1L);
tOrderInfo.setMerId("QRC123456789012");
Mockito.when(tOrderInfoMapper.selectByPrimaryKey(1L))
.thenReturn(tOrderInfo);
TOrderInfo tOrderInfo1 = tOrderInfoService.selectByPrimaryKey(1L);
Assertions.assertThat(tOrderInfo1).isNotNull();
}
}

五、相关注解汇总

  • @SpringBootTest:用于构建测试用的 Spring 上下文,通常用于集成测试。
  • @WebMvcTest:用于 MVC 控制器的单元测试,自动配置 MockMvc
  • @RestClientTest:用于测试应用程序作为客户端访问其他 REST 服务的场景。
  • @MybatisTest:用于 MyBatis Mapper 层的单元测试。
  • @AutoConfigureMockMvc:自动配置 MockMvc,用于模拟 HTTP 请求。
  • @AutoConfigureMock:用于其他依赖的模拟测试。

六、参考网站

  • 官方文档
  • Spring Boot 测试文档
  • MyBatis Spring Boot 测试

  • 希望这篇优化后的文章能够符合您的需求!

    转载地址:http://ptptz.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现Diffie—Hellman密钥交换(附完整源码)
    查看>>
    Objective-C实现Diffie—Hellman密钥交换(附完整源码)
    查看>>
    Objective-C实现Dijkstra最小路径算法(附完整源码)
    查看>>
    Objective-C实现dijkstra迪杰斯特拉算法(附完整源码)
    查看>>
    Objective-C实现dijkstra迪杰斯特拉算法(附完整源码)
    查看>>
    Objective-C实现Dijkstra迪杰斯特拉算法(附完整源码)
    查看>>
    Objective-C实现dijkstra银行家算法(附完整源码)
    查看>>
    Objective-C实现Dinic算法(附完整源码)
    查看>>
    Objective-C实现disjoint set不相交集算法(附完整源码)
    查看>>
    Objective-C实现DisjointSet并查集的算法(附完整源码)
    查看>>
    Objective-C实现djb2哈希算法(附完整源码)
    查看>>
    Objective-C实现DNF排序算法(附完整源码)
    查看>>
    Objective-C实现doomsday末日算法(附完整源码)
    查看>>
    Objective-C实现double factorial iterative双阶乘迭代算法(附完整源码)
    查看>>
    Objective-C实现double factorial recursive双阶乘递归算法(附完整源码)
    查看>>
    Objective-C实现double hash双哈希算法(附完整源码)
    查看>>
    Objective-C实现double linear search recursion双线性搜索递归算法(附完整源码)
    查看>>
    Objective-C实现double linear search 双线性搜索算法(附完整源码)
    查看>>
    Objective-C实现double sort双重排序算法(附完整源码)
    查看>>
    Objective-C实现DoublyLinkedList双链表的算法(附完整源码)
    查看>>