Redirect testing in a Spring MVC application

Is there a way to test the functionality of the forward:/ view returned from Spring MVC in a JUnit test?

I use MockMvc functionality from Spring 3.2, and under certain circumstances my controller switches to another (by returning a name like forward:/pathHandledByController ).

It would be great to say that when this transfer took place, all @ModelAttribute from the second controller are applied, and everything happens correctly. Unfortunately, MockMvc allows me to claim that the return name of the view began with forward:/ .

Is there a way to test this without deploying the entire web application in something like Jetty? I have many services embedded in an MVC application, how can I create a web application using a separate Spring config (from src/test/resources ) with mocks of these services?

+4
source share
1 answer

You can use forwardedUrl :

  mockMvc.perform(get("/controller/path")) .andExpect(forwardedUrl("/forwarded/url")); 
+2
source

Source: https://habr.com/ru/post/1483306/


All Articles