6

Is there any way of testing the functionality of a forward:/ view being returned from a Spring MVC controller in a JUnit test?

I'm using the MockMvc functionality from Spring 3.2 and my controller under certain circumstances forwards to another (by means of returning a view name of forward:/pathHandledByController).

It'd be great to be able to assert that when this forwarding has taken place, all the @ModelAttributes from the second controller are applied and everything processes properly. Unfortunately MockMvc only lets me assert that the view name returned started with forward:/.

Is there any way I can test this without spinning up the whole web-app in something like Jetty? I've got lots of services plumbed into the MVC application, how would I create a web-app that uses separate Spring config (from src/test/resources) with mocks of these services?

DeejUK
  • 12,891
  • 19
  • 89
  • 169
  • This sounds like you are trying to test Spring itself rather than your webapp. If you are checking the viewName is as expected, then in my view you can trust Spring to take over from there, at least as far as unit testing goes. – Mark Chorley Jun 25 '13 at 09:09

1 Answers1

5

You can use forwardedUrl matcher:

 mockMvc.perform(get("/controller/path"))
    .andExpect(forwardedUrl("/forwarded/url"));
mtyurt
  • 3,369
  • 6
  • 38
  • 57