单元测试真

好的,我将基于你提供的 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));
    }
}

逐行分析

  1. 导入必要的依赖

    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.*;
    
  2. 定义测试类和模拟对象

    class DepartmentServiceImplTest {
    
        @Mock
        private PermissionService permissionService;
    
        @Mock
        private DepartmentService departmentService;
    
        @InjectMocks
        private DepartmentServiceImpl departmentServiceImpl;
    
        @BeforeEach
        void setUp() {
            MockitoAnnotations.openMocks(this);
        }
    
  3. 测试方法

    • 成功情况

      @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 方法按预期工作。

posted @ 2024-11-12 11:08  一曲微茫  阅读(5)  评论(0编辑  收藏  举报