Skip to content

Commit

Permalink
Added test for controller exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
SampathKumarAmex committed Mar 14, 2022
1 parent 79d482c commit 837c6f4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ dependencies {
// https://mvnrepository.com/artifact/org.mockito/mockito-core
testImplementation group: 'org.mockito', name: 'mockito-core', version: '3.5.13'

// https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter
testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '3.5.13'

// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter', version: '5.7.0'

Expand Down
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());
}
}

0 comments on commit 837c6f4

Please sign in to comment.