40

实施doFilter()。如何用 jUnit 正确覆盖过滤器?

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws java.io.IOException, javax.servlet.ServletException
{
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    String currentURL = request.getRequestURI();

    if (!currentURL.equals("/maintenance.jsp") && modeService.getOnline())
    {
        response.sendRedirect("/maintenance.jsp");
    }
    filterChain.doFilter(servletRequest, servletResponse);
}
4

3 回答 3

40

ServletRequestServletResponse并且FilterChain都是接口,因此您可以轻松地为它们创建测试存根,无论是手动还是使用模拟框架。

使模拟对象可配置,以便您可以准备一个预设响应getRequestURI(),以便您可以查询 ServletResponse 以断言已调用 sendRedirect。

注入一个模拟 ModeService。

调用 doFilter 传递模拟 ServletRequest、ServletResponse 和 FilterChain 作为其参数。

@Test
public void testSomethingAboutDoFilter() {
    MyFilter filterUnderTest = new MyFilter();
    filterUnderTest.setModeService(new MockModeService(ModeService.ONLINE));
    MockFilterChain mockChain = new MockFilterChain();
    MockServletRequest req = new MockServletRequest("/maintenance.jsp");
    MockServletResponse rsp = new MockServletResponse();

    filterUnderTest.doFilter(req, rsp, mockChain);

    assertEquals("/maintenance.jsp",rsp.getLastRedirect());
}

在实践中,您需要将设置移动到 @BeforesetUp()方法中,并编写更多 @Test 方法来覆盖所有可能的执行路径。

...而且您可能会使用像 JMock 或 Mockito 这样的模拟框架来创建模拟,而不是MockModeService我在这里使用的假设等。

这是一种单元测试方法,与集成测试相反。您只是在执行被测单元(和测试代码)。

于 2012-07-12T12:50:15.473 回答
21

您可以使用模拟框架来模拟上述内容HttpServletRequest、对象HttpServletResponse及其FilterChain行为。根据框架,您具有某些功能,以验证在执行代码期间是否对所采取的模拟对象采取了正确的操作。

例如,当使用Mockito模拟框架时,doFilter()可以使用以下测试用例对提供的方法进行 JUnit 测试:

@Test
public void testDoFilter() throws IOException, ServletException {
    // create the objects to be mocked
    HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
    HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
    FilterChain filterChain = mock(FilterChain.class);
    // mock the getRequestURI() response
    when(httpServletRequest.getRequestURI()).thenReturn("/otherurl.jsp");

    MaintenanceFilter maintenanceFilter = new MaintenanceFilter();
    maintenanceFilter.doFilter(httpServletRequest, httpServletResponse,
            filterChain);

    // verify if a sendRedirect() was performed with the expected value
    verify(httpServletResponse).sendRedirect("/maintenance.jsp");
}
于 2014-11-09T07:57:17.763 回答
18

如果您使用的是 Spring,那么它有自己的模拟:

  • org.springframework.mock.web.MockFilterChain
  • org.springframework.mock.web.MockHttpServletRequest
  • org.springframework.mock.web.MockHttpServletResponse

例如,您可以添加标头并设置测试 Uri。

所以你的测试可以是这样的:

@RunWith(MockitoJUnitRunner.class)
public class TokenAuthenticationFilterTest {

    private static final String token = "260bce87-6be9-4897-add7-b3b675952538";
    private static final String testUri = "/testUri";

    @Mock
    private SecurityService securityService;

    @InjectMocks
    private TokenAuthenticationFilter tokenAuthenticationFilter = new TokenAuthenticationFilter();

    @Test
    public void testDoFilterInternalPositiveScenarioWhenTokenIsInHeader() throws ServletException, IOException {
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.addHeader(TOKEN, token);
        request.setRequestURI(testUri);
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain filterChain = new MockFilterChain();
        when(securityService.doesExistToken(token)).thenReturn(true);
        tokenAuthenticationFilter.doFilterInternal(request, response, filterChain);
        assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
    }
}
于 2017-03-29T08:03:01.240 回答