优化
This commit is contained in:
@@ -7,6 +7,9 @@ import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.validation.BeanPropertyBindingResult;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
@@ -43,6 +46,21 @@ class GlobalExceptionHandlerTest {
|
||||
assertThat(response.getBody().code()).isEqualTo(ResultCode.BAD_REQUEST.code());
|
||||
}
|
||||
|
||||
@Test
|
||||
void validationDetailsAreIncludedInMessageAndData() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(new Object(), "request");
|
||||
bindingResult.addError(new FieldError("request", "carrierId", "不能为null"));
|
||||
bindingResult.addError(new FieldError("request", "carrierName", "不能为空"));
|
||||
|
||||
var response = handler.handleBinding(new BindException(bindingResult), request);
|
||||
|
||||
assertThat(response.getBody().code()).isEqualTo(ResultCode.VALIDATION_FAILED.code());
|
||||
assertThat(response.getBody().message()).isEqualTo("carrierId: 不能为null;carrierName: 不能为空");
|
||||
assertThat(response.getBody().data()).containsEntry("carrierId", "不能为null")
|
||||
.containsEntry("carrierName", "不能为空");
|
||||
}
|
||||
|
||||
@Test
|
||||
void recordsBusinessFailureMetricByFiniteResultCode() {
|
||||
SimpleMeterRegistry registry = new SimpleMeterRegistry();
|
||||
|
||||
Reference in New Issue
Block a user