2013-07-09 12 views
6

Spring MVCコントローラをテストするためのテストを実行しようとしています。次のようにMockMvcを使用したSpring MVCテスト

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

私は本当に簡単なテスト:テストのコンパイルと実行されますが、私の問題は、私はPageNotFoundの警告を得たことである

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"であることを確認します。

助けてください!

答えて

5

My ContextConfigurationが間違っていました。正解は

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

すべて正常です。

+0

おかげで、これは私が私のテストが作動し得る助けました。 –

+0

@ jorgen applicationContext.xmlファイルとserviceContext.xmlファイルの内容を提供できますか? – dVaffection

0

問題を解決するもう一つの簡単な方法はこれにINITを変更することです:

mockMvc = MockMvcBuilders.standaloneSetup(new FrontPageController()).build(); 
関連する問題