Junit单元测试使用
-
- 1.简单测试
- 2.DAO未完成时的测试
- 3.测试方法
- 4.覆盖率查看
1.简单测试
先创建springBoot项目,选择javaWeb进行创建
创建实体类、DAO、Service
实体类
public class Student {
private int id;
private int age;
private String name;
public Student(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
对应的DAO类
import com.zxm.mockito.domain.Student;
import org.springframework.stereotype.Component;
@Component
public class StudentDao {
public Student getStudentById(int id){
return new Student(id,10,"Tony");
}
}
对应的Service类
import com.zxm.mockito.dao.StudentDao;
import com.zxm.mockito.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
@Autowired
StudentDao studentDao;
public Student getStudentById(int id){
return studentDao.getStudentById(id);
}
}
对需要测试的类,按ctrl + shift +T,生成测试类

或者鼠标右键Generate再选择test
接下来就是选择好测试的方法
在测试类里进行方法测试的内容,该类用于测试一般不需要公开,对测试类添加@SpringBootTest注解,再@Autowired自动装配StudentService对象
@Test
void getStudentById() {
Student student =studentService.getStudentById(1);
Assertions.assertNotNull(student);
Assertions.assertEquals(18,student.getAge());
Assertions.assertEquals("cattle",student.getName());
//lambda表达式一次检测全部的断言
//Assertions.assertAll(()->Assertions.assertNull(student),
//()->Assertions.assertEquals(12,student.getAge()),
//()->Assertions.assertEquals("Tom",student.getName())
// );
}
lamda表达式的查看,第一个是期望参数,第二个是实际参数
2.DAO未完成时的测试
用Mockito执行when方法返回Mock数据,在when里的方法执行前返回一个对象
MockBean的作用:创建一个虚拟的对象替代那些不易构造或不易获取的对象。
@MockBean
StudentDao studentDao;
@BeforeEach
void setUp(){
Mockito.when(studentDao.getStudentById(1)).thenReturn(new Student(1,18,"cattle"));
}
再进行上面方法测试。
3.测试方法
假设测试,Assumptions.assumeTrue()只有假设成立才进行断言,否则忽略或跳过
@Test
@DisplayName("Should only run test if some criteria are met")
void shouldOnlyRun(){
Assumptions.assumeTrue(20<10);
Assertions.assertEquals(1,1);
}
数据驱动测试,将ValueSource里的数据都带入参数进行测试
//数据驱动测试
@ParameterizedTest(name="{0}")
@DisplayName("should create shapes with different number of size")
@ValueSource(ints={3,5,4,9,7,8})
void shouldCreateShape(int expectedInt){
List<Integer> list =new ArrayList<Integer>();
list.add(0,expectedInt);
Assertions.assertEquals(expectedInt,list.get(0));
}
异常值检测,所有ValueSource的值都需要异常抛出才能正常通过
//无效值异常检测
@ParameterizedTest
@DisplayName("check invalid nums")
@ValueSource(ints={-1,0,2,Integer.MAX_VALUE})
void checkInvalidNums(int expectedInt){
Assertions.assertThrows(IllegalArgumentException.class,
()->normalNums(expectedInt));
}
//检测方法
public void normalNums(int num){
if(num<=0||num==Integer.MAX_VALUE){
throw new IllegalArgumentException();
}
}
嵌套测试,对多个相似或不同的模块进行测试,@Nested对类进行嵌套说明。
assertNotSame与assertNotEquals不同,后者更擅长对比数字和boolean值,前者的语义要求更严格一些
//嵌套测试 相似的类进行串联测试
@Nested
@DisplayName("Whem a List has been create")
class WhenListCreate{
private final List<Integer> list=new ArrayList<>();
@Nested
@DisplayName("Should allow")
class ShouldAllow{
@Test
@DisplayName("See the size")
void seeTheSide(){
Assertions.assertEquals(0,list.size());
}
@Test
@DisplayName("See the description")
void seeTheDescription(){
Assertions.assertEquals("[]",list.toString());
}
}
@Nested
@DisplayName("Should not allow")
class ShouldNotAllow{
@Test
@DisplayName("be equal to another list with the same size")
void beEqualAnotherList(){
Assertions.assertNotSame(new ArrayList<String>(),list);
}
}
}
4.覆盖率查看
在整个项目目录上右键选择测试覆盖,
从自己的包中找到对应的类
每个包对应的类的覆盖率、方法覆盖率、行覆盖率都可以查看