2016-11-09 3 views
1

Spring Web Model-View-Controller(MVC)フレームワークアプリケーションでMockito(MITライセンスで公開されたJavaのオープンソーステストフレームワーク)を使用してこのjunitテストを実施しました 私はこれを持っていますコントローラのメソッド:Mockito:check responseヘッダーとステータス

@RequestMapping(value = { "/devices" } , method = { RequestMethod.GET, RequestMethod.POST}) 
    public void initGetForm(@ModelAttribute("searchForm") final SearchForm searchForm, 
          HttpServletRequest request, 
          HttpServletResponse response, 
          Locale locale) throws Exception { 

     String newUrl = request.getContextPath() + "/devices/" + locale; 

     if (locale !=null && isValid(locale)) { 
      newUrl = request.getContextPath() + "/devices/" + DEFAULT_LOCALE;   
     } 

     redirectPermanently (response, newUrl);  

    } 




    protected void redirectPermanently (HttpServletResponse response, String to) { 

     response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); 
     response.setHeader("Location", to); 
     response.setHeader("Connection", "close"); 
     } 

と、これは私のテストクラスです:

@Test 
    public void testInitGetForm() throws Exception { 

     controller.initGetForm(emptySearchForm(), request, response, null); 

    } 

@Before 
public void setUpTest() { 
    request = new MockHttpServletRequest(); 
    response = new MockHttpServletResponse(); 
} 

はレスポンスのヘッダーとステータスを確認することが可能です?????

+0

このテストではMockitoを使用していません。 –

答えて

1

spring-testモジュールのMockHttpServletResponseMockHttpServletRequestのインスタンスを使用できます。 コントローラに渡してから、MockHttpServletResponse.getStatus()MockHttpServletResponse.containsHeader() メソッドを使用して結果を確認してください。