Source:
public void shutDown() throws Exception { DirectChannel requestChannel = new DirectChannel(); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override protected Object handleRequestMessage(Message<?> requestMessage) { try { latch2.countDown(); latch1.await(10, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } return requestMessage.getPayload().toString().toUpperCase(); } }; requestChannel.subscribe(handler); final HttpRequestHandlingController controller = new HttpRequestHandlingController(true); controller.setBeanFactory(mock(BeanFactory.class)); controller.setRequestChannel(requestChannel); controller.setViewName("foo"); controller.afterPropertiesSet(); controller.start(); final MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("POST"); request.setContent("hello".getBytes()); request.addHeader("Content-Type", "text/plain"); MockHttpServletResponse response = new MockHttpServletResponse(); final AtomicInteger active = new AtomicInteger(); final AtomicBoolean expected503 = new AtomicBoolean(); Executors.newSingleThreadExecutor().execute(() -> { try { latch2.await(10, TimeUnit.SECONDS); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } active.set(controller.beforeShutdown()); try { MockHttpServletResponse response1 = new MockHttpServletResponse(); controller.handleRequest(request, response1); expected503.set(response1.getStatus() == HttpStatus.SERVICE_UNAVAILABLE.value()); latch1.countDown(); } catch (Exception e) { e.printStackTrace(); } }); ModelAndView modelAndView = controller.handleRequest(request, response); assertEquals(1, active.get()); assertTrue(expected503.get()); assertEquals("foo", modelAndView.getViewName()); assertEquals(1, modelAndView.getModel().size()); Object reply = modelAndView.getModel().get("reply"); assertNotNull(reply); assertEquals("HELLO", reply); }

Target:
public void shutDown() throws Exception { DirectChannel requestChannel = new DirectChannel(); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override protected Object handleRequestMessage(Message<?> requestMessage) { try { latch2.countDown(); latch1.await(10, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } return requestMessage.getPayload().toString().toUpperCase(); } }; requestChannel.subscribe(handler); final HttpRequestHandlingController controller = new HttpRequestHandlingController(true); controller.setBeanFactory(mock(BeanFactory.class)); controller.setRequestChannel(requestChannel); controller.setViewName("foo"); controller.afterPropertiesSet(); controller.start(); final MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("POST"); request.setContent("hello".getBytes()); request.addHeader("Content-Type", "text/plain"); MockHttpServletResponse response = new MockHttpServletResponse(); final AtomicInteger active = new AtomicInteger(); final AtomicBoolean expected503 = new AtomicBoolean(); Executors.newSingleThreadExecutor().execute(() -> { try { latch2.await(10, TimeUnit.SECONDS); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } active.set(controller.beforeShutdown()); try { MockHttpServletResponse response1 = new MockHttpServletResponse(); controller.handleRequest(request, response1); expected503.set(response1.getStatus() == HttpStatus.SERVICE_UNAVAILABLE.value()); latch1.countDown(); } catch (Exception e) { LogFactory.getLog(getClass()).error("Async handleRequest failed", e); } }); ModelAndView modelAndView = controller.handleRequest(request, response); assertEquals(1, active.get()); assertTrue(expected503.get()); assertEquals("foo", modelAndView.getViewName()); assertEquals(1, modelAndView.getModel().size()); Object reply = modelAndView.getModel().get("reply"); assertNotNull(reply); assertEquals("HELLO", reply); }