diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java index f674878e30..214600310d 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.springframework.test.web.servlet.result; import java.util.Enumeration; import java.util.Map; + import javax.servlet.http.HttpServletRequest; import org.springframework.http.HttpHeaders; @@ -127,8 +128,15 @@ public class PrintingResultHandler implements ResultHandler { protected void printAsyncResult(MvcResult result) throws Exception { HttpServletRequest request = result.getRequest(); - this.printer.printValue("Was async started", request.isAsyncStarted()); - this.printer.printValue("Async result", (request.isAsyncStarted() ? result.getAsyncResult(0) : null)); + this.printer.printValue("Async started", request.isAsyncStarted()); + Object asyncResult = null; + try { + asyncResult = result.getAsyncResult(0); + } + catch (IllegalStateException ex) { + // Not set + } + this.printer.printValue("Async result", asyncResult); } /** Print the handler */ diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java index 6cb606df16..8437c4579b 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java @@ -16,6 +16,7 @@ package org.springframework.test.web.servlet.samples.standalone; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; @@ -56,7 +57,7 @@ public class AsyncTests { this.asyncController = new AsyncController(); this.mockMvc = standaloneSetup(this.asyncController).build(); } - + @Test public void testCallable() throws Exception { @@ -112,6 +113,25 @@ public class AsyncTests { .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); } + // SPR-12735 + + @Test + public void testPrintAsyncResult() throws Exception { + MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResult", "true")) + .andDo(print()) + .andExpect(request().asyncStarted()) + .andReturn(); + + this.asyncController.onMessage("Joe"); + + this.mockMvc.perform(asyncDispatch(mvcResult)) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); + } + + @Controller private static class AsyncController {