首页 > 其他分享 >删除团队关联索引单元测试

删除团队关联索引单元测试

时间:2024-11-14 20:11:14浏览次数:1  
标签:删除 indexPatternNameList request 单元测试 DelDepartmentPermissionRequest 索引 departmen

理解了,如果你不能更改 RespUtils 类,我们可以在测试用例中直接检查 BaseResponse 对象的成功状态和其他属性。以下是更新后的单元测试代码,不依赖于 RespUtils 中的方法。

更新后的单元测试代码

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

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.Arrays;
import java.util.Collections;
import java.util.List;

import com.example.service.DepartmentServiceImpl;
import com.example.mapper.DepartmentMapper;
import com.example.request.DelDepartmentPermissionRequest;
import com.example.response.DelDepartmentPermissionResponse;
import com.example.entity.Department;
import com.example.service.DepartmentPermissionService;
import com.example.service.IndexPatternService;
import com.example.exception.BaseBizException;
import com.example.enums.HadesErrCodeEnum;
import com.example.enums.ErrCodeBaseEnum;

public class DepartmentServiceImplTest {

    @Mock
    private DepartmentMapper departmentMapper;

    @Mock
    private DepartmentPermissionService permissionService;

    @Mock
    private IndexPatternService indexPatternService;

    @InjectMocks
    private DepartmentServiceImpl departmentService;

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.openMocks(this);
    }

    @Test
    public void testDeleteDepartmentPermission_Success() throws Exception {
        // 准备数据
        String departmentId = "123";
        List<String> indexPatternNameList = Arrays.asList("pattern1", "pattern2");
        List<Long> indexIdList = Arrays.asList(1L, 2L);

        Department department = new Department();
        department.setDepartmentId(departmentId);

        // 模拟行为
        when(departmentMapper.selectOneByExample(any())).thenReturn(department);
        when(indexPatternService.getIndexPatternIdByNameList(indexPatternNameList)).thenReturn(indexIdList);

        // 执行方法
        DelDepartmentPermissionRequest request = new DelDepartmentPermissionRequest();
        request.setDepartmentId(departmentId);
        request.setIndexPatternNameList(indexPatternNameList);

        DelDepartmentPermissionResponse response = departmentService.deleteDepartmentPermission(request);

        // 断言结果
        assertNotNull(response);
        assertTrue(response.isSuccess());
        verify(permissionService).deletePermissionForDepartment(departmentId, indexIdList);
    }

    @Test
    public void testDeleteDepartmentPermission_DepartmentNotFound() {
        // 准备数据
        String departmentId = "123";
        List<String> indexPatternNameList = Arrays.asList("pattern1", "pattern2");

        // 模拟行为
        when(departmentMapper.selectOneByExample(any())).thenReturn(null);

        // 执行方法
        DelDepartmentPermissionRequest request = new DelDepartmentPermissionRequest();
        request.setDepartmentId(departmentId);
        request.setIndexPatternNameList(indexPatternNameList);

        DelDepartmentPermissionResponse response = departmentService.deleteDepartmentPermission(request);

        // 断言结果
        assertNotNull(response);
        assertFalse(response.isSuccess());
        assertEquals(HadesErrCodeEnum.DEPARTMENT_NOT_FOUND.getErrCode(), response.getErrCode());
        assertEquals(HadesErrCodeEnum.DEPARTMENT_NOT_FOUND.getErrMsg(), response.getErrMsg());

        // 验证日志
        verifyNoInteractions(permissionService);
        verifyNoInteractions(indexPatternService);
    }

    @Test
    public void testDeleteDepartmentPermission_EmptyDepartmentId() {
        // 准备数据
        List<String> indexPatternNameList = Arrays.asList("pattern1", "pattern2");

        // 执行方法
        DelDepartmentPermissionRequest request = new DelDepartmentPermissionRequest();
        request.setDepartmentId("");
        request.setIndexPatternNameList(indexPatternNameList);

        assertThrows(BaseBizException.class, () -> {
            departmentService.deleteDepartmentPermission(request);
        });

        // 验证日志
        verifyNoInteractions(departmentMapper);
        verifyNoInteractions(permissionService);
        verifyNoInteractions(indexPatternService);
    }

    @Test
    public void testDeleteDepartmentPermission_EmptyIndexPatternNameList() {
        // 准备数据
        String departmentId = "123";

        // 执行方法
        DelDepartmentPermissionRequest request = new DelDepartmentPermissionRequest();
        request.setDepartmentId(departmentId);
        request.setIndexPatternNameList(Collections.emptyList());

        assertThrows(BaseBizException.class, () -> {
            departmentService.deleteDepartmentPermission(request);
        });

        // 验证日志
        verifyNoInteractions(departmentMapper);
        verifyNoInteractions(permissionService);
        verifyNoInteractions(indexPatternService);
    }

    @Test
    public void testDeleteDepartmentPermission_IndexPatternNotFound() {
        // 准备数据
        String departmentId = "123";
        List<String> indexPatternNameList = Arrays.asList("pattern1", "pattern2");

        Department department = new Department();
        department.setDepartmentId(departmentId);

        // 模拟行为
        when(departmentMapper.selectOneByExample(any())).thenReturn(department);
        when(indexPatternService.getIndexPatternIdByNameList(indexPatternNameList)).thenReturn(Collections.emptyList());

        // 执行方法
        DelDepartmentPermissionRequest request = new DelDepartmentPermissionRequest();
        request.setDepartmentId(departmentId);
        request.setIndexPatternNameList(indexPatternNameList);

        DelDepartmentPermissionResponse response = departmentService.deleteDepartmentPermission(request);

        // 断言结果
        assertNotNull(response);
        verify(permissionService, never()).deletePermissionForDepartment(anyString(), anyList());
    }
}

解释每个步骤

  1. 准备数据

    • 创建必要的输入数据,如 departmentIdindexPatternNameList
  2. 模拟行为

    • 使用 Mockito 模拟 departmentMapperindexPatternService 的行为。
  3. 执行方法

    • 创建 DelDepartmentPermissionRequest 对象并调用 departmentService.deleteDepartmentPermission 方法。
  4. 断言结果

    • 使用 assertNotNull 确保响应对象不为空。
    • 直接检查 response.isSuccess() 方法来验证响应对象的状态是否成功。
    • 直接检查 response.getErrCode()response.getErrMsg() 方法来验证响应对象的错误码和错误信息是否符合预期。
  5. 验证日志

    • 使用 verifyNoInteractions 方法确保在某些情况下,某些服务没有被调用。

通过这些更新,我们确保了测试用例能够在不依赖 RespUtils 方法的情况下,正确验证 deleteDepartmentPermission 方法的行为。希望这些更新对你有所帮助!如果有任何问题或需要进一步的帮助,请随时告诉我。

标签:删除,indexPatternNameList,request,单元测试,DelDepartmentPermissionRequest,索引,departmen
From: https://www.cnblogs.com/lmzzr24/p/18546693

相关文章

  • flutter TabBarView 动态添加删除页面
    在TabBarView动态添加页面后删除其中一个页面会导致后面的页面状态错误或删除的页面不正确。出现这种问题是由于创建子页面时没有为子页面设置唯一的key导致的。1voidaddNewPage(){2_pageCount++;3setState((){4Stringtitle="页面$_pageCount......
  • cmu15545-索引并发控制(Concurrent Indexes)
    目录OverviewLock和Latch辨析设计目标大致分类HashTableLatchesPageLatchesSlotLatchesB+TreeLatches并发问题LatchCrabbing/CoupingOptimisticCoupling(乐观锁)LeafNodeScanOverviewLock和Latch辨析Lock:抽象的,逻辑的,整体统筹Latch:具体的,原语性的,自我管理本节主要探......
  • 代码随想录算法训练营day45| 115.不同的子序列 583. 两个字符串的删除操作 72.
    学习资料:https://programmercarl.com/0115.不同的子序列.html#算法公开课动态规划系列之编辑距离问题学习记录:115.不同的子序列(当遇到相同字母时,可以选择也可以不选;刚开始没看懂;dp[i][j]是对应i-1结尾和j-1结尾,这样的目的是方便第一行和第一列初始化)点击查看代码classSolut......
  • mysql修改/删除
    mysql增删改查中的改使用update表名set列名条件where;  matched是当前表有几行,changed是执行了几行,warnings是警告删除deleteform表名where条件;删除表里的某行信息 deletefrom表名;是删除整张表数据 ......
  • js.删除并获得点数
    链接:740.删除并获得点数-力扣(LeetCode)题目:给你一个整数数组 nums ,你可以对它进行一些操作。每次操作中,选择任意一个 nums[i] ,删除它并获得 nums[i] 的点数。之后,你必须删除 所有 等于 nums[i]-1 和 nums[i]+1 的元素。开始你拥有 0 个点数。返回你能......
  • 利用索引遍历二维数组
    文章目录一、两种二维数组初始化定义方法二、索引遍历的代码一、两种二维数组初始化定义方法intarr[3][5]={ {1,2,3,4,5}, {11,22,33,44,55}, {6,5,4,3,2} };第一种初始化方法比较直观,即大括号内包含3对一维数组,每对一维数组的长度都为5,这种方法适合于一维......
  • 如何轻松快速地删除iPhone上的相册?
    iPhone上的相册非常适合整理您拍摄或下载的照片和视频。有了它们,您可以根据事件、地点或人物存储您的记忆。一些第三方应用程序(例如Instagram或WhatsApp)会在您允许的情况下自动创建相册来存储已发送或下载的照片。由于iPhone上的相册太多,您可能需要删除一些相册以释放存......
  • 深入理解MySQL索引的底层原理和优化
    深入理解MySQL索引的底层原理和优化1.什么是索引索引是帮助MySQL高效获取数据的排好序的数据结构。用于提高查询性能,相当于书的目录。比如我们在读一本书的时候,首先是通过目录来定位到文章的页码,然后通过页码再来快速定位到具体的内容。MySQL中也是一样,在查询数据的时候,首先看......
  • 删除重复id的记录
    数据里面,id重复,创建时间不同 --新建字段repete_flag--针对重复id的数据,打标记updateyg_gate_base_bsetrepete_flag='REPETE'WHEREidIN(selectidfromyg_gate_base_bgroupbyidhavingcount(*)>1) select*fromyg_gate_base_bwhererepete_flag=......
  • PowerShell 重命名文件夹及删除空文件夹
    数据来源$urldata中的倒数第2列(子文件夹名称列)包含/、\等特殊字符某个文件夹重命名脚本foreach($iin0..100){#提取路径部分$basePath=($urldata[$i].split("`t")[-1]).trim()$subPath=($urldata[$i].split("`t")[-2].replace('"','')......