Uso de caso simple de mockito
mockito se usa principalmente para simular llamadas y devolver resultados, y a menudo se usa en casos de prueba
1. Estructura del proyecto
Realice una consulta de base de datos en studentDao, devuelva datos específicos, use simulacro en la prueba unitaria para simular el resultado devuelto de StudentDao, devuelva los datos deseados (diferentes del resultado devuelto real en StudentDao) y haga juicios de afirmación
2. Dependencia
<?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.clase de dominio
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.servicio
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. La clase de inicio principal
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. Clase de prueba
La forma rápida
de crear una clase de prueba Seleccione la clase en la que desea crear una prueba
ctrl+shift+t
ocommond+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. Categoría de prueba principal
Sin significado obvio
package com.example.mockito;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MockitoApplicationTests {
@Test
void contextLoads() {
}
}