2013-07-09 18 views
6

Próbuję uruchomić test do testowania kontrolera Spring MVC. Kompilacji testy i działa, ale moim problemem jest to, że dostałem ostrzeżenie PageNotFound:Spring MVC test z MockMvc

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

My naprawdę prosty test następująco:

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

jestem 100% pewna, że ​​moje mapy do webapp strona tytułowa "/", a nazwa w widoku to "strona wyszukiwania".

Proszę pomóc!

Odpowiedz

5

Moja konfiguracja kontekstu była niepoprawna. Poprawnie:

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

Teraz wszystko działa poprawnie.

+0

Dzięki, to pomogło mi moje testy pracy. –

+0

@jorgen Czy możesz podać zawartość plików applicationContext.xml i serviceContext.xml? – dVaffection

0

Innym łatwiejszy sposób, aby rozwiązać ten problem jest zmiana init to:

mockMvc = MockMvcBuilders.standaloneSetup(new FrontPageController()).build(); 
Powiązane problemy