- 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 类已经存在。我们需要创建上述包和相关的类。
EmployeeControllerTest
要测试 REST 控制器,我们需要以下注解和类:
@ExtendWith(SpringExtension.class) − 使用 SpringExtension 类将类标记为运行为测试用例。
@SpringBootTest(classes = SprintBootH2Application.class) − 配置 Spring Boot 应用程序。
@AutoConfigureMockMvc − 自动配置 MockMVC 以模拟 HTTP 请求和响应。
@Autowired private MockMvc mvc; − 用于测试的 MockMvc 对象。
@MockBean private EmployeeController employeeController − 要测试的 EmployeeController 模拟对象。
EmployeeControllerTest.java
以下是 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.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; 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.springboot_h2.SpringbootH2Application; @ExtendWith(SpringExtension.class) @SpringBootTest(classes = SpringbootH2Application.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("[email protected]"); return employee; } private static String asJson(final Object obj) { try { return new ObjectMapper().writeValueAsString(obj); } catch (Exception e) { throw new RuntimeException(e); } } }
运行测试用例。
在 eclipse 中右键单击文件,然后选择运行 JUnit 测试并验证结果。