### Question:
PrettyLogExceptionResolver extends SimpleMappingExceptionResolver { @Override protected void logException(Exception ex, HttpServletRequest request) { if (ex instanceof NotFoundException) { logger.info(getLogMessage(request, ex)); } else if (ex instanceof TypeMismatchException) { logger.info(getLogMessage(request, ex)); } else if (ex instanceof AccessDeniedException) { String url = request.getRequestURL().toString(); Principal principal = request.getUserPrincipal(); String user = principal != null ? principal.getName() : NOT_AUTHORIZED_USERNAME; String accessDeniedMessage = String.format(ACCESS_DENIED_MESSAGE, user, request.getMethod(), url); logger.info(accessDeniedMessage); } else { super.logException(ex, request); logger.info(getLogMessage(request, ex)); } }  @Override ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
                                         Object handler, Exception ex);  }### Answer:
@Test public void testLogExceptionWithIncomingNotFoundException() throws Exception { Log mockLog = replaceLoggerWithMock(prettyLogExceptionResolver); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); NotFoundException notFoundException = new NotFoundException("Entity not found"); String logMessage = String.format("[%s][%s][%s][%s]", request.getMethod(), request.getRequestURL().toString(), request.getHeader("Cookie"), "Entity not found"); request.setContent("".getBytes()); prettyLogExceptionResolver.logException(notFoundException, request); verify(mockLog).info(logMessage); }
@Test public void testLogExceptionWithIncomingTypeMismatchException() throws Exception { Log mockLog = replaceLoggerWithMock(prettyLogExceptionResolver); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); TypeMismatchException typeMismatchException = new TypeMismatchException("Not a number", Number.class); String logMessage = String.format("[%s][%s][%s][%s]", request.getMethod(), request.getRequestURL().toString(), request.getHeader("Cookie"), typeMismatchException.getMessage()); request.setContent("".getBytes()); prettyLogExceptionResolver.logException(typeMismatchException, request); verify(mockLog).info(logMessage); }
@Test public void testLogExceptionWithIncomingAccessDeniedException() throws Exception { Log mockLog = replaceLoggerWithMock(prettyLogExceptionResolver); AccessDeniedException accessDeniedException = new AccessDeniedException("Access denied"); MockHttpServletRequest request = new MockHttpServletRequest("POST", "/testing/url/42"); request.setServerName("testserver.com"); request.setServerPort(8080); request.setContent("12345".getBytes()); request.setUserPrincipal(new UsernamePasswordAuthenticationToken("username", "password")); prettyLogExceptionResolver.logException(accessDeniedException, request); verify(mockLog).info( "Access was denied for user [username] trying to POST http: }
@Test public void testLogExceptionWithoutNotFoundException() throws Exception { Log mockLog = replaceLoggerWithMock(prettyLogExceptionResolver); Exception exception = new Exception(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); request.setContent("".getBytes()); prettyLogExceptionResolver.logException(exception, request); verify(mockLog, times(1)).info(anyString()); }