好的,我將基於你提供的 AddDepartmentPermission
方法的實現,編寫一個單元測試。我們使用 JUnit 和 Mockito 來進行測試。
原始程式碼回顧
首先,回顧一下 AddDepartmentPermission
方法的實現:
@Override
@Transactional(rollbackFor = Exception.class)
public AddDepartmentPermissionResponse addDepartmentPermission(AddDepartmentPermissionRequest request) {
AddDepartmentPermissionResponse response = new AddDepartmentPermissionResponse();
if (StringUtils.isEmpty(request.getDepartmentId()) || CollectionUtils.isEmpty(request.getDepartmentPermissionDTOList())) {
log.error("團隊新建索關聯引請求引數為空");
throw new TitanException("所傳引數為空!");
}
Department department = getDepartmentById(request.getDepartmentId());
if (department == null) {
addDepartment(request.getDepartmentId());
} else if (department.getGlobalQuery()) {
RespUtils.setError(ErrorCodeEnum.DEPARTMENT_ALREADY_GLOBAL_QUERY, response);
return response;
}
permissionService.addPermissionForDepartment(request.getDepartmentId(), request.getDepartmentPermissionDTOList());
RespUtils.setSuccess(response);
return response;
}
單元測試程式碼
下面是針對 AddDepartmentPermission
方法的單元測試程式碼:
package com.example.service.impl;
import com.example.entity.AddDepartmentPermissionRequest;
import com.example.entity.AddDepartmentPermissionResponse;
import com.example.entity.Department;
import com.example.entity.DepartmentPermissionDTO;
import com.example.exception.TitanException;
import com.example.service.DepartmentService;
import com.example.service.PermissionService;
import com.example.utils.RespUtils;
import com.example.utils.ErrorCodeEnum;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.*;
class DepartmentServiceImplTest {
@Mock
private PermissionService permissionService;
@Mock
private DepartmentService departmentService;
@InjectMocks
private DepartmentServiceImpl departmentServiceImpl;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
void testAddDepartmentPermission_Success() {
// 準備測試資料
AddDepartmentPermissionRequest request = new AddDepartmentPermissionRequest();
request.setDepartmentId("123");
List<DepartmentPermissionDTO> permissionList = new ArrayList<>();
permissionList.add(new DepartmentPermissionDTO("permission1"));
request.setDepartmentPermissionDTOList(permissionList);
// 模擬 getDepartmentById 返回一個非全域性查詢的部門
Department department = new Department();
department.setDepartmentId("123");
department.setGlobalQuery(false);
when(departmentService.getDepartmentById("123")).thenReturn(department);
// 模擬 addPermissionForDepartment 方法的行為
doNothing().when(permissionService).addPermissionForDepartment("123", permissionList);
// 執行測試
AddDepartmentPermissionResponse response = departmentServiceImpl.addDepartmentPermission(request);
// 驗證結果
assertNotNull(response);
assertTrue(response.isSuccess());
verify(permissionService).addPermissionForDepartment("123", permissionList);
}
@Test
void testAddDepartmentPermission_DepartmentNotFound() {
// 準備測試資料
AddDepartmentPermissionRequest request = new AddDepartmentPermissionRequest();
request.setDepartmentId("123");
List<DepartmentPermissionDTO> permissionList = new ArrayList<>();
permissionList.add(new DepartmentPermissionDTO("permission1"));
request.setDepartmentPermissionDTOList(permissionList);
// 模擬 getDepartmentById 返回 null,表示部門不存在
when(departmentService.getDepartmentById("123")).thenReturn(null);
// 執行測試
AddDepartmentPermissionResponse response = departmentServiceImpl.addDepartmentPermission(request);
// 驗證結果
assertNotNull(response);
assertFalse(response.isSuccess());
verify(permissionService, never()).addPermissionForDepartment(anyString(), anyList());
}
@Test
void testAddDepartmentPermission_GlobalQueryDepartment() {
// 準備測試資料
AddDepartmentPermissionRequest request = new AddDepartmentPermissionRequest();
request.setDepartmentId("123");
List<DepartmentPermissionDTO> permissionList = new ArrayList<>();
permissionList.add(new DepartmentPermissionDTO("permission1"));
request.setDepartmentPermissionDTOList(permissionList);
// 模擬 getDepartmentById 返回一個全域性查詢的部門
Department department = new Department();
department.setDepartmentId("123");
department.setGlobalQuery(true);
when(departmentService.getDepartmentById("123")).thenReturn(department);
// 執行測試
AddDepartmentPermissionResponse response = departmentServiceImpl.addDepartmentPermission(request);
// 驗證結果
assertNotNull(response);
assertFalse(response.isSuccess());
assertEquals(ErrorCodeEnum.DEPARTMENT_ALREADY_GLOBAL_QUERY.getCode(), response.getErrorCode());
verify(permissionService, never()).addPermissionForDepartment(anyString(), anyList());
}
@Test
void testAddDepartmentPermission_EmptyRequest() {
// 準備測試資料
AddDepartmentPermissionRequest request = new AddDepartmentPermissionRequest();
// 執行測試
assertThrows(TitanException.class, () -> departmentServiceImpl.addDepartmentPermission(request));
}
}
逐行分析
-
匯入必要的依賴:
import com.example.entity.AddDepartmentPermissionRequest; import com.example.entity.AddDepartmentPermissionResponse; import com.example.entity.Department; import com.example.entity.DepartmentPermissionDTO; import com.example.exception.TitanException; import com.example.service.DepartmentService; import com.example.service.PermissionService; import com.example.utils.RespUtils; import com.example.utils.ErrorCodeEnum; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import java.util.ArrayList; import java.util.List; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.Mockito.*;
-
定義測試類和模擬物件:
class DepartmentServiceImplTest { @Mock private PermissionService permissionService; @Mock private DepartmentService departmentService; @InjectMocks private DepartmentServiceImpl departmentServiceImpl; @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); }
-
測試方法:
-
成功情況:
@Test void testAddDepartmentPermission_Success() { // 準備測試資料 AddDepartmentPermissionRequest request = new AddDepartmentPermissionRequest(); request.setDepartmentId("123"); List<DepartmentPermissionDTO> permissionList = new ArrayList<>(); permissionList.add(new DepartmentPermissionDTO("permission1")); request.setDepartmentPermissionDTOList(permissionList); // 模擬 getDepartmentById 返回一個非全域性查詢的部門 Department department = new Department(); department.setDepartmentId("123"); department.setGlobalQuery(false); when(departmentService.getDepartmentById("123")).thenReturn(department); // 模擬 addPermissionForDepartment 方法的行為 doNothing().when(permissionService).addPermissionForDepartment("123", permissionList); // 執行測試 AddDepartmentPermissionResponse response = departmentServiceImpl.addDepartmentPermission(request); // 驗證結果 assertNotNull(response); assertTrue(response.isSuccess()); verify(permissionService).addPermissionForDepartment("123", permissionList); }
-
部門不存在的情況:
@Test void testAddDepartmentPermission_DepartmentNotFound() { // 準備測試資料 AddDepartmentPermissionRequest request = new AddDepartmentPermissionRequest(); request.setDepartmentId("123"); List<DepartmentPermissionDTO> permissionList = new ArrayList<>(); permissionList.add(new DepartmentPermissionDTO("permission1")); request.setDepartmentPermissionDTOList(permissionList); // 模擬 getDepartmentById 返回 null,表示部門不存在 when(departmentService.getDepartmentById("123")).thenReturn(null); // 執行測試 AddDepartmentPermissionResponse response = departmentServiceImpl.addDepartmentPermission(request); // 驗證結果 assertNotNull(response); assertFalse(response.isSuccess()); verify(permissionService, never()).addPermissionForDepartment(anyString(), anyList()); }
-
全域性查詢部門的情況:
@Test void testAddDepartmentPermission_GlobalQueryDepartment() { // 準備測試資料 AddDepartmentPermissionRequest request = new AddDepartmentPermissionRequest(); request.setDepartmentId("123"); List<DepartmentPermissionDTO> permissionList = new ArrayList<>(); permissionList.add(new DepartmentPermissionDTO("permission1")); request.setDepartmentPermissionDTOList(permissionList); // 模擬 getDepartmentById 返回一個全域性查詢的部門 Department department = new Department(); department.setDepartmentId("123"); department.setGlobalQuery(true); when(departmentService.getDepartmentById("123")).thenReturn(department); // 執行測試 AddDepartmentPermissionResponse response = departmentServiceImpl.addDepartmentPermission(request); // 驗證結果 assertNotNull(response); assertFalse(response.isSuccess()); assertEquals(ErrorCodeEnum.DEPARTMENT_ALREADY_GLOBAL_QUERY.getCode(), response.getErrorCode()); verify(permissionService, never()).addPermissionForDepartment(anyString(), anyList()); }
-
空請求的情況:
@Test void testAddDepartmentPermission_EmptyRequest() { // 準備測試資料 AddDepartmentPermissionRequest request = new AddDepartmentPermissionRequest(); // 執行測試 assertThrows(TitanException.class, () -> departmentServiceImpl.addDepartmentPermission(request)); }
-
總結
這些測試方法覆蓋了 addDepartmentPermission
方法的各種情況,包括成功新增許可權、部門不存在、部門已經是全域性查詢的情況以及空請求的情況。透過這些測試,可以確保 addDepartmentPermission
方法按預期工作。