Simple case use of mockito
mockito is mainly used to simulate calls and return results, and is often used in test cases
1. Project structure
Perform database query in studentDao, return specific data, use mock in unit test to simulate the return result of StudentDao, return the desired data (different from the actual return result in StudentDao), and make assertion judgments
2. Dependence
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mockito</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mockito</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.domain class
package com.example.mockito.domain;
public class Student {
private String name;
private int age;
private Integer id;
public Student(String name, Integer age, Integer id) {
this.name = name;
this.age = age;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
4.dao
package com.example.mockito.dao;
import com.example.mockito.domain.Student;
import org.springframework.stereotype.Component;
@Component
public class StudentDao {
public Student getById(Integer id) {
return new Student("张三", 20, 5);
}
}
5.service
package com.example.mockito.service;
import com.example.mockito.dao.StudentDao;
import com.example.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(Integer id) {
return studentDao.getById(id);
}
}
6. The main startup class
package com.example.mockito;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MockitoApplication {
public static void main(String[] args) {
SpringApplication.run(MockitoApplication.class, args);
}
}
7. Test class
The quick way
to create a test class Select the class you want to create a test
ctrl+shift+t
orcommond+shift+t
package com.example.mockito.service;
import com.example.mockito.dao.StudentDao;
import com.example.mockito.domain.Student;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
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;
@SpringBootTest
class StudentServiceTest {
@Autowired
StudentService studentService;
@MockBean
StudentDao studentDao;
@BeforeEach
void setUp() {
Mockito.when(studentDao.getById(5)).thenReturn(new Student("李四", 30, 5));
}
@Test
void getStudentById() {
Student student = studentService.getStudentById(5);
Assert.assertNotNull(student);
Assert.assertEquals(student.getAge(), 30);
Assert.assertEquals(student.getName(), "李四");
}
}
8. Main test category
No obvious meaning
package com.example.mockito;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MockitoApplicationTests {
@Test
void contextLoads() {
}
}