How to fix the error when trying "No ModelAndView found"?

This class is at the top of my test hierarchy:

@TestPropertySource("/test.properties") @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public abstract class ApplicationAbstractTest { } 

And a few more test classes:

 @WebAppConfiguration @ActiveProfiles("mysql") abstract public class AbstractControllerTest extends ApplicationAbstractTest { protected MockMvc mockMvc; @Autowired private WebApplicationContext webApplicationContext; @PostConstruct private void postConstruct() { mockMvc = MockMvcBuilders .webAppContextSetup(webApplicationContext) .apply(springSecurity()) .build(); } } 

JsonUserServiceTest:

 @ActiveProfiles("json") public class JsonUserServiceTest extends ApplicationAbstractTest { @Before public void setUp() throws Exception { ... } } 

ContactControllerTest:

 public class ContactControllerTest extends AbstractControllerTest { @Test public void testGet() throws Exception { mockMvc.perform(get("/update-" + ID + "-contact") .with(userAuth(USER))) // .andExpect(status().isOk()) .andDo(print()) .andExpect(view().name("details")) .andExpect(forwardedUrl("/WEB-INF/jsp/details.jsp")); } } 

So, when I run ContactControllerTest along - it succeeds, and the print method shows me:

 Handler: Type = com.telecom.web.ContactController Method = public java.lang.String com.myApp.web.ContactController.details(java.lang.Integer,org.springframework.ui.ModelMap) 

But when I run all the tests, so the JsonUserServiceTest works first, the ContactControllerTest fails. And print shows:

 Handler: Type = null ... java.lang.AssertionError: No ModelAndView found 

What is wrong with the configuration? Or how to fix it?

UPD: at the same time, a test like this always works fine:

 public class UserControllerTest extends AbstractControllerTest { @Test public void testRegister() throws Exception { mockMvc.perform(get("/register")) .andDo(print()) .andExpect(view().name("profile")) .andExpect(forwardedUrl("/WEB-INF/jsp/profile.jsp")); } } 

UPD: There is a controller method that I am testing:

 @GetMapping("/update-{id}-contact") public String details(@PathVariable Integer id, ModelMap model) { Integer userId = AuthorizedUser.id(); LOG.info("get contact {} for User {}", id, userId); Contact contact = service.get(id, userId); model.addAttribute("contact", contact); return "details"; } 

I also have a bean:

 @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/WEB-INF/jsp/"); viewResolver.setSuffix(".jsp"); return viewResolver; } 

UPD: I tried setting up mockMvc in a separate class:

 @Configuration public class TestConfig { @Autowired private WebApplicationContext webApplicationContext; @Bean public MockMvc mockMvc() { return MockMvcBuilders .webAppContextSetup(webApplicationContext) .apply(springSecurity()) .build(); } } 

And added it here:

 @WebAppConfiguration @ContextConfiguration(classes = {TestConfig.class}) @ActiveProfiles("mysql") abstract public class AbstractControllerTest extends ApplicationAbstractTest { 

but i got:

java.lang.IllegalStateException: springSecurityFilterChain cannot be zero. Ensure that a Bean named springSecurityFilterChain that implements the filter is present or enters the filter to be used.

+5
source share
2 answers

The WARN message does not crash test cases. He simply says that the factory entity manager is registered twice. This will only be a problem if you group your application using the same Factory Entity Manager. To run a test test, this is not a concern.

The root cause of the test failure in these two lines

  .andExpect(view().name("details")) .andExpect(forwardedUrl("/WEB-INF/jsp/details.jsp")); 

Please check if the project has a "details" view and the forwarded URL is "/WEB-INF/jsp/details.jsp"

Update

Could you try this

 @Configuration public class TestConfig { @Autowired private Filter springSecurityFilterChain; @Autowired private WebApplicationContext webApplicationContext; @Bean public MockMvc mockMvc() { return MockMvcBuilders .webAppContextSetup(webApplicationContext) .apply(springSecurityFilterChain) .build(); } } 
+3
source

Create a configuration file that will initialize mock objects for your test cases. And put all the test classes.

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {TestConfig.class}) 

It will initialize all your mocking objects only once and then cache after that and reuse for all test cases.

Or, if you do not want to use a mocking configuration, you can directly pass the actual application configuration to ContextConfiguration as below

To configure annotation-based applications (here AppConfig and AppConfig2 are your configuration class)

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {AppConfig.class, AppConfig2.class}) 

To configure an xml-based application (here appConfig.xml and appConfig2.xml are your configuration files)

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:pathTo/appConfig.xml","classpath:pathTo/appConfig2.xml"}) 

Link: JUnit + Spring Integration Example

+3
source

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


All Articles