Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC test with MockMvc

I'm trying to run a test for testing a Spring MVC controller. The test compile and runs, but my problem is that I got a PageNotFound warning:

WARN  PageNotFound - No mapping found for HTTP request with URI [/] in DispatcherServlet with name ''

My really simple test as follows:

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({
  "classpath*:/WEB-INF/applicationContext.xml",
  "classpath*:/WEB-INF/serviceContext.xml"
})
public class FrontPageControllerTest {

@Autowired 
private WebApplicationContext ctx;

private MockMvc mockMvc;

@Before  
public void init() {  
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.ctx).build();
}  

@Test
public void frontPageController() throws Exception {
    this.mockMvc.perform(get("/"))
    .andDo(print())
    .andExpect(status().isOk())  
    .andExpect(view().name("searchfrontpage"));       
  }
}

I'm 100% sure that my webapp maps to the frontpage at "/" and that the name on the view is "searchfrontpage".

Please help!

like image 976
jorgen Avatar asked Jul 09 '13 07:07

jorgen


People also ask

Is MockMvc integration test?

Technically speaking, tests using MockMvc are in the boundaries between unit and integration tests. They aren't unit tests because endpoints are tested in integration with a Mocked MVC container with mocked inputs and dependencies.

What does MockMvc perform do?

MockMVC class is part of Spring MVC test framework which helps in testing the controllers explicitly starting a Servlet container. In this MockMVC tutorial, we will use it along with Spring boot's WebMvcTest class to execute Junit testcases which tests REST controller methods written for Spring boot 2 hateoas example.

Which mock MVC method is used to return the DispatcherServlet instance that this MockMvc was initialized with?

getDispatcherServlet. Return the underlying DispatcherServlet instance that this MockMvc was initialized with. This is intended for use in custom request processing scenario where a request handling component happens to delegate to the DispatcherServlet at runtime and therefore needs to be injected with it.


2 Answers

Another easier way to solve the issue is to change init to this :

mockMvc = MockMvcBuilders.standaloneSetup(new FrontPageController()).build();
like image 158
Ali Hashemi Avatar answered Oct 23 '22 07:10

Ali Hashemi


My ContextConfiguration was wrong. Correct was:

@ContextConfiguration({
  "file:src/main/webapp/WEB-INF/applicationContext.xml",
  "file:src/main/webapp/WEB-INF/serviceContext.xml"
})

Now everything works fine.

like image 26
jorgen Avatar answered Oct 23 '22 06:10

jorgen