- 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("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 測試並驗證結果。