### Question:
EtagMessageDigestServletFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (!(response instanceof SlingHttpServletResponse) || !(request instanceof SlingHttpServletRequest)) { throw new IllegalStateException("Filter not properly registered as Sling Servlet Filter"); } if (configuration.enabled()) { log.debug("ETag filter enabled"); SlingHttpServletResponse slingHttpServletResponse = (SlingHttpServletResponse) response; SlingHttpServletRequest slingHttpServletRequest = (SlingHttpServletRequest) request; if (slingHttpServletRequest.getMethod().equals(HttpConstants.METHOD_HEAD) || slingHttpServletRequest.getMethod().equals(HttpConstants.METHOD_GET)) { log.debug("Method is GET or HEAD, calculating ETag..."); doFilterWithMessageDigest(slingHttpServletRequest, slingHttpServletResponse, chain); return; } else { log.debug("Method neither GET or HEAD: {}, no ETag necessary!", slingHttpServletRequest.getMethod()); } } else { log.debug("ETag filter disabled"); } chain.doFilter(request, response); }  @Activate void activate(Config configuration); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy();  }### Answer:
@SuppressWarnings("deprecation") @Test public void testDoFilter() throws IOException, ServletException { Mockito.when(configuration.addAsHtmlComment()).thenReturn(true); Mockito.when(configuration.enabled()).thenReturn(true); StringWriter responseWriter = new StringWriter(); Mockito.when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter)); Mockito.when(mockResponse.getContentType()).thenReturn("text/html"); Mockito.when(mockRequest.getMethod()).thenReturn(HttpConstants.METHOD_POST); final FilterChain chain = new FilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { response.getWriter().write(EXAMPLE_TEXT); } }; filter.doFilter(mockRequest, mockResponse, chain); Mockito.verify(mockResponse, Mockito.never()).setHeader(Mockito.eq(HttpConstants.HEADER_ETAG), Mockito.anyString()); Mockito.verify(mockResponse, Mockito.never()).setStatus(Mockito.anyInt()); Mockito.when(mockRequest.getMethod()).thenReturn(HttpConstants.METHOD_GET); responseWriter = new StringWriter(); Mockito.when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter)); filter.doFilter(mockRequest, mockResponse, chain); Mockito.verify(mockResponse).setHeader(HttpConstants.HEADER_ETAG, "\"9e107d9d372bb6826bd81d3542a419d6\""); Mockito.verify(mockResponse, Mockito.never()).setStatus(Mockito.anyInt()); Mockito.verify(mockResponse, Mockito.never()).setStatus(Mockito.anyInt(), Mockito.anyString()); Assert.assertEquals(responseWriter.toString(), String.format("%s%n<!-- ETag: 9e107d9d372bb6826bd81d3542a419d6 -->%n", EXAMPLE_TEXT)); Vector<String> ifNoneMatchETags = new Vector<>(); ifNoneMatchETags.add("\"someinvalidone\""); ifNoneMatchETags.add("\"9e107d9d372bb6826bd81d3542a419d6\""); Mockito.when(mockRequest.getHeaders(HttpHeaders.IF_NONE_MATCH)).thenReturn(ifNoneMatchETags.elements()); filter.doFilter(mockRequest, mockResponse, chain); Mockito.verify(mockResponse).setStatus(304); }