Java 用MockMvc进行springmvc测试

Java 用MockMvc进行springmvc测试,java,spring,unit-testing,mocking,spring-mvc-test,Java,Spring,Unit Testing,Mocking,Spring Mvc Test,我正在尝试运行一个测试SpringMVC控制器。测试将编译并运行,但我的问题是收到了PageNotFound警告: WARN PageNotFound - No mapping found for HTTP request with URI [/] in DispatcherServlet with name '' 我的简单测试如下: import static org.springframework.test.web.servlet.request.MockMvcRequestBuilde

我正在尝试运行一个测试SpringMVC控制器。测试将编译并运行,但我的问题是收到了PageNotFound警告:

WARN  PageNotFound - No mapping found for HTTP request with URI [/] in DispatcherServlet with name ''
我的简单测试如下:

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"));       
  }
}
我100%确信我的webapp映射到位于“/”的frontpage,并且视图上的名称为“searchfrontpage”


请帮忙

我的上下文配置错误。正确答案是:

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

现在一切正常。

解决此问题的另一个简单方法是将init更改为:

mockMvc = MockMvcBuilders.standaloneSetup(new FrontPageController()).build();

@jorgen能否提供applicationContext.xml和serviceContext.xml文件的内容?