前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【从零开始】springboot单元测试(一)

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

作者头像
小尘哥
发布2022-12-07 10:43:02
3480
发布2022-12-07 10:43:02
举报
文章被收录于专栏:小尘哥的专栏小尘哥的专栏

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

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

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

源码地址:https://gitee.com/chemors/springboot-unit-test-sample

1、 默认版本

springboot 2.7.4

hutool 5.8.8

lombok 1.18.24

2、 maven引入

代码语言:javascript
复制
   <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

代码语言:javascript
复制
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代码

代码语言:javascript
复制
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 测试类

代码语言:javascript
复制
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”运行,可以看到代码测试覆盖率。
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-10-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 陌与尘埃 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、 默认版本
  • 2、 maven引入
  • 3、代码
    • 3.1 entity
      • 3.2 service代码
        • 3.3 测试类
        • 4、解释
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档