test: 补充Handler层测试用例提升分支覆盖率
- SysUserHandler: 新增7个测试(分页、创建、更新、批量操作) - SysRoleHandler: 新增3个测试(分页、更新NotFound) - MenuHandler: 新增3个测试(默认参数、null参数、更新NotFound) - 覆盖更多分支逻辑和边界条件 - 提升Handler层测试完整性
This commit is contained in:
+52
@@ -135,6 +135,22 @@ class MenuHandlerTest {
|
||||
verify(menuService).findByParentId(0L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetMenusByParent_Default() {
|
||||
when(menuService.findByParentId(0L)).thenReturn(Flux.just(testMenu));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.build();
|
||||
Mono<ServerResponse> response = menuHandler.getMenusByParent(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(menuService).findByParentId(0L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetMenusByType() {
|
||||
when(menuService.findAll()).thenReturn(Flux.just(testMenu));
|
||||
@@ -152,6 +168,22 @@ class MenuHandlerTest {
|
||||
verify(menuService).findAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetMenusByType_Null() {
|
||||
when(menuService.findAll()).thenReturn(Flux.just(testMenu));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.build();
|
||||
Mono<ServerResponse> response = menuHandler.getMenusByType(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(menuService).findAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateMenu() {
|
||||
MenuCreateRequest createRequest = new MenuCreateRequest();
|
||||
@@ -203,6 +235,26 @@ class MenuHandlerTest {
|
||||
verify(menuService).updateMenu(any(UpdateMenuCommand.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateMenu_NotFound() {
|
||||
MenuUpdateRequest updateRequest = new MenuUpdateRequest();
|
||||
updateRequest.setMenuName("更新菜单");
|
||||
|
||||
when(menuService.updateMenu(any(UpdateMenuCommand.class))).thenReturn(Mono.empty());
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.pathVariable("id", "999")
|
||||
.body(Mono.just(updateRequest));
|
||||
Mono<ServerResponse> response = menuHandler.updateMenu(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.NOT_FOUND)
|
||||
.verifyComplete();
|
||||
|
||||
verify(menuService).updateMenu(any(UpdateMenuCommand.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteMenu() {
|
||||
when(menuService.deleteMenu(1L)).thenReturn(Mono.empty());
|
||||
|
||||
+74
@@ -64,6 +64,60 @@ class SysRoleHandlerTest {
|
||||
verify(roleService).findAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRolesByPage() {
|
||||
cn.novalon.manage.common.dto.PageResponse<SysRole> pageResponse =
|
||||
new cn.novalon.manage.common.dto.PageResponse<>();
|
||||
pageResponse.setContent(java.util.List.of(testRole));
|
||||
pageResponse.setTotalElements(1L);
|
||||
pageResponse.setTotalPages(1);
|
||||
pageResponse.setCurrentPage(0);
|
||||
pageResponse.setPageSize(10);
|
||||
|
||||
when(roleService.findRolesByPage(any(cn.novalon.manage.common.dto.PageRequest.class)))
|
||||
.thenReturn(Mono.just(pageResponse));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("page", "0")
|
||||
.queryParam("size", "10")
|
||||
.queryParam("sort", "id")
|
||||
.queryParam("order", "asc")
|
||||
.build();
|
||||
Mono<ServerResponse> response = roleHandler.getRolesByPage(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(roleService).findRolesByPage(any(cn.novalon.manage.common.dto.PageRequest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRolesByPage_WithKeyword() {
|
||||
cn.novalon.manage.common.dto.PageResponse<SysRole> pageResponse =
|
||||
new cn.novalon.manage.common.dto.PageResponse<>();
|
||||
pageResponse.setContent(java.util.List.of(testRole));
|
||||
pageResponse.setTotalElements(1L);
|
||||
|
||||
when(roleService.findRolesByPage(any(cn.novalon.manage.common.dto.PageRequest.class)))
|
||||
.thenReturn(Mono.just(pageResponse));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("page", "0")
|
||||
.queryParam("size", "10")
|
||||
.queryParam("keyword", "admin")
|
||||
.build();
|
||||
Mono<ServerResponse> response = roleHandler.getRolesByPage(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(roleService).findRolesByPage(any(cn.novalon.manage.common.dto.PageRequest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRoleCount() {
|
||||
when(roleService.count()).thenReturn(Mono.just(5L));
|
||||
@@ -209,6 +263,26 @@ class SysRoleHandlerTest {
|
||||
verify(roleService).updateRole(any(UpdateRoleCommand.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateRole_NotFound() {
|
||||
RoleUpdateRequest updateRequest = new RoleUpdateRequest();
|
||||
updateRequest.setRoleName("UPDATED_ROLE");
|
||||
|
||||
when(roleService.updateRole(any(UpdateRoleCommand.class))).thenReturn(Mono.empty());
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.pathVariable("id", "999")
|
||||
.body(Mono.just(updateRequest));
|
||||
Mono<ServerResponse> response = roleHandler.updateRole(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.NOT_FOUND)
|
||||
.verifyComplete();
|
||||
|
||||
verify(roleService).updateRole(any(UpdateRoleCommand.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteRole() {
|
||||
when(roleService.logicalDeleteRole(1L)).thenReturn(Mono.just(testRole));
|
||||
|
||||
+151
@@ -241,4 +241,155 @@ class SysUserHandlerTest {
|
||||
|
||||
verify(userService).existsByEmail("test@example.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUsersByPage() {
|
||||
cn.novalon.manage.common.dto.PageResponse<SysUser> pageResponse =
|
||||
new cn.novalon.manage.common.dto.PageResponse<>();
|
||||
pageResponse.setContent(List.of(testUser));
|
||||
pageResponse.setTotalElements(1L);
|
||||
pageResponse.setTotalPages(1);
|
||||
pageResponse.setCurrentPage(0);
|
||||
pageResponse.setPageSize(10);
|
||||
|
||||
when(userService.findUsersByPage(any(cn.novalon.manage.common.dto.PageRequest.class)))
|
||||
.thenReturn(Mono.just(pageResponse));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("page", "0")
|
||||
.queryParam("size", "10")
|
||||
.queryParam("sort", "id")
|
||||
.queryParam("order", "asc")
|
||||
.build();
|
||||
Mono<ServerResponse> response = userHandler.getUsersByPage(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(userService).findUsersByPage(any(cn.novalon.manage.common.dto.PageRequest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUsersByPage_WithKeyword() {
|
||||
cn.novalon.manage.common.dto.PageResponse<SysUser> pageResponse =
|
||||
new cn.novalon.manage.common.dto.PageResponse<>();
|
||||
pageResponse.setContent(List.of(testUser));
|
||||
pageResponse.setTotalElements(1L);
|
||||
|
||||
when(userService.findUsersByPage(any(cn.novalon.manage.common.dto.PageRequest.class)))
|
||||
.thenReturn(Mono.just(pageResponse));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("page", "0")
|
||||
.queryParam("size", "10")
|
||||
.queryParam("keyword", "test")
|
||||
.build();
|
||||
Mono<ServerResponse> response = userHandler.getUsersByPage(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(userService).findUsersByPage(any(cn.novalon.manage.common.dto.PageRequest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateUser() {
|
||||
UserRegisterRequest registerRequest = new UserRegisterRequest();
|
||||
registerRequest.setUsername("newuser");
|
||||
registerRequest.setPassword("Password123!");
|
||||
registerRequest.setEmail("new@example.com");
|
||||
|
||||
when(userService.createUser(any(CreateUserCommand.class))).thenReturn(Mono.just(testUser));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.body(Mono.just(registerRequest));
|
||||
Mono<ServerResponse> response = userHandler.createUser(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.CREATED)
|
||||
.verifyComplete();
|
||||
|
||||
verify(userService).createUser(any(CreateUserCommand.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateUser() {
|
||||
UserUpdateRequest updateRequest = new UserUpdateRequest();
|
||||
updateRequest.setEmail("updated@example.com");
|
||||
updateRequest.setRoleId(2L);
|
||||
updateRequest.setStatus(0);
|
||||
|
||||
when(userService.updateUser(any(UpdateUserCommand.class))).thenReturn(Mono.just(testUser));
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.pathVariable("id", "1")
|
||||
.body(Mono.just(updateRequest));
|
||||
Mono<ServerResponse> response = userHandler.updateUser(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
|
||||
verify(userService).updateUser(any(UpdateUserCommand.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateUser_NotFound() {
|
||||
UserUpdateRequest updateRequest = new UserUpdateRequest();
|
||||
updateRequest.setEmail("updated@example.com");
|
||||
|
||||
when(userService.updateUser(any(UpdateUserCommand.class))).thenReturn(Mono.empty());
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.pathVariable("id", "999")
|
||||
.body(Mono.just(updateRequest));
|
||||
Mono<ServerResponse> response = userHandler.updateUser(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.NOT_FOUND)
|
||||
.verifyComplete();
|
||||
|
||||
verify(userService).updateUser(any(UpdateUserCommand.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLogicalDeleteUsers() {
|
||||
List<Long> ids = List.of(1L, 2L, 3L);
|
||||
when(userService.logicalDeleteUsers(anyList())).thenReturn(Mono.empty());
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.body(Mono.just(ids));
|
||||
Mono<ServerResponse> response = userHandler.logicalDeleteUsers(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.NO_CONTENT)
|
||||
.verifyComplete();
|
||||
|
||||
verify(userService).logicalDeleteUsers(anyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRestoreUsers() {
|
||||
List<Long> ids = List.of(1L, 2L, 3L);
|
||||
when(userService.restoreUsers(anyList())).thenReturn(Mono.empty());
|
||||
|
||||
ServerRequest request = MockServerRequest.builder()
|
||||
.body(Mono.just(ids));
|
||||
Mono<ServerResponse> response = userHandler.restoreUsers(request);
|
||||
|
||||
StepVerifier.create(response)
|
||||
.expectNextMatches(serverResponse ->
|
||||
serverResponse.statusCode() == HttpStatus.NO_CONTENT)
|
||||
.verifyComplete();
|
||||
|
||||
verify(userService).restoreUsers(anyList());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user