title: Unit test for filter in jersey-spring
body: <p>I have added one health link to my web service in web.xml as</p>
  
  <pre><code>&lt;filter&gt;
      &lt;filter-name&gt;healthChecker&lt;/filter-name&gt;
      &lt;filter-class&gt;test.HealthChecker&lt;/filter-class&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;healthChecker&lt;/filter-name&gt;
      &lt;url-pattern&gt;/health&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;basicAuthenticationFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
      &lt;async-supported&gt;true&lt;/async-supported&gt;&lt;!-- filter supports asynchronous processing --&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;basicAuthenticationFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>Now I want to unit test this health check</p>
  
  <pre><code>@Test
  public void testHealthCheck() {
      ClientHttpRequestFactory originalRequestFactory = restTemplate.getRequestFactory();
      try {
          WebTarget target = target().path("/health");;
  
          final Response mockResponse = target.request().get();
  
          Assert.assertNotNull("Response must not be null", mockResponse.getEntity());
      } finally {
          restTemplate.setRequestFactory(originalRequestFactory);
      }
  }
  </code></pre>
  
  <p>Code for health checker is </p>
  
  <pre><code>public class HealthChecker implements Filter {
  
      @Override
      public void destroy() {
          //do nothing
  
      }
  
      @Override
      public void doFilter(ServletRequest request, ServletResponse response, 
          FilterChain chain) throws IOException, ServletException {
          response.setContentType("text/json");
          String json ="{\"status\":\"UP\"}";
          response.getWriter().append(json);
  
      }
  
      @Override
      public void init(FilterConfig filter) throws ServletException {
           // do nothing
  
      }
  
  }
  </code></pre>
  
  <p>Now when I execute this unit test I am getting 404 error. If I see target then url in target is <a href="http://localhost:9998/health" rel="nofollow noreferrer">http://localhost:9998/health</a>, which is right.</p>
  
  <p>I used this url in chrome but couldnot get anything.</p>
  
  <p>This is done in jersey test framework</p>