title: How to Junit test servlet filter which has specific response
body: <p>What is the best way to unit test this code?  I need to establish a consistent check for httpResponse which sendError() when condition is true.  Thanks in advance!</p>
  
  <p>Edit:  Unfortunately, this filter is not with Spring MVC so my choice is limited.</p>
  
  <pre><code>    public void doFilter(ServletRequest request, ServletResponse response,
          FilterChain filterchain) throws IOException, ServletException {
  
      String ipAddress = request.getRemoteAddr();
      if( SomeParameterCheckingFunction ((request)) ) {
          logger.error("Error detected! Time: " + new Date().toString() + ", Originating IP: " + ipAddress);
          if (response instanceof HttpServletResponse){
              HttpServletResponse httpResponse = (HttpServletResponse) response;
              httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN,"You are not allowed to access the server!");
          }
      }
      filterchain.doFilter(request, response);
  }
  </code></pre>