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

本文共 3122 字,大约阅读时间需要 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 注解可以自动识别主配置类,创建测试环境。

@SpringBootApplicationpublic 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 假装无服务器环境执行测试:

@Autowiredprivate MockMvc mockMvc;@Testpublic void test() throws Exception {    mockMvc.perform(MockMvcRequestBuilders.get("/query"))        .andExpect(MockMvcResultMatchers.status().isOk());}

四、单元测试

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

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

2. MyBatis Mapper 测试

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

@MybatisTestpublic 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 注解:

@SpringBootApplicationpublic 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/

    你可能感兴趣的文章
    postgresql 更新多列数据
    查看>>
    PostgreSQL 服务启动后停止
    查看>>
    PostgreSQL 辟谣存在任意代码执行漏洞:消息不实
    查看>>
    PostgreSQL+PostGIS实现两坐标点之间最短路径查询算法函数(地图工具篇.12)
    查看>>
    Qt开发——简易调色板QPalette
    查看>>
    PostgreSQL-解决连接时遇到的乱码问题
    查看>>
    PostgreSQL15.2最新版本安装_远程连接_Navicat操作_pgAdmin操作_Windows10上安装---PostgreSQL工作笔记001
    查看>>
    PostgreSQL9.1 双机部署配置(主备数据同步)
    查看>>
    Qt开发——简易网络浏览器(一)
    查看>>
    Qt开发——简易成绩登记系统
    查看>>
    Postgresql中PL/pgSQL代码块的语法与使用-声明与赋值、IF语句、CASE语句、循环语句
    查看>>
    Postgresql中PL/pgSQL的游标、自定义函数、存储过程的使用
    查看>>
    Postgresql中的表结构和数据同步/数据传输到Mysql
    查看>>
    Postgresql中自增主键序列的使用以及数据传输时提示:错误:关系“xxx_xx_xx_seq“不存在
    查看>>
    postgreSQL入门命令
    查看>>
    PostgreSQL删除数据库报"ERROR: There is 1 other session using the database."
    查看>>
    Qt开发——爱情公寓人事管理系统
    查看>>
    PostgreSQL和Oracle两种数据库有啥区别?如何选择?
    查看>>
    Postgresql在Windows中使用pg_dump实现数据库(指定表)的导出与导入
    查看>>
    PostgreSQL在何处处理 sql查询之四
    查看>>