junit单元测试:使用@ParameterizedTest 和 @CsvSource注解简化单元测试方法

        在平常的开发工作中,我们经常需要写单元测试。比如,我们有一个校验接口,可能会返回多种错误信息。我们可以针对这个接口,写多个单元测试方法,然后将其场景覆盖全。那么,怎么才能写一个测试方法,就将其涉及到的场景测试全呢?

        1:例如:有一个校验身份证号的接口,涉及业务场景:身份证号为空校验,身份证号是否正确。那么在单元测试的时候,需要测试身份证号为空的数据、身份证号格式错误的数据和身份证号格式正确的数据。

        2:业务代码实现:

package test.boot.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import test.boot.dao.StudentDao;
import test.boot.service.StudentService;
import test.boot.vo.StuentVO;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    public String checkIdNo(String idNo) {
        String regex = "^\\d{17}[0-9Xx]$";
        if (StringUtils.isBlank(idNo)) {
            return "身份证号为空";
        }
        if (idNo.matches(regex)) {
            return "身份证号格式正确";
        } else {
            return "身份证号格式错误";
        }
    }

}

        3:涉及三种场景,单元测试怎么写呢?可能我们会写三个单元测试的方法,如下:

@Test
public void testCheckIdNoEmpty() {
     String result = new StudentServiceImpl().checkIdNo("");
     Assert.assertEquals("身份证号为空", result);
}
@Test
public void testCheckIdNoFormatSuc() {
     String result = new StudentServiceImpl().checkIdNo("666777199911112222");
     Assert.assertEquals("身份证号格式正确", result);
}
@Test
public void testCheckIdNoFormatError() {
    String result = new StudentServiceImpl().checkIdNo("345678889");
    Assert.assertEquals("身份证号格式错误", result);
}

        4:上述写法没有任何问题,如果我们需要测很多个接口,涉及到的业务场景有很多,那么看单元测试的时候,很难发现场景是否覆盖完全,那么我们是否可以优化为一个方法呢?如下:使用 @ParameterizedTest 和 @CsvSource 注解,@ParameterizedTest表示参数注册,也表示单元测试,@ParameterizedTest 和 @Test不能同时使用,@CsvSource 表示多参数注解,可以用分隔符分割数据。

        5:使用 @ParameterizedTest 和 @CsvSource 注解,可以用一个方法,即可测所有场景,我们可以传入两个参数,一个是身份证号,一个是预期结果。代码如下:

@ParameterizedTest
@CsvSource({"'', '身份证号为空'",
             "'610222199911115511', '身份证号格式正确'",
             "'61022219991111551X', '身份证号格式正确'",
             "'6102221999111', '身份证号格式错误'"})
public void testCheckIdNo(String idNo, String expected) {
     StudentService studentService = new StudentServiceImpl();
     String result = studentService.checkIdNo(idNo);
     Assert.assertEquals(expected, result);
}

执行结果:

        不断的学习,才能让自己变得更好!美好的风景一直在路上,加油!

相关推荐

  1. JUnit 4单元测试常用注解方法

    2024-01-07 08:40:02       10 阅读
  2. spring 单元测试 Junit

    2024-01-07 08:40:02       38 阅读
  3. 单元测试框架jUnit

    2024-01-07 08:40:02       42 阅读
  4. 单元测试框架 Junit

    2024-01-07 08:40:02       30 阅读
  5. 浅谈单元测试JUnit4使用

    2024-01-07 08:40:02       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-07 08:40:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-07 08:40:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-07 08:40:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-07 08:40:02       20 阅读

热门阅读

  1. win10、win11安装pytorch(可用)

    2024-01-07 08:40:02       35 阅读
  2. 【React】常用Hook函数的梳理和总结(第二篇)

    2024-01-07 08:40:02       40 阅读
  3. React查询、搜索类功能的实现

    2024-01-07 08:40:02       36 阅读
  4. C++ set用法总结

    2024-01-07 08:40:02       37 阅读
  5. 2024.1.5 Hadoop各组件工作原理,面试题

    2024-01-07 08:40:02       28 阅读
  6. c# 学习笔记 - LINQ

    2024-01-07 08:40:02       35 阅读
  7. ElasticSearch删除索引的命令

    2024-01-07 08:40:02       41 阅读
  8. 2024年学习计划

    2024-01-07 08:40:02       50 阅读
  9. 牛客网编程题——“求IBSN码”

    2024-01-07 08:40:02       37 阅读
  10. Mybatis缓存相关面试题有多卷

    2024-01-07 08:40:02       31 阅读
  11. Android NumberPicker使用

    2024-01-07 08:40:02       44 阅读
  12. SQL SELECT 语句

    2024-01-07 08:40:02       38 阅读
  13. 大模型查询工具助手之股票免费查询接口

    2024-01-07 08:40:02       36 阅读
  14. 数据结构 —— 手写排序算法

    2024-01-07 08:40:02       48 阅读