一、测试介绍
二、单元测试
1)介绍
2)快速入门
添加依赖
<dependencies><!-- junit依赖 --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.9.1</version></dependency>
</dependencies>
UserService
package com.project;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
public class UserService {
/*** 给定一个身份证号, 计算出该用户的年龄* @param idCard 身份证号*/public Integer getAge(String idCard){if (idCard == null || idCard.length() != 18) {throw new IllegalArgumentException("无效的身份证号码");}String birthday = idCard.substring(6, 14);LocalDate parse = LocalDate.parse(birthday, DateTimeFormatter.ofPattern("yyyyMMdd"));return Period.between(parse, LocalDate.now()).getYears();}
/*** 给定一个身份证号, 计算出该用户的性别* @param idCard 身份证号*/public String getGender(String idCard){if (idCard == null || idCard.length() != 18) {throw new IllegalArgumentException("无效的身份证号码");}return Integer.parseInt(idCard.substring(16,17)) % 2 == 1 ? "男" : "女";}
}
UserServiceTest
package com.project;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
//测试类
public class UserServiceTest {
@Testpublic void testGetAge(){UserService userService = new UserService();Integer age = userService.getAge("100000200010011011");System.out.println(age);}
@Testpublic void testGetName(){UserService userService = new UserService();String gender = userService.getGender("100000200010011011");System.out.println(gender);}// 断言可以判断业务逻辑是否正确@Testpublic void testGenderWithAssert(){UserService userService = new UserService();String gender = userService.getGender("100000200010011011");// 断言Assertions.assertEquals("男", gender,"性别获取有问题");}
@Testpublic void testGenderWithAssert2(){UserService userService = new UserService();String gender = userService.getGender("100000200010011011");// 断言如果有错误抛出异常Assertions.assertThrows(IllegalArgumentException.class, ()->{userService.getGender("100000200010011011");});}
}
3)断言
上述方法形参中的最后一个参数 msg ,表示错误提示信息,可以不指定(有对应的重载方法)
4)常见注解
5)单元测试-企业开发规范
1、判断测试方法的覆盖率
2、只判断某个类之中的覆盖率
3、AI生成单元测试
IDea安装通义灵码AI插件
点击标志后,点击单元测试
package com.project;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.*;
public class UserServiceAiTest {
private UserService userService;@BeforeEach // 在每个测试方法执行前执行public void setUp() {userService = new UserService();}
@Testpublic void getGender_ValidMaleIdCard_ReturnsMale() {String gender = userService.getGender("100000200010011011");assertEquals("男", gender, "性别获取错误,应为男性");}
@Testpublic void getGender_ValidFemaleIdCard_ReturnsFemale() {String gender = userService.getGender("100000200010011022");assertEquals("女", gender, "性别获取错误,应为女性");}
@Testpublic void getGender_NullIdCard_ThrowsException() {assertThrows(IllegalArgumentException.class, () -> {userService.getGender(null);}, "无效的身份证号码");}
@Testpublic void getGender_InvalidLengthIdCard_ThrowsException() {assertThrows(IllegalArgumentException.class, () -> {userService.getGender("10000020001001101");}, "无效的身份证号码");}
@ParameterizedTest@ValueSource(strings = {"100000200010011011", "100000200010011031", "100000200010011051"})public void getGender_MultipleMaleIdCards_ReturnsMale(String idCard) {String gender = userService.getGender(idCard);assertEquals("男", gender, "性别获取错误,应为男性");}
@ParameterizedTest@ValueSource(strings = {"100000200010011022", "100000200010011042", "100000200010011062"})public void getGender_MultipleFemaleIdCards_ReturnsFemale(String idCard) {String gender = userService.getGender(idCard);assertEquals("女", gender, "性别获取错误,应为女性");}
}
6)生命周期中的test
所有命名规范的test都会运行