junit框架(单元测试、mock、覆盖率)
类似python中测试类下面的执行顺序
import org.junit.*; import static org.junit.Assert.assertEquals; public class CalculatorTest { public CalculatorTest() { System.out.println("构造函数"); } @BeforeClass public static void beforeClass() { System.out.println("@BeforeClass, 所有测试开始之前运行。"); } @Before public void befor() { System.out.println("@Before, 每一个测试方法之前运行。"); } @Test public void test() { System.out.println("@Test1"); Calculator c = new Calculator(); int a = c.add(1, 2); assertEquals(a, 3); } @Ignore public void testIgnore() { System.out.println("@Ignore, 忽略的测试用例。"); } @After public void after() { System.out.println("@After, 每一个测试方法之后运行。"); } @AfterClass public static void afterClass() { System.out.println("@AfterClass, 所有测试结果之后运行。"); } } /*运行结果如下 @BeforeClass, 所有测试开始之前运行。 构造函数 @Before, 每一个测试方法之前运行。 @Test1 @After, 每一个测试方法之后运行。 @AfterClass, 所有测试结果之后运行。 */
一、依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
二、创建测试类

三、示例以及mock
1、domain类
package yin.domain; 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; } }
2、dao层类
package yin.dao; import org.springframework.stereotype.Component; import yin.domain.Student; @Component public class StudentDao { public Student getStudentById(int id){ return new Student(1,12,"whitewall"); } }
3、service层类
package yin.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import yin.dao.StudentDao; import yin.domain.Student; @Service("StudentService") public class StudentService { @Autowired StudentDao studentdao; public Student getStudentById(int id){ return studentdao.getStudentById(id); } public static void main(String[] args) { StudentService studentService = new StudentService(); Student studentById = studentService.getStudentById(1); System.out.println(studentById.getAge()); } }
4、测试类(包含mock)
package yin.service; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import yin.dao.StudentDao; import yin.domain.Student; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest class StudentServiceTest { @Autowired StudentService studentService; @MockBean StudentDao studentDao; @BeforeEach void setUp() { Mockito.when(studentDao.getStudentById(1)). thenReturn(new Student(1,10,"yinwenbin")); } @Test void getStudentById() { Student student = studentService.getStudentById(1); assertNotNull(student); assertEquals(student.getAge(),10); } }
四、覆盖率
1、设置

2、运行

一个只会点点点的测试,有疑问可以在测试群(群号:330405140)问我

浙公网安备 33010602011771号