diff --git a/exception-handler/src/main/java/com/raychatter/common/annotation/AnnotationHandler.java b/exception-handler/src/main/java/com/raychatter/common/annotation/AnnotationHandler.java index 12ce0ca..14d9415 100644 --- a/exception-handler/src/main/java/com/raychatter/common/annotation/AnnotationHandler.java +++ b/exception-handler/src/main/java/com/raychatter/common/annotation/AnnotationHandler.java @@ -52,7 +52,7 @@ public class AnnotationHandler implements HandlerExceptionResolver { return new ModelAndView(); } - private ModelAndView respondWithDefault(final Exception thrownException, final HttpServletResponse response) throws IOException { + protected ModelAndView respondWithDefault(final Exception thrownException, final HttpServletResponse response) throws IOException { response.setContentType(MediaType.APPLICATION_XML_VALUE); response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); response.getWriter().write(formatDefaultMessage(thrownException)); diff --git a/exception-handler/src/test/java/com/raychatter/common/annotation/AnnotationHandlerTest.java b/exception-handler/src/test/java/com/raychatter/common/annotation/AnnotationHandlerTest.java index 83e35f4..2675cd9 100644 --- a/exception-handler/src/test/java/com/raychatter/common/annotation/AnnotationHandlerTest.java +++ b/exception-handler/src/test/java/com/raychatter/common/annotation/AnnotationHandlerTest.java @@ -240,6 +240,21 @@ public class AnnotationHandlerTest { verify(sut).handleException(mockAnnotation, expectedException, mockResponse); } + + @Test public void resolveException_ShouldReturnDefaultErrorMessage_WhenUncheckedExceptionIsGiven() throws Exception { + final NullPointerException expectedException = mock(NullPointerException.class); + + final HttpServletResponse mockResponse = mock(HttpServletResponse.class); + final PrintWriter mockPrinter = mock(PrintWriter.class); + when(mockResponse.getWriter()).thenReturn(mockPrinter); + + final AnnotationHandler sut = spy(new AnnotationHandler()); + doReturn(null).when(sut).getAnnotationFrom(expectedException); + + final ModelAndView view = sut.resolveException(null, mockResponse, null, expectedException); + + verify(sut).respondWithDefault(expectedException, mockResponse); + } } @ExceptionHandler()