diff --git a/pom.xml b/pom.xml index 4533295..e547396 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,7 @@ 21 1.5.5.Final 3.14.0 + 0.2.0 @@ -108,15 +109,29 @@ - org.springframework.boot - spring-boot-maven-plugin + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} - - + 17 + 17 + + + org.mapstruct + mapstruct-processor + ${mapstruct.version} + + org.projectlombok lombok - - + ${lombok.version} + + + org.projectlombok + lombok-mapstruct-binding + ${lombok-mapstruct-binding.version} + + diff --git a/src/main/java/com/springboot/ratelimiter/common/exception/GlobalExceptionHandler.java b/src/main/java/com/springboot/ratelimiter/common/exception/GlobalExceptionHandler.java index 805137a..fd09e83 100644 --- a/src/main/java/com/springboot/ratelimiter/common/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/springboot/ratelimiter/common/exception/GlobalExceptionHandler.java @@ -53,6 +53,7 @@ protected ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotV return new ResponseEntity<>(customError, HttpStatus.BAD_REQUEST); } + @ExceptionHandler(ConstraintViolationException.class) protected ResponseEntity handlePathVariableErrors(final ConstraintViolationException constraintViolationException) { diff --git a/src/test/java/com/springboot/ratelimiter/common/exception/GlobalExceptionHandlerTest.java b/src/test/java/com/springboot/ratelimiter/common/exception/GlobalExceptionHandlerTest.java index 287ac68..fd6603a 100644 --- a/src/test/java/com/springboot/ratelimiter/common/exception/GlobalExceptionHandlerTest.java +++ b/src/test/java/com/springboot/ratelimiter/common/exception/GlobalExceptionHandlerTest.java @@ -30,11 +30,13 @@ class GlobalExceptionHandlerTest extends AbstractRestControllerTest { private GlobalExceptionHandler globalExceptionHandler; @Test - void givenMethodArgumentNotValidException_handleMethodArgumentNotValid_throwCustomError() throws NoSuchMethodException { + void givenMethodArgumentNotValidException_handleMethodArgumentNotValid_throwCustomError() { + // Given - MethodArgumentNotValidException mockException = getMethodArgumentNotValidException(); + MethodParameter mockParameter = mock(MethodParameter.class); + BindingResult mockBindingResult = mock(BindingResult.class); + when(mockBindingResult.getAllErrors()).thenReturn(Collections.emptyList()); - // When CustomError expectedError = CustomError.builder() .time(LocalDateTime.now()) .httpStatus(HttpStatus.BAD_REQUEST) @@ -43,6 +45,9 @@ void givenMethodArgumentNotValidException_handleMethodArgumentNotValid_throwCust .subErrors(Collections.emptyList()) .build(); + // When + MethodArgumentNotValidException mockException = new MethodArgumentNotValidException(mockParameter, mockBindingResult); + // Then ResponseEntity responseEntity = globalExceptionHandler.handleMethodArgumentNotValid( mockException, new HttpHeaders(), HttpStatus.BAD_REQUEST, null); @@ -52,14 +57,6 @@ void givenMethodArgumentNotValidException_handleMethodArgumentNotValid_throwCust } - private MethodArgumentNotValidException getMethodArgumentNotValidException() throws NoSuchMethodException { - Method method = GlobalExceptionHandlerTest.class.getDeclaredMethod("handleMethodArgumentNotValid"); - int parameterIndex = -1; - - MethodParameter mockParameter = new MethodParameter(method, parameterIndex); - BindingResult mockBindingResult = new BeanPropertyBindingResult(null, ""); - return new MethodArgumentNotValidException(mockParameter, mockBindingResult); - } @Test void givenConstraintViolationException_whenHandlePathVariableErrors_throwCustomError() { @@ -74,7 +71,7 @@ void givenConstraintViolationException_whenHandlePathVariableErrors_throwCustomE .message("must not be null") .field("") .value("invalid value") - .type(mockViolation.getRootBeanClass().getSimpleName()) + .type("String") // Default to String if getRootBeanClass() is null .build(); CustomError expectedError = CustomError.builder() @@ -87,11 +84,10 @@ void givenConstraintViolationException_whenHandlePathVariableErrors_throwCustomE // When when(mockViolation.getMessage()).thenReturn("must not be null"); - - when(mockPath.toString()).thenReturn(""); when(mockViolation.getPropertyPath()).thenReturn(mockPath); + when(mockPath.toString()).thenReturn("field"); when(mockViolation.getInvalidValue()).thenReturn("invalid value"); - when(mockViolation.getRootBeanClass()).thenReturn(String.class); + when(mockViolation.getRootBeanClass()).thenReturn(String.class); // Ensure this does not return null // Then ResponseEntity responseEntity = globalExceptionHandler.handlePathVariableErrors(mockException);