【从零开始】springboot单元测试(一)

412 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第2天,点击查看活动详情

工作十来年,代码也写了不少,接受过“祖传屎山”,也经历过非常优雅规范的流程,一直心里有些遗憾的,是后来绝大部分公司(不分大小)都忽略了最低成本质量保证的方法:单元测试。虽然很多公司在提,但是很少有公司愿意给程序猿分配写单元测试相应的工作量,因为这玩意表面看起来投入收益不成正比,似乎都是在做无用功,但是在产品的整个生命周期,单元测试却是产品质量的最低保证。

长远考虑,程序猿还是需要在时间允许的情况下,把质量管控的第一道关卡把握好。

源码地址:gitee.com/chemors/spr…

1、 默认版本

springboot 2.7.4

hutool 5.8.8

lombok 1.18.24

2、 maven引入

        <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>

3、代码

3.1 entity

 package com.mos.springboot.unit.sample.entity;
 ​
 import lombok.*;
 ​
 import java.io.Serializable;
 ​
 /**
  * 学生
  *
  * @author 小尘哥
  * @date 2022/10/13
  */
 @Data
 @Builder
 @NoArgsConstructor
 @AllArgsConstructor
 @EqualsAndHashCode
 public class Student implements Serializable {
 ​
     /**
      * id
      */
     private String id;
 ​
     /**
      * 名字
      */
     private String name;
 ​
     /**
      * 性别
      */
     private String sex;
 ​
     /**
      * 校园卡卡号
      */
     private Integer cardNum;
 }
 ​

3.2 service代码

 package com.mos.springboot.unit.sample.service.impl;
 ​
 import com.mos.springboot.unit.sample.entity.Student;
 import com.mos.springboot.unit.sample.service.IStudentService;
 import org.springframework.stereotype.Service;
 ​
 /**
  * 学生服务
  *
  * @author 小尘哥
  * @date 2022/10/13
  */
 @Service
 public class StudentServiceImpl implements IStudentService {
 ​
 ​
     @Override
     public Student getByCardNum(Integer cardNum) {
         return createMock(cardNum);
     }
 ​
     private Student createMock(Integer cardNum){
         return Student.builder().id("1").name("张三").cardNum(cardNum).build();
     }
 }
 ​

3.3 测试类

 package com.mos.springboot.unit.sample;
 ​
 import com.mos.springboot.unit.sample.entity.Student;
 import com.mos.springboot.unit.sample.service.IStudentService;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;
 import org.springframework.boot.test.context.SpringBootTest;
 ​
 import javax.annotation.Resource;
 ​
 @DisplayName("学生服务测试类")
 @SpringBootTest
 public class StudentServiceTest {
 ​
     @Resource
     private IStudentService studentService;
 ​
     @DisplayName("根据校园卡号获取学生信息(有效卡号)")
     @Test
     public void validCardNum(){
         Integer cardNum = 9999;
         Student student = studentService.getByCardNum(cardNum);
         Assertions.assertNotNull(student ,"student must not be null");
     }
 ​
     @DisplayName("根据校园卡号获取学生信息(无效卡号)")
     @Test
     public void unValidCardNum(){
         try {
             Student student = studentService.getByCardNum(Integer.parseInt("abc"));
         }catch (Exception e){
             Assertions.assertNotNull(e instanceof NumberFormatException ,"卡号转换失败,非number format异常");
         }
 ​
     }
 ​
 }
 ​

4、解释

本次是最基础的单元测试,主要注意以下几点:

  1. springboot 2.7.4默认使用junit5,而junit5和junit4已经有较大区别,junit5分为了三个子项目JUnit Platform,JUnit Jupiter和JUnit Vintage,但是对于使用来说,可以暂不关注,因为spring-boot-starter-test已经默认都帮我们解决了。
  2. @SpringBootTest:可以在运行基于Spring Boot的测试的测试类上指定的注释。
  3. @DisplayName:标识测试方法名字,具体展示见测试结果图
  4. 使用“run xxx with coverage”运行,可以看到代码测试覆盖率。

image.png

image.png