diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java index 6bb763bf026..fe1af58a09b 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java @@ -22,8 +22,6 @@ import javax.servlet.http.HttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.web.context.request.async.WebAsyncManager; -import org.springframework.web.context.request.async.WebAsyncUtils; import org.springframework.web.servlet.FlashMap; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; @@ -50,6 +48,8 @@ class DefaultMvcResult implements MvcResult { private Exception resolvedException; + private Object asyncResult; + private CountDownLatch asyncResultLatch; @@ -105,29 +105,23 @@ class DefaultMvcResult implements MvcResult { return RequestContextUtils.getOutputFlashMap(mockRequest); } - public void setAsyncResultLatch(CountDownLatch asyncResultLatch) { - this.asyncResultLatch = asyncResultLatch; + public void setAsyncResult(Object asyncResult) { + this.asyncResult = asyncResult; } public Object getAsyncResult() { HttpServletRequest request = this.mockRequest; if (request.isAsyncStarted()) { - - long timeout = request.getAsyncContext().getTimeout(); - if (!awaitAsyncResult(timeout)) { + if (!awaitAsyncResult(request)) { throw new IllegalStateException( - "Gave up waiting on async result from [" + this.handler + "] to complete"); - } - - WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.mockRequest); - if (asyncManager.hasConcurrentResult()) { - return asyncManager.getConcurrentResult(); + "Gave up waiting on async result from handler [" + this.handler + "] to complete"); } } - return null; + return this.asyncResult; } - private boolean awaitAsyncResult(long timeout) { + private boolean awaitAsyncResult(HttpServletRequest request) { + long timeout = request.getAsyncContext().getTimeout(); if (this.asyncResultLatch != null) { try { return this.asyncResultLatch.await(timeout, TimeUnit.MILLISECONDS); @@ -139,4 +133,8 @@ class DefaultMvcResult implements MvcResult { return true; } + public void setAsyncResultLatch(CountDownLatch asyncResultLatch) { + this.asyncResultLatch = asyncResultLatch; + } + } diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/TestDispatcherServlet.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/TestDispatcherServlet.java index 6a22d24f4af..8c57fae2475 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/TestDispatcherServlet.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/TestDispatcherServlet.java @@ -66,19 +66,21 @@ final class TestDispatcherServlet extends DispatcherServlet { super.service(request, response); } - private CountDownLatch registerAsyncInterceptors(HttpServletRequest request) { + private CountDownLatch registerAsyncInterceptors(final HttpServletRequest servletRequest) { final CountDownLatch asyncResultLatch = new CountDownLatch(1); - WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request); + WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(servletRequest); asyncManager.registerCallableInterceptor(KEY, new CallableProcessingInterceptorAdapter() { public void postProcess(NativeWebRequest request, Callable task, Object value) throws Exception { + getMvcResult(servletRequest).setAsyncResult(value); asyncResultLatch.countDown(); } }); asyncManager.registerDeferredResultInterceptor(KEY, new DeferredResultProcessingInterceptorAdapter() { public void postProcess(NativeWebRequest request, DeferredResult result, Object value) throws Exception { + getMvcResult(servletRequest).setAsyncResult(value); asyncResultLatch.countDown(); } });