Skip to content

Commit

Permalink
Task 12 : Revise GlobalExceptionHandler, GlobalExceptionHandlerTest a…
Browse files Browse the repository at this point in the history
…nd pom.xml
  • Loading branch information
Rapter1990 committed Jun 28, 2024
1 parent 5d0445b commit 715b596
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
27 changes: 21 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<java.version>21</java.version>
<mapstruct.version>1.5.5.Final</mapstruct.version>
<apache.common.lang3.version>3.14.0</apache.common.lang3.version>
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
</properties>
<dependencies>

Expand Down Expand Up @@ -108,15 +109,29 @@
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<excludes>
<exclude>
<source>17</source>
<target>17</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok-mapstruct-binding.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotV
return new ResponseEntity<>(customError, HttpStatus.BAD_REQUEST);
}


@ExceptionHandler(ConstraintViolationException.class)
protected ResponseEntity<Object> handlePathVariableErrors(final ConstraintViolationException constraintViolationException) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -43,6 +45,9 @@ void givenMethodArgumentNotValidException_handleMethodArgumentNotValid_throwCust
.subErrors(Collections.emptyList())
.build();

// When
MethodArgumentNotValidException mockException = new MethodArgumentNotValidException(mockParameter, mockBindingResult);

// Then
ResponseEntity<Object> responseEntity = globalExceptionHandler.handleMethodArgumentNotValid(
mockException, new HttpHeaders(), HttpStatus.BAD_REQUEST, null);
Expand All @@ -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() {
Expand All @@ -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()
Expand All @@ -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<Object> responseEntity = globalExceptionHandler.handlePathVariableErrors(mockException);
Expand Down

0 comments on commit 715b596

Please sign in to comment.