HandlerExceptionResolverLoggingInterceptor implements
                                                        MethodInterceptor,
                                                        InitializingBean { @Override public Object invoke(MethodInvocation invocation) throws Throwable { Object returnObj = invocation.proceed(); if (returnObj == null) { return returnObj; } Object targetObject = invocation.getThis(); if (!(targetObject instanceof HandlerExceptionResolver)) { if (logger.isWarnEnabled()) { logger.warn( "target object does not implement the HandlerExceptionResolver interface. target object is '{}'.", targetObject.getClass().getName()); } return returnObj; } Exception exception = (Exception) invocation.getArguments()[3]; if (isTargetException(exception)) { HttpServletRequest request = (HttpServletRequest) invocation .getArguments()[0]; HttpServletResponse response = (HttpServletResponse) invocation .getArguments()[1]; Object handler = invocation.getArguments()[2]; log(exception, request, response, handler); } return returnObj; } HandlerExceptionResolverLoggingInterceptor(); void setExceptionLogger(ExceptionLogger exceptionLogger); void setIgnoreExceptions(
            Set<Class<? extends Exception>> ignoreExceptions); @Override Object invoke(MethodInvocation invocation); @Override void afterPropertiesSet();  }
@Test public void testInvoke_ResponseStatusExceptionResolver_responseCode_3xx() throws Throwable { NullPointerException occurException = new NullPointerException("null pointer exception."); HttpServletResponse mockResponse = mock(HttpServletResponse.class); ResponseStatusExceptionResolver resolver = new ResponseStatusExceptionResolver(); when(mockMethodInvocation.proceed()).thenReturn("viewname"); when(mockMethodInvocation.getThis()).thenReturn(resolver); when(mockResponse.getStatus()).thenReturn(300); when(mockMethodInvocation.getArguments()).thenReturn(new Object[] { null, mockResponse, null, occurException }); testTarget.invoke(mockMethodInvocation); verify(mockExceptionLogger, times(1)).info((Exception) any()); }
@Test public void testInvoke_SystemExceptionResolver_responseCode_4xx() throws Throwable { NullPointerException occurException = new NullPointerException("null pointer exception."); HttpServletResponse mockResponse = mock(HttpServletResponse.class); SystemExceptionResolver resolver = new SystemExceptionResolver(); when(mockMethodInvocation.proceed()).thenReturn("viewname"); when(mockMethodInvocation.getThis()).thenReturn(resolver); when(mockResponse.getStatus()).thenReturn(400); when(mockMethodInvocation.getArguments()).thenReturn(new Object[] { null, mockResponse, null, occurException }); testTarget.invoke(mockMethodInvocation); verify(mockExceptionLogger, times(1)).warn((Exception) any()); }
@Test public void testInvoke_responseCode_199() throws Throwable { NullPointerException occurException = new NullPointerException("null pointer exception."); HttpServletResponse mockResponse = mock(HttpServletResponse.class); ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver(); when(mockMethodInvocation.proceed()).thenReturn("viewname"); when(mockMethodInvocation.getThis()).thenReturn(resolver); when(mockResponse.getStatus()).thenReturn(199); when(mockMethodInvocation.getArguments()).thenReturn(new Object[] { null, mockResponse, null, occurException }); testTarget.invoke(mockMethodInvocation); verify(mockExceptionLogger, times(1)).info((Exception) any()); }
@Test public void testInvoke_responseCode_299() throws Throwable { NullPointerException occurException = new NullPointerException("null pointer exception."); HttpServletResponse mockResponse = mock(HttpServletResponse.class); DefaultHandlerExceptionResolver resolver = new DefaultHandlerExceptionResolver(); when(mockMethodInvocation.proceed()).thenReturn("viewname"); when(mockMethodInvocation.getThis()).thenReturn(resolver); when(mockResponse.getStatus()).thenReturn(299); when(mockMethodInvocation.getArguments()).thenReturn(new Object[] { null, mockResponse, null, occurException }); testTarget.invoke(mockMethodInvocation); verify(mockExceptionLogger, times(1)).info((Exception) any()); }
@Test public void testInvoke_responseCode_399() throws Throwable { NullPointerException occurException = new NullPointerException("null pointer exception."); HttpServletResponse mockResponse = mock(HttpServletResponse.class); ResponseStatusExceptionResolver resolver = new ResponseStatusExceptionResolver(); when(mockMethodInvocation.proceed()).thenReturn("viewname"); when(mockMethodInvocation.getThis()).thenReturn(resolver); when(mockResponse.getStatus()).thenReturn(399); when(mockMethodInvocation.getArguments()).thenReturn(new Object[] { null, mockResponse, null, occurException }); testTarget.invoke(mockMethodInvocation); verify(mockExceptionLogger, times(1)).info((Exception) any()); }
@Test public void testInvoke_responseCode_499() throws Throwable { NullPointerException occurException = new NullPointerException("null pointer exception."); HttpServletResponse mockResponse = mock(HttpServletResponse.class); SystemExceptionResolver resolver = new SystemExceptionResolver(); when(mockMethodInvocation.proceed()).thenReturn("viewname"); when(mockMethodInvocation.getThis()).thenReturn(resolver); when(mockResponse.getStatus()).thenReturn(499); when(mockMethodInvocation.getArguments()).thenReturn(new Object[] { null, mockResponse, null, occurException }); testTarget.invoke(mockMethodInvocation); verify(mockExceptionLogger, times(1)).warn((Exception) any()); }
@Test public void testInvoke_responseCode_599() throws Throwable { NullPointerException occurException = new NullPointerException("null pointer exception."); HttpServletResponse mockResponse = mock(HttpServletResponse.class); SystemExceptionResolver resolver = new SystemExceptionResolver(); when(mockMethodInvocation.proceed()).thenReturn("viewname"); when(mockMethodInvocation.getThis()).thenReturn(resolver); when(mockResponse.getStatus()).thenReturn(599); when(mockMethodInvocation.getArguments()).thenReturn(new Object[] { null, mockResponse, null, occurException }); testTarget.invoke(mockMethodInvocation); verify(mockExceptionLogger, times(1)).error((Exception) any()); }
@Test public void testInvoke_ExceptionHandlerExceptionResolver_responseCode_1xx() throws Throwable { NullPointerException occurException = new NullPointerException("null pointer exception."); HttpServletResponse mockResponse = mock(HttpServletResponse.class); ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver(); when(mockMethodInvocation.proceed()).thenReturn("viewname"); when(mockMethodInvocation.getThis()).thenReturn(resolver); when(mockResponse.getStatus()).thenReturn(100); when(mockMethodInvocation.getArguments()).thenReturn(new Object[] { null, mockResponse, null, occurException }); testTarget.invoke(mockMethodInvocation); verify(mockExceptionLogger, times(1)).info((Exception) any()); }
@Test public void testInvoke_DefaultHandlerExceptionResolver_responseCode_2xx() throws Throwable { NullPointerException occurException = new NullPointerException("null pointer exception."); HttpServletResponse mockResponse = mock(HttpServletResponse.class); DefaultHandlerExceptionResolver resolver = new DefaultHandlerExceptionResolver(); when(mockMethodInvocation.proceed()).thenReturn("viewname"); when(mockMethodInvocation.getThis()).thenReturn(resolver); when(mockResponse.getStatus()).thenReturn(200); when(mockMethodInvocation.getArguments()).thenReturn(new Object[] { null, mockResponse, null, occurException }); testTarget.invoke(mockMethodInvocation); verify(mockExceptionLogger, times(1)).info((Exception) any()); }