@Test public void nullHandler() { Object handler = null; this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handler, null); assertNull("Exception can be resolved only if there is a HandlerMethod", mav); }
@Test public void setReturnValueHandlers() { HandlerMethodReturnValueHandler handler = new ModelMethodProcessor(); this.resolver.setReturnValueHandlers(Collections.singletonList(handler)); this.resolver.afterPropertiesSet(); assertMethodProcessorCount(RESOLVER_COUNT, 1); }
@Test public void setArgumentResolvers() { HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver(); this.resolver.setArgumentResolvers(Collections.singletonList(resolver)); this.resolver.afterPropertiesSet(); assertMethodProcessorCount(1, HANDLER_COUNT); }
@BeforeClass public static void setupOnce() { ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver(); resolver.afterPropertiesSet(); RESOLVER_COUNT = resolver.getArgumentResolvers().getResolvers().size(); HANDLER_COUNT = resolver.getReturnValueHandlers().getHandlers().size(); }
@Test public void setCustomArgumentResolvers() { HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver(); this.resolver.setCustomArgumentResolvers(Collections.singletonList(resolver)); this.resolver.afterPropertiesSet(); assertTrue(this.resolver.getArgumentResolvers().getResolvers().contains(resolver)); assertMethodProcessorCount(RESOLVER_COUNT + 1, HANDLER_COUNT); }
@Test public void setCustomReturnValueHandlers() { HandlerMethodReturnValueHandler handler = new ViewNameMethodReturnValueHandler(); this.resolver.setCustomReturnValueHandlers(Collections.singletonList(handler)); this.resolver.afterPropertiesSet(); assertTrue(this.resolver.getReturnValueHandlers().getHandlers().contains(handler)); assertMethodProcessorCount(RESOLVER_COUNT, HANDLER_COUNT + 1); }
@Test public void resolveNoExceptionHandlerForException() throws NoSuchMethodException { Exception npe = new NullPointerException(); HandlerMethod handlerMethod = new HandlerMethod(new IoExceptionController(), "handle"); this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, npe); assertNull("NPE should not have been handled", mav); }
@Test public void resolveExceptionControllerAdviceNoHandler() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyControllerAdviceConfig.class); this.resolver.setApplicationContext(ctx); this.resolver.afterPropertiesSet(); IllegalStateException ex = new IllegalStateException(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, null, ex); assertNotNull("Exception was not handled", mav); assertTrue(mav.isEmpty()); assertEquals("DefaultTestExceptionResolver: IllegalStateException", this.response.getContentAsString()); }
@Test public void resolveExceptionGlobalHandler() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class); this.resolver.setApplicationContext(ctx); this.resolver.afterPropertiesSet(); IllegalAccessException ex = new IllegalAccessException(); HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle"); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull("Exception was not handled", mav); assertTrue(mav.isEmpty()); assertEquals("AnotherTestExceptionResolver: IllegalAccessException", this.response.getContentAsString()); }
@Test // SPR-12605 public void resolveExceptionWithHandlerMethodArg() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class); this.resolver.setApplicationContext(ctx); this.resolver.afterPropertiesSet(); ArrayIndexOutOfBoundsException ex = new ArrayIndexOutOfBoundsException(); HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle"); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull("Exception was not handled", mav); assertTrue(mav.isEmpty()); assertEquals("HandlerMethod: handle", this.response.getContentAsString()); }
@Test public void resolveExceptionGlobalHandlerOrdered() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class); this.resolver.setApplicationContext(ctx); this.resolver.afterPropertiesSet(); IllegalStateException ex = new IllegalStateException(); HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle"); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull("Exception was not handled", mav); assertTrue(mav.isEmpty()); assertEquals("TestExceptionResolver: IllegalStateException", this.response.getContentAsString()); }
@Test public void resolveExceptionControllerAdviceHandler() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyControllerAdviceConfig.class); this.resolver.setApplicationContext(ctx); this.resolver.afterPropertiesSet(); IllegalStateException ex = new IllegalStateException(); HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle"); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull("Exception was not handled", mav); assertTrue(mav.isEmpty()); assertEquals("BasePackageTestExceptionResolver: IllegalStateException", this.response.getContentAsString()); }
@Test // SPR-16496 public void resolveExceptionControllerAdviceAgainstProxy() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyControllerAdviceConfig.class); this.resolver.setApplicationContext(ctx); this.resolver.afterPropertiesSet(); IllegalStateException ex = new IllegalStateException(); HandlerMethod handlerMethod = new HandlerMethod(new ProxyFactory(new ResponseBodyController()).getProxy(), "handle"); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull("Exception was not handled", mav); assertTrue(mav.isEmpty()); assertEquals("BasePackageTestExceptionResolver: IllegalStateException", this.response.getContentAsString()); }
@Test public void resolveExceptionModelAndView() throws NoSuchMethodException { IllegalArgumentException ex = new IllegalArgumentException("Bad argument"); HandlerMethod handlerMethod = new HandlerMethod(new ModelAndViewController(), "handle"); this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull(mav); assertFalse(mav.isEmpty()); assertEquals("errorView", mav.getViewName()); assertEquals("Bad argument", mav.getModel().get("detail")); }
@Test public void resolveExceptionResponseBody() throws UnsupportedEncodingException, NoSuchMethodException { IllegalArgumentException ex = new IllegalArgumentException(); HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle"); this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull(mav); assertTrue(mav.isEmpty()); assertEquals("IllegalArgumentException", this.response.getContentAsString()); }
@Test public void resolveExceptionWithAssertionErrorAsRootCause() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class); this.resolver.setApplicationContext(ctx); this.resolver.afterPropertiesSet(); AssertionError err = new AssertionError("argh"); FatalBeanException ex = new FatalBeanException("wrapped", err); HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle"); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull("Exception was not handled", mav); assertTrue(mav.isEmpty()); assertEquals(err.toString(), this.response.getContentAsString()); }
@Test public void resolveExceptionResponseWriter() throws Exception { IllegalArgumentException ex = new IllegalArgumentException(); HandlerMethod handlerMethod = new HandlerMethod(new ResponseWriterController(), "handle"); this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull(mav); assertTrue(mav.isEmpty()); assertEquals("IllegalArgumentException", this.response.getContentAsString()); }
@Test public void controllerAdviceWithNestedException() { StaticWebApplicationContext ctx = new StaticWebApplicationContext(); ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class); ctx.refresh(); ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver(); resolver.setApplicationContext(ctx); resolver.afterPropertiesSet(); IllegalStateException ex = new IllegalStateException(new ServletRequestBindingException("message")); assertNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex)); }
@Test // SPR-14651 public void resolveRedirectAttributesAtArgument() throws Exception { IllegalArgumentException ex = new IllegalArgumentException(); HandlerMethod handlerMethod = new HandlerMethod(new RedirectAttributesController(), "handle"); this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull(mav); assertEquals("redirect:/", mav.getViewName()); FlashMap flashMap = (FlashMap) this.request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE); assertNotNull("output FlashMap should exist", flashMap); assertEquals("IllegalArgumentException", flashMap.get("exceptionClassName")); }
@Test // SPR-13546 public void resolveExceptionModelAtArgument() throws Exception { IllegalArgumentException ex = new IllegalArgumentException(); HandlerMethod handlerMethod = new HandlerMethod(new ModelArgumentController(), "handle"); this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull(mav); assertEquals(1, mav.getModelMap().size()); assertEquals("IllegalArgumentException", mav.getModelMap().get("exceptionClassName")); }