- Spring Boot 和 H2 教程
- Spring Boot 和 H2 - 主页
- Spring Boot 和 H2 - 概述
- Spring Boot & H2 - 环境设置
- Spring Boot 和 H2 - 项目设置
- Spring Boot 和 H2 - REST API
- Spring Boot 和 H2 - H2 控制台
- Spring Boot 和 H2 示例
- Spring Boot & H2 - 添加记录
- Spring Boot & H2 - 获取记录
- Spring Boot & H2 - 获取所有记录
- Spring Boot & H2 - 更新记录
- Spring Boot & H2 - 删除记录
- Spring Boot & H2 - 单元测试控制器
- Spring Boot & H2 - 单元测试服务
- Spring Boot 和 H2 - 单元测试存储库
- Spring Boot 和 H2 有用资源
- Spring Boot 和 H2 - 快速指南
- Spring Boot 和 H2 - 有用的资源
- Spring Boot 和 H2 - 讨论
Spring Boot & H2 - 单元测试控制器
与前一章一样,我们已经完成了 REST API。现在让我们在src/main/test文件夹中创建以下结构。
com.tutorialspoint.controller.EmployeeControllerTest - 一个单元测试器类,用于对 EmployeeController 的所有方法进行单元测试。
com.tutorialspoint.repository.EmployeeRepositoryTest - 一个单元测试器类,用于对 EmployeeRepository 的所有方法进行单元测试。
com.tutorialspoint.service.EmployeeServiceTest - 一个单元测试器类,用于对 EmployeeService 的所有方法进行单元测试。
SprintBootH2ApplicationTests 类已经存在。我们需要创建上面的包和相关的类。
员工控制器测试
要测试 REST 控制器,我们需要以下注释和类 -
@ExtendWith(SpringExtension.class) - 使用 SpringExtension 类标记要作为测试用例运行的类。
@SpringBootTest(classes = SprintBootH2Application.class) - 配置 Spring Boot 应用程序。
@AutoConfigureMockMvc - 自动配置 MockMVC 来模拟 HTTP 请求和响应。
@Autowired私有MockMvc mvc;− 测试中使用的 MockMvc 对象。
@MockBean private EmployeeController EmployeeController - 要测试的 EmployeeController 模拟对象。
以下是EmployeeControllerTest的完整代码。
package com.tutorialspoint.controller;
import static org.hamcrest.core.Is.is;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tutorialspoint.entity.Employee;
import com.tutorialspoint.sprintbooth2.SprintBootH2Application;
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = SprintBootH2Application.class)
@AutoConfigureMockMvc
public class EmployeeControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private EmployeeController employeeController;
@Test
public void testGetAllEmployees() throws Exception {
Employee employee = getEmployee();
List<Employee> employees = new ArrayList<>();
employees.add(employee);
given(employeeController.getAllEmployees()).willReturn(employees);
mvc.perform(get("/emp/employees/").contentType(APPLICATION_JSON)).andExpect(status().isOk())
.andExpect(jsonPath("$[0].name", is(employee.getName())));
}
@Test
public void testGetEmployee() throws Exception {
Employee employee = getEmployee();
given(employeeController.getEmployee(1)).willReturn(employee);
mvc.perform(get("/emp/employee/" + employee.getId()).contentType(APPLICATION_JSON)).andExpect(status().isOk())
.andExpect(jsonPath("name", is(employee.getName())));
}
@Test
public void testDeleteEmployee() throws Exception {
Employee employee = getEmployee();
doNothing().when(employeeController).deleteEmployee(1);
mvc.perform(delete("/emp/employee/" + employee.getId()).contentType(APPLICATION_JSON))
.andExpect(status().isOk()).andReturn();
}
@Test
public void testAddEmployee() throws Exception {
Employee employee = getEmployee();
doNothing().when(employeeController).addEmployee(employee);
mvc.perform(post("/emp/employee/").content(asJson(employee)).contentType(APPLICATION_JSON))
.andExpect(status().isOk()).andReturn();
}
@Test
public void testUpdateEmployee() throws Exception {
Employee employee = getEmployee();
doNothing().when(employeeController).updateEmployee(employee);
mvc.perform(put("/emp/employee/").content(asJson(employee)).contentType(APPLICATION_JSON))
.andExpect(status().isOk()).andReturn();
}
private Employee getEmployee() {
Employee employee = new Employee();
employee.setId(1);
employee.setName("Mahesh");
employee.setAge(30);
employee.setEmail("mahesh@test.com");
return employee;
}
private static String asJson(final Object obj) {
try {
return new ObjectMapper().writeValueAsString(obj);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
运行测试用例。
在 eclipse 中右键单击该文件并选择“运行 JUnit 测试”并验证结果。
