Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Forwarding in a Spring MVC App

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?

like image 847
EngineerBetter_DJ Avatar asked Oct 05 '22 00:10

EngineerBetter_DJ


1 Answers

You can use forwardedUrl matcher:

 mockMvc.perform(get("/controller/path"))
    .andExpect(forwardedUrl("/forwarded/url"));
like image 162
mtyurt Avatar answered Oct 13 '22 10:10

mtyurt