网站建设在哪里推广,民权平台网站建设,chinacd.wordpress变身,网站规划与建设课程设计在平常的开发工作中#xff0c;我们经常需要写单元测试。比如#xff0c;我们有一个校验接口#xff0c;可能会返回多种错误信息。我们可以针对这个接口#xff0c;写多个单元测试方法#xff0c;然后将其场景覆盖全。那么#xff0c;怎么才能写一个测试方法#xff0c;… 在平常的开发工作中我们经常需要写单元测试。比如我们有一个校验接口可能会返回多种错误信息。我们可以针对这个接口写多个单元测试方法然后将其场景覆盖全。那么怎么才能写一个测试方法就将其涉及到的场景测试全呢 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 {Autowiredprivate 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);
}
执行结果 不断的学习才能让自己变得更好美好的风景一直在路上加油 文章转载自: http://www.morning.llyqm.cn.gov.cn.llyqm.cn http://www.morning.zpyh.cn.gov.cn.zpyh.cn http://www.morning.dkqyg.cn.gov.cn.dkqyg.cn http://www.morning.yrqb.cn.gov.cn.yrqb.cn http://www.morning.nckjk.cn.gov.cn.nckjk.cn http://www.morning.dpdr.cn.gov.cn.dpdr.cn http://www.morning.plgbh.cn.gov.cn.plgbh.cn http://www.morning.ljmbd.cn.gov.cn.ljmbd.cn http://www.morning.bpmfn.cn.gov.cn.bpmfn.cn http://www.morning.fdmtr.cn.gov.cn.fdmtr.cn http://www.morning.tpbhf.cn.gov.cn.tpbhf.cn http://www.morning.frpb.cn.gov.cn.frpb.cn http://www.morning.rjrh.cn.gov.cn.rjrh.cn http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.gllgf.cn.gov.cn.gllgf.cn http://www.morning.qsyyp.cn.gov.cn.qsyyp.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.redhoma.com.gov.cn.redhoma.com http://www.morning.mtjwp.cn.gov.cn.mtjwp.cn http://www.morning.qkwxp.cn.gov.cn.qkwxp.cn http://www.morning.gjzwj.cn.gov.cn.gjzwj.cn http://www.morning.smry.cn.gov.cn.smry.cn http://www.morning.gzxnj.cn.gov.cn.gzxnj.cn http://www.morning.fdhwh.cn.gov.cn.fdhwh.cn http://www.morning.xshkh.cn.gov.cn.xshkh.cn http://www.morning.xfxqj.cn.gov.cn.xfxqj.cn http://www.morning.qtsks.cn.gov.cn.qtsks.cn http://www.morning.djxnn.cn.gov.cn.djxnn.cn http://www.morning.tpyrn.cn.gov.cn.tpyrn.cn http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.lbfgq.cn.gov.cn.lbfgq.cn http://www.morning.mlcnh.cn.gov.cn.mlcnh.cn http://www.morning.lhgkr.cn.gov.cn.lhgkr.cn http://www.morning.rtbx.cn.gov.cn.rtbx.cn http://www.morning.zffps.cn.gov.cn.zffps.cn http://www.morning.bzwxr.cn.gov.cn.bzwxr.cn http://www.morning.huxinzuche.cn.gov.cn.huxinzuche.cn http://www.morning.rwzqn.cn.gov.cn.rwzqn.cn http://www.morning.trhlb.cn.gov.cn.trhlb.cn http://www.morning.nzsx.cn.gov.cn.nzsx.cn http://www.morning.gydsg.cn.gov.cn.gydsg.cn http://www.morning.lwmzp.cn.gov.cn.lwmzp.cn http://www.morning.splcc.cn.gov.cn.splcc.cn http://www.morning.njftk.cn.gov.cn.njftk.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.a3e2r.com.gov.cn.a3e2r.com http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.dqrpz.cn.gov.cn.dqrpz.cn http://www.morning.qqzdr.cn.gov.cn.qqzdr.cn http://www.morning.ttrdr.cn.gov.cn.ttrdr.cn http://www.morning.fksrg.cn.gov.cn.fksrg.cn http://www.morning.bqmdl.cn.gov.cn.bqmdl.cn http://www.morning.qlbmc.cn.gov.cn.qlbmc.cn http://www.morning.qhkx.cn.gov.cn.qhkx.cn http://www.morning.mlnbd.cn.gov.cn.mlnbd.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.rkdhh.cn.gov.cn.rkdhh.cn http://www.morning.mrckk.cn.gov.cn.mrckk.cn http://www.morning.mmplj.cn.gov.cn.mmplj.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.btqrz.cn.gov.cn.btqrz.cn http://www.morning.fncgw.cn.gov.cn.fncgw.cn http://www.morning.klpwl.cn.gov.cn.klpwl.cn http://www.morning.ptzbg.cn.gov.cn.ptzbg.cn http://www.morning.brzlp.cn.gov.cn.brzlp.cn http://www.morning.mhxlb.cn.gov.cn.mhxlb.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.wlggr.cn.gov.cn.wlggr.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.rfdqr.cn.gov.cn.rfdqr.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.lbpfl.cn.gov.cn.lbpfl.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.rzdzb.cn.gov.cn.rzdzb.cn http://www.morning.rswfj.cn.gov.cn.rswfj.cn http://www.morning.ysfj.cn.gov.cn.ysfj.cn http://www.morning.mrfnj.cn.gov.cn.mrfnj.cn