### Question:
ESProxyFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { final HttpServletRequest httpRequest = (HttpServletRequest) request; final HttpServletResponse httpResponse = (HttpServletResponse) response; if (httpRequest.isUserInRole(Role.ADMIN)) { doProxyCall(httpRequest, httpResponse); } else { if (httpRequest.getUserPrincipal() == null) { httpResponse.addHeader("WWW-Authenticate", "Basic realm=\"" + HttpBasicChallengeInterceptor.CHALLENGE_TEXT + "\""); httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED); } else { httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN); } } }  @Override void init(FilterConfig filterConfig); @Override void destroy(); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain);  }### Answer:
@Test public void doFilter() throws IOException, ServletException { ESProxyFilter tested = Mockito.mock(ESProxyFilter.class); HttpServletRequest requestMock = Mockito.mock(HttpServletRequest.class); HttpServletResponse responseMock = Mockito.mock(HttpServletResponse.class); FilterChain filterChainMock = Mockito.mock(FilterChain.class); Mockito .doCallRealMethod() .when(tested) .doFilter(Mockito.any(ServletRequest.class), Mockito.any(ServletResponse.class), Mockito.any(FilterChain.class)); Mockito.when(requestMock.isUserInRole(Role.ADMIN)).thenReturn(true); tested.doFilter(requestMock, responseMock, filterChainMock); Mockito.verify(tested).doFilter(requestMock, responseMock, filterChainMock); Mockito.verify(tested).doProxyCall(requestMock, responseMock); Mockito.verify(requestMock).isUserInRole(Role.ADMIN); Mockito.verifyNoMoreInteractions(requestMock, responseMock, filterChainMock); Mockito.reset(tested, requestMock, responseMock, filterChainMock); Mockito .doCallRealMethod() .when(tested) .doFilter(Mockito.any(ServletRequest.class), Mockito.any(ServletResponse.class), Mockito.any(FilterChain.class)); Mockito.when(requestMock.isUserInRole(Role.ADMIN)).thenReturn(false); Mockito.when(requestMock.getUserPrincipal()).thenReturn(null); tested.doFilter(requestMock, responseMock, filterChainMock); Mockito.verify(tested).doFilter(requestMock, responseMock, filterChainMock); Mockito.verify(requestMock).isUserInRole(Role.ADMIN); Mockito.verify(requestMock).getUserPrincipal(); Mockito.verify(responseMock).addHeader(Mockito.eq("WWW-Authenticate"), Mockito.anyString()); Mockito.verify(responseMock).sendError(HttpServletResponse.SC_UNAUTHORIZED); Mockito.verifyNoMoreInteractions(requestMock, responseMock, filterChainMock); Mockito.reset(tested, requestMock, responseMock, filterChainMock); Mockito .doCallRealMethod() .when(tested) .doFilter(Mockito.any(ServletRequest.class), Mockito.any(ServletResponse.class), Mockito.any(FilterChain.class)); Mockito.when(requestMock.isUserInRole(Role.ADMIN)).thenReturn(false); Principal pm = Mockito.mock(Principal.class); Mockito.when(requestMock.getUserPrincipal()).thenReturn(pm); tested.doFilter(requestMock, responseMock, filterChainMock); Mockito.verify(tested).doFilter(requestMock, responseMock, filterChainMock); Mockito.verify(requestMock).isUserInRole(Role.ADMIN); Mockito.verify(requestMock).getUserPrincipal(); Mockito.verify(responseMock).sendError(HttpServletResponse.SC_FORBIDDEN); Mockito.verifyNoMoreInteractions(requestMock, responseMock, filterChainMock); }