2017-09-21 15 views
1

模擬方法HttpServletRequestgetResourceAsStream Javaユニットテストでは?私はそれを使ってサーブレットリクエストからリソースファイルを読み込みます。JavaユニットテストモックHttpServletRequest getResourceAsStream

HttpServletRequest.getSession().getServletContext().getResourceAsStream()

私はHttpServletRequestをモックするorg.mockito.Mockを使用しています。

答えて

2

あなたがしなければならないことをかなり嘲笑しています。注釈を使用することをおすすめします:

import static org.mockito.Mockito.when; 

public class TestClass{ 

    @Mock 
    private HttpServletRequest httpServletRequestMock; 

    @Mock 
    private HttpSession httpsSessionMock; 

    @Mock 
    private ServletContext servletContextMock; 

    @Before 
    public void init(){ 
     MockitoAnnotations.initMocks(this); 
    } 

    @Test 
    public void test(){ 
     // Arrange 
     when(httpServletRequestMock.getSession()).thenReturn(httpSessionMock); 
     when(httpSessionMock.getServletContext()).thenReturn(servletContextMock); 

     InputStream inputStream = // instantiate; 

     when(servletContextMock.getResourceAsStream()).thenReturn(inputStream); 

     // Act - invoke method under test with mocked HttpServletRequest 

    } 
} 
関連する問題