@Test public void handleExceptionWithCustomExceptionHandler() { Method m = ReflectionUtils.findMethod(TestBean.class, "failWithVoid"); TestableAsyncUncaughtExceptionHandler exceptionHandler = new TestableAsyncUncaughtExceptionHandler(); BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class); processorDefinition.getPropertyValues().add("exceptionHandler", exceptionHandler); ConfigurableApplicationContext context = initContext(processorDefinition); ITestBean testBean = context.getBean("target", ITestBean.class); assertFalse("Handler should not have been called", exceptionHandler.isCalled()); testBean.failWithVoid(); exceptionHandler.await(3000); exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class); }
@Test public void exceptionHandlerThrowsUnexpectedException() { Method m = ReflectionUtils.findMethod(TestBean.class, "failWithVoid"); TestableAsyncUncaughtExceptionHandler exceptionHandler = new TestableAsyncUncaughtExceptionHandler(true); BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class); processorDefinition.getPropertyValues().add("exceptionHandler", exceptionHandler); processorDefinition.getPropertyValues().add("executor", new DirectExecutor()); ConfigurableApplicationContext context = initContext(processorDefinition); ITestBean testBean = context.getBean("target", ITestBean.class); assertFalse("Handler should not have been called", exceptionHandler.isCalled()); try { testBean.failWithVoid(); exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class); } catch (Exception e) { fail("No unexpected exception should have been received"); } }
@Test public void configuredThroughNamespace() { GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.load(new ClassPathResource("taskNamespaceTests.xml", getClass())); context.refresh(); ITestBean testBean = context.getBean("target", ITestBean.class); testBean.test(); testBean.await(3000); Thread asyncThread = testBean.getThread(); assertTrue(asyncThread.getName().startsWith("testExecutor")); TestableAsyncUncaughtExceptionHandler exceptionHandler = context.getBean("exceptionHandler", TestableAsyncUncaughtExceptionHandler.class); assertFalse("handler should not have been called yet", exceptionHandler.isCalled()); testBean.failWithVoid(); exceptionHandler.await(3000); Method m = ReflectionUtils.findMethod(TestBean.class, "failWithVoid"); exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class); context.close(); }