删除团队关联索引单元测试
理解了,如果你不能更改 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());
}
}
解释每个步骤
-
准备数据:
- 创建必要的输入数据,如
departmentId
和indexPatternNameList
。
- 创建必要的输入数据,如
-
模拟行为:
- 使用 Mockito 模拟
departmentMapper
和indexPatternService
的行为。
- 使用 Mockito 模拟
-
执行方法:
- 创建
DelDepartmentPermissionRequest
对象并调用departmentService.deleteDepartmentPermission
方法。
- 创建
-
断言结果:
- 使用
assertNotNull
确保响应对象不为空。 - 直接检查
response.isSuccess()
方法来验证响应对象的状态是否成功。 - 直接检查
response.getErrCode()
和response.getErrMsg()
方法来验证响应对象的错误码和错误信息是否符合预期。
- 使用
-
验证日志:
- 使用
verifyNoInteractions
方法确保在某些情况下,某些服务没有被调用。
- 使用
通过这些更新,我们确保了测试用例能够在不依赖 RespUtils
方法的情况下,正确验证 deleteDepartmentPermission
方法的行为。希望这些更新对你有所帮助!如果有任何问题或需要进一步的帮助,请随时告诉我。