2013-07-09 45 views
6

Tôi đang cố chạy thử nghiệm để kiểm tra bộ điều khiển MVC Spring. Các biên dịch kiểm tra và chạy, nhưng vấn đề của tôi là tôi có một cảnh báo PageNotFound:Kiểm tra MVC mùa xuân với MockMvc

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

My thử nghiệm thực sự đơn giản như sau:

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"));  
    } 
} 

Tôi chắc chắn 100% rằng bản đồ webapp của tôi vào frontpage tại "/" và tên trên view là "searchfrontpage".

Vui lòng trợ giúp!

Trả lời

5

ContextConfiguration của tôi đã sai. Chính xác là:

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

Bây giờ mọi thứ hoạt động tốt.

+0

Thanks, điều này đã giúp tôi có được xét nghiệm của tôi làm việc. –

+0

@jorgen Bạn có thể cung cấp nội dung của tệp applicationContext.xml và serviceContext.xml của mình không? – dVaffection

0

Một cách dễ dàng hơn để giải quyết vấn đề này là thay đổi init này:

mockMvc = MockMvcBuilders.standaloneSetup(new FrontPageController()).build(); 
Các vấn đề liên quan