Skip to content
This repository has been archived by the owner on Feb 5, 2022. It is now read-only.

Commit

Permalink
Add async dispatches to async tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Nov 6, 2012
1 parent 05fe57a commit 18da3dc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main/resources/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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"));
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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"));
}

}

0 comments on commit 18da3dc

Please sign in to comment.