-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add test for "forward:..." #42
Comments
Something like this? this.mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp")); |
That would be good. I also want to test the actual output from the forwarded resource (it's an API and the rest-docs should contain the proper response). Is there a way to do that? I think you showed me how to do once but I must have lost the knack. |
At the moment we only have an explicit option for async dispatches. Should probably add one for FORWARD and ERROR dispatches. Take a look at how MockMvcRequestBuilders#asyncDispatch is done. |
Something like this? public static RequestBuilder forward(MvcResult result) {
return new RequestBuilder() {
@Override
public MockHttpServletRequest buildRequest(
ServletContext servletContext) {
MockHttpServletRequest request = result.getRequest();
request.setRequestURI(result.getResponse().getForwardedUrl());
return request;
}
};
} and then @Test
public void getLocations() throws Exception {
final MvcResult result = this.mockMvc
.perform(MockMvcRequestBuilders.get("/")).andReturn();
this.mockMvc.perform(forward(result))
.andExpect(...);
} It seems clunky to me, and I'm not sure I care too much about shaving this yak. Would it not be possible to build this into the @Test
public void getLocations() throws Exception {
this.mockMvc.followForwards(true);
this.mockMvc
.perform(MockMvcRequestBuilders.get("/")).andExpect(...);
} |
Also, although the test passes, it doesn't work with restdocs (no snippets created). Cc @wilkinsona. UPDATE: cancel that, it works fine with restdocs. It's just the yak shaving that I would like to fix. |
Yes, we probably could do some such option. It wouldn't work well with JSP but apart from that yes. |
Or on the original builder somehow
I see a |
There is a test for a controller that uses "redirect:..." views but none for "forward:..." (and it seems to be non-obvious how to get the mock request dispatcher to dispatch back to the main servlet).
The text was updated successfully, but these errors were encountered: