Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add async dispatches to async tests

  • Loading branch information...
commit 18da3dcab0fd24188d3d054bab38860feb252213 1 parent 05fe57a
@rstoyanchev rstoyanchev authored
View
2  src/main/resources/log4j.xml
@@ -6,7 +6,7 @@
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="%-5p: %c - %m%n" />
+ <param name="ConversionPattern" value="%d{HH:mm:ss} [%t] %c{1} - %m%n" />
</layout>
</appender>
View
39 src/test/java/org/springframework/samples/mvc/async/CallableControllerTests.java
@@ -1,7 +1,11 @@
package org.springframework.samples.mvc.async;
import static org.hamcrest.Matchers.instanceOf;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
@@ -12,6 +16,7 @@
import org.springframework.samples.mvc.AbstractContextControllerTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.MvcResult;
@RunWith(SpringJUnit4ClassRunner.class)
public class CallableControllerTests extends AbstractContextControllerTests {
@@ -25,26 +30,42 @@ public void setup() throws Exception {
@Test
public void responseBody() throws Exception {
- this.mockMvc.perform(get("/async/callable/response-body"))
- .andExpect(status().isOk())
+ MvcResult mvcResult = this.mockMvc.perform(get("/async/callable/response-body"))
.andExpect(request().asyncStarted())
- .andExpect(request().asyncResult("Callable result"));
+ .andExpect(request().asyncResult("Callable result"))
+ .andReturn();
+
+ this.mockMvc.perform(asyncDispatch(mvcResult))
+ .andExpect(status().isOk())
+ .andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
+ .andExpect(content().string("Callable result"));
}
@Test
public void view() throws Exception {
- this.mockMvc.perform(get("/async/callable/view"))
- .andExpect(status().isOk())
+ MvcResult mvcResult = this.mockMvc.perform(get("/async/callable/view"))
.andExpect(request().asyncStarted())
- .andExpect(request().asyncResult("views/html"));
+ .andExpect(request().asyncResult("views/html"))
+ .andReturn();
+
+ this.mockMvc.perform(asyncDispatch(mvcResult))
+ .andExpect(status().isOk())
+ .andExpect(forwardedUrl("/WEB-INF/views/views/html.jsp"))
+ .andExpect(model().attribute("foo", "bar"))
+ .andExpect(model().attribute("fruit", "apple"));
}
@Test
public void exception() throws Exception {
- this.mockMvc.perform(get("/async/callable/exception"))
- .andExpect(status().isOk())
+ MvcResult mvcResult = this.mockMvc.perform(get("/async/callable/exception"))
.andExpect(request().asyncStarted())
- .andExpect(request().asyncResult(instanceOf(IllegalStateException.class)));
+ .andExpect(request().asyncResult(instanceOf(IllegalStateException.class)))
+ .andReturn();
+
+ this.mockMvc.perform(asyncDispatch(mvcResult))
+ .andExpect(status().isOk())
+ .andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
+ .andExpect(content().string("Handled exception: Callable error"));
}
}
View
32 src/test/java/org/springframework/samples/mvc/async/DeferredResultControllerTests.java
@@ -1,8 +1,11 @@
package org.springframework.samples.mvc.async;
import static org.hamcrest.Matchers.instanceOf;
-import static org.junit.Assert.*;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
@@ -28,31 +31,44 @@ public void setup() throws Exception {
@Test
public void responseBody() throws Exception {
- this.mockMvc.perform(get("/async/deferred-result/response-body"))
+ MvcResult mvcResult = this.mockMvc.perform(get("/async/deferred-result/response-body"))
.andExpect(status().isOk())
.andExpect(request().asyncStarted())
- .andExpect(request().asyncResult("Deferred result"));
+ .andExpect(request().asyncResult("Deferred result"))
+ .andReturn();
+
+ this.mockMvc.perform(asyncDispatch(mvcResult))
+ .andExpect(status().isOk())
+ .andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
+ .andExpect(content().string("Deferred result"));
}
@Test
public void view() throws Exception {
-
MvcResult mvcResult = this.mockMvc.perform(get("/async/deferred-result/model-and-view"))
.andExpect(status().isOk())
.andExpect(request().asyncStarted())
.andExpect(request().asyncResult(instanceOf(ModelAndView.class)))
.andReturn();
- ModelAndView mav = (ModelAndView) mvcResult.getAsyncResult();
- assertEquals("views/html", mav.getViewName());
+ this.mockMvc.perform(asyncDispatch(mvcResult))
+ .andExpect(status().isOk())
+ .andExpect(forwardedUrl("/WEB-INF/views/views/html.jsp"))
+ .andExpect(model().attributeExists("javaBean"));
}
@Test
public void exception() throws Exception {
- this.mockMvc.perform(get("/async/deferred-result/exception"))
+ MvcResult mvcResult = this.mockMvc.perform(get("/async/deferred-result/exception"))
.andExpect(status().isOk())
.andExpect(request().asyncStarted())
- .andExpect(request().asyncResult(instanceOf(IllegalStateException.class)));
+ .andExpect(request().asyncResult(instanceOf(IllegalStateException.class)))
+ .andReturn();
+
+ this.mockMvc.perform(asyncDispatch(mvcResult))
+ .andExpect(status().isOk())
+ .andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
+ .andExpect(content().string("Handled exception: DeferredResult error"));
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.