单元测试jee过滤器

时间:2017-02-08 12:52:43

标签: testing junit

我正在尝试测试此过滤器:

public class HttpMethodOverrideHeaderFilter extends OncePerRequestFilter {
    private static final String X_HTTP_METHOD_OVERRIDE_HEADER = "X-HTTP-Method-Override";

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        if (isMethodOverriden(request)) {
            HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, request.getHeader(X_HTTP_METHOD_OVERRIDE_HEADER).toUpperCase(Locale.ENGLISH));
            filterChain.doFilter(wrapper, response);
        }
        else {
            filterChain.doFilter(request, response);
        }

    }

    private boolean isMethodOverriden(HttpServletRequest request) {
        String methodOverride = request.getHeader(X_HTTP_METHOD_OVERRIDE_HEADER);
        return RequestMethod.POST.name().equalsIgnoreCase(request.getMethod()) &&
                (RequestMethod.PUT.name().equalsIgnoreCase(methodOverride) || RequestMethod.DELETE.name().equalsIgnoreCase(methodOverride));
    }

    protected static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {
        private final String method;

        public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
            super(request);
            this.method = method;
        }

        @Override
        public String getMethod() {
            return this.method;
        }
    }

}

这是单元测试:

@RunWith(MockitoJUnitRunner.class)
public class HttpMethodOverrideHeaderFilterTest {

    private static final String X_HTTP_METHOD_OVERRIDE_HEADER = "X-HTTP-Method-Override";

    private HttpMethodOverrideHeaderFilter httpMethodOverrideHeaderFilter;

    @Mock
    private HttpServletRequest httpServletRequest;

    @Mock
    private HttpServletResponse httpServletResponse;

    @Mock
    private FilterChain filterChain;

    @Before
    public void setUp() {
        httpMethodOverrideHeaderFilter = new HttpMethodOverrideHeaderFilter();
    }

    @Test
    public void testDoFilterInternalWithPUTMethodAsOverrideHeader() throws Exception {
        when(httpServletRequest.getHeader(X_HTTP_METHOD_OVERRIDE_HEADER)).thenReturn("PUT");
        when(httpServletRequest.getMethod()).thenReturn("POST");

        HttpServletRequest wrapper = new HttpMethodOverrideHeaderFilter.HttpMethodRequestWrapper(httpServletRequest, "PUT");

        httpMethodOverrideHeaderFilter.doFilterInternal(httpServletRequest, httpServletResponse, filterChain);
        verify(filterChain).doFilter(wrapper, httpServletResponse);
    }

}

测试没有传递,因为包装器不是同一个实例。基本上我需要知道的是如果包装器设置了PUT方法。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我找到了办法:

@Test
public void testDoFilterInternalWithPUTMethodAsOverrideHeader() throws Exception {
    when(httpServletRequest.getHeader(X_HTTP_METHOD_OVERRIDE_HEADER)).thenReturn("PUT");
    when(httpServletRequest.getMethod()).thenReturn("POST");

    httpMethodOverrideHeaderFilter.doFilterInternal(httpServletRequest, httpServletResponse, filterChain);

    ArgumentCaptor<ServletRequest> requestCaptor = ArgumentCaptor.forClass(ServletRequest.class);
    ArgumentCaptor<ServletResponse> responseCaptor = ArgumentCaptor.forClass(ServletResponse.class);
    verify(filterChain).doFilter(requestCaptor.capture(), responseCaptor.capture());

    HttpMethodOverrideHeaderFilter.HttpMethodRequestWrapper wrapper = (HttpMethodOverrideHeaderFilter.HttpMethodRequestWrapper) requestCaptor.getValue();
    assertEquals(wrapper.getMethod(), "PUT");
}

如果有人知道更好的方式,请告诉我!!!