Einfache Verwendung von Mockito
mockito wird hauptsächlich zur Simulation von Anrufen und Rückgabe von Ergebnissen verwendet und wird häufig in Testfällen verwendet
1. Projektstruktur
Führen Sie eine Datenbankabfrage in studentDao durch, geben Sie bestimmte Daten zurück, verwenden Sie im Komponententest den Mock, um das Rückgabeergebnis von StudentDao zu simulieren, geben Sie die gewünschten Daten zurück (anders als das tatsächliche Rückgabeergebnis in StudentDao) und treffen Sie Bestätigungsentscheidungen
2. Abhängigkeit
<?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. Domänenklasse
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. Die Hauptstartklasse
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. Testklasse
Der schnelle Weg
zum Erstellen einer Testklasse Wählen Sie die Klasse aus, für die Sie einen Test erstellen möchten,
ctrl+shift+t
odercommond+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. Haupttestkategorie
Keine offensichtliche Bedeutung
package com.example.mockito;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MockitoApplicationTests {
@Test
void contextLoads() {
}
}