2017-11-15 11 views
0

私は**ルートURLにすべてのHTML5のルートをリダイレクトし、単一の方法では、次のコントローラを持っています。コントローラメソッドがリクエストを特定のURLに転送するかどうかをテストする方法?私の春のブートアプリケーションで

@Controller 
public class RedirectController { 

    @RequestMapping(value = "/**/{path:[^\\.]*}") 
    public String redirect() { 
    return "forward:/"; 
    } 
} 

私は適切に期待どおりに動作することをテストする必要がありますどのように?

動作しませんMockMvcResultMatchersクラスのcontent()メソッドを呼び出す:

@Test 
    public void givenPathWithoutDotShouldReturnString() throws Exception { 
    this.mockMvc.perform(get("/somePath")) 
     .andExpect(content().string("forward:/")); 
    } 

>>> java.lang.AssertionError: Response content 
>>> Expected :forward:/ 
>>> Actual : 

は**私は this Spring tutorialを次からこのソリューションを知りました。方法で(

MockHttpServletResponse: 
      Status = 200 
    Error message = null 
      Headers = {} 
    Content type = null 
      Body = 
    Forwarded URL =/
    Redirected URL = null 
      Cookies = [] 
ここ

私は春には、単純な文字列の結果としてreturn "forward:/";を扱わないことに気づいたが、URL転送:私はmockMvcクラスのandDo(print())を呼ばれると

答えて

1

は、私は以下の結果を得ましたそれは)かなり明白ですので、テストを書くための適切な方法は、引数としてforwardedUrl("/").andExpect()メソッドを呼び出すことである:

@Test 
    public void givenPathWithoutDotShouldReturnString() throws Exception { 
    this.mockMvc.perform(get("/somePath")) 
     .andExpect(forwardedUrl("/")); 
    } 

forwardedUrl()方法はから来ていますorg.springframework.test.web.servlet.result.MockMvcResultMatchers