test(api): 补齐聚合接口控制器测试并修复参数异常语义
This commit is contained in:
@@ -6,6 +6,8 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
@@ -17,6 +19,15 @@ public class GlobalExceptionHandler {
|
||||
return buildResponse(HttpStatus.BAD_REQUEST, exception.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler({
|
||||
MissingServletRequestParameterException.class,
|
||||
MethodArgumentTypeMismatchException.class
|
||||
})
|
||||
public ResponseEntity<RequestResult<Void>> handleBadRequest(Exception exception) {
|
||||
log.warn("GlobalExceptionHandler.handleBadRequest, message={}", exception.getMessage(), exception);
|
||||
return buildResponse(HttpStatus.BAD_REQUEST, "请求参数不合法");
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<RequestResult<Void>> handleException(Exception exception) {
|
||||
log.error("GlobalExceptionHandler.handleException", exception);
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.bruce.common.controller;
|
||||
|
||||
import com.bruce.common.dto.response.SysEnumResponse;
|
||||
import com.bruce.common.handler.GlobalExceptionHandler;
|
||||
import com.bruce.common.service.ISysEnumService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
/**
|
||||
* 验证系统枚举控制器的基础接口契约,确保前端依赖的 RequestResult 结构稳定。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class SysEnumControllerTests {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Mock
|
||||
private ISysEnumService sysEnumService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
SysEnumController controller = new SysEnumController();
|
||||
ReflectionTestUtils.setField(controller, "sysEnumService", sysEnumService);
|
||||
mockMvc = MockMvcBuilders.standaloneSetup(controller)
|
||||
.setControllerAdvice(new GlobalExceptionHandler())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryForManagementShouldReturnStructuredResult() throws Exception {
|
||||
SysEnumResponse response = new SysEnumResponse();
|
||||
response.setId(1L);
|
||||
response.setCatalog("common");
|
||||
response.setType("enable_status");
|
||||
response.setName("启用");
|
||||
response.setValue(1);
|
||||
response.setStrvalue("ENABLED");
|
||||
|
||||
when(sysEnumService.listForManagement(any())).thenReturn(List.of(response));
|
||||
|
||||
mockMvc.perform(post("/api/sys-enum/queryForManagement")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("""
|
||||
{
|
||||
"catalog": "common",
|
||||
"type": "enable_status"
|
||||
}
|
||||
"""))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.resultcode").value("0"))
|
||||
.andExpect(jsonPath("$.data[0].catalog").value("common"))
|
||||
.andExpect(jsonPath("$.data[0].strvalue").value("ENABLED"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void detailShouldReturnBadRequestWhenIdMissing() throws Exception {
|
||||
mockMvc.perform(get("/api/sys-enum/detail"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user