Spring Boot & H2 - 單元測試服務



概述

要測試服務,我們需要使用以下注釋和類 −

  • @ExtendWith(SpringExtension.class) − 使用 SpringExtension 類標識要作為測試用例執行的類。

  • @SpringBootTest(classes = SprintBootH2Application.class) − 配置 Spring Boot 應用程式。

  • @MockBean private EmployeeService employeeService − 要進行測試的 EmployeeService 模擬物件。

EmployeeServiceTest.java

以下為 EmployeeServiceTest 類的完整程式碼。

package com.tutorialspoint.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import com.tutorialspoint.entity.Employee;
import com.tutorialspoint.springboot_h2.SpringbootH2Application;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = SpringbootH2Application.class)
public class EmployeeServiceTest {
   
   @MockBean
   private EmployeeService employeeService;
   
   @Test
   public void testGetAllEmployees() throws Exception {
      Employee employee = getEmployee();
      List<Employee> employees = new ArrayList<>();
      employees.add(employee);
      given(employeeService.getAllEmployees()).willReturn(employees);
      List<Employee> result = employeeService.getAllEmployees();
      assertEquals(result.size(), 1);
   }
   
   @Test
   public void testGetEmployee() throws Exception {
      Employee employee = getEmployee();
      given(employeeService.getEmployeeById(1)).willReturn(employee);
      Employee result = employeeService.getEmployeeById(1);
      assertEquals(result.getId(), 1);	
   }
   
   @Test
   public void testDeleteEmployee() throws Exception {
      doNothing().when(employeeService).deleteEmployeeById(1);
      employeeService.deleteEmployeeById(1);
      assertTrue(true);
   }
   
   @Test
   public void testSaveOrUpdateEmployee() throws Exception {
      Employee employee = getEmployee();
      doNothing().when(employeeService).saveOrUpdate(employee);	
      employeeService.saveOrUpdate(employee);
      assertTrue(true);
   }
   
   private Employee getEmployee() {
      Employee employee = new Employee();
      employee.setId(1);
      employee.setName("Mahesh");
      employee.setAge(30);
      employee.setEmail("mahesh@test.com");
      return employee;
   }
}

執行測試用例。

在 Eclipse 中右鍵單擊該檔案,然後選擇執行 JUnit 測試並驗證結果。

Service Test Result
廣告
© . All rights reserved.