forked from SasanLabs/VulnerableApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test for controller exception handler
- Loading branch information
1 parent
79d482c
commit 837c6f4
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/test/java/org/sasanlabs/controller/exception/ControllerExceptionHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.sasanlabs.controller.exception; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.sasanlabs.internal.utility.MessageBundle; | ||
import org.sasanlabs.service.exception.ExceptionStatusCodeEnum; | ||
import org.sasanlabs.service.exception.ServiceApplicationException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.context.request.WebRequest; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ControllerExceptionHandlerTest { | ||
|
||
@Mock private MessageBundle messageBundle; | ||
|
||
@Mock private WebRequest webRequest; | ||
|
||
@InjectMocks private ControllerExceptionHandler controllerExceptionHandler; | ||
|
||
@Test | ||
void shouldHandleControllerExceptions() { | ||
// Arrange | ||
when(messageBundle.getString(any(), any())).thenReturn("Exception occurred"); | ||
ServiceApplicationException serviceApplicationException = | ||
new ServiceApplicationException( | ||
ExceptionStatusCodeEnum.SYSTEM_ERROR, new NullPointerException("ex")); | ||
|
||
// Act | ||
ResponseEntity<String> responseEntity = | ||
controllerExceptionHandler.handleControllerExceptions( | ||
new ControllerException(serviceApplicationException), webRequest); | ||
|
||
// Assert | ||
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode()); | ||
assertEquals("Exception occurred", responseEntity.getBody()); | ||
verify(messageBundle).getString(any(), any()); | ||
} | ||
|
||
@Test | ||
void shouldHandleExceptions() { | ||
// Arrange | ||
when(messageBundle.getString(any(), any())).thenReturn("IO exception occurred"); | ||
|
||
// Act | ||
ResponseEntity<String> responseEntity = | ||
controllerExceptionHandler.handleExceptions( | ||
new IOException("IO operation failed"), webRequest); | ||
|
||
// Assert | ||
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode()); | ||
assertEquals("IO exception occurred", responseEntity.getBody()); | ||
verify(messageBundle).getString(any(), any()); | ||
} | ||
} |