简体   繁体   English

jersey-spring 过滤器的单元测试

[英]Unit test for filter in jersey-spring

I have added one health link to my web service in web.xml as我在 web.xml 中向我的 Web 服务添加了一个健康链接作为

<filter>
    <filter-name>healthChecker</filter-name>
    <filter-class>test.HealthChecker</filter-class>
</filter>

<filter-mapping>
    <filter-name>healthChecker</filter-name>
    <url-pattern>/health</url-pattern>
</filter-mapping>
<filter>
    <filter-name>basicAuthenticationFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <async-supported>true</async-supported><!-- filter supports asynchronous processing -->
</filter>
<filter-mapping>
    <filter-name>basicAuthenticationFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Now I want to unit test this health check现在我想对这个健康检查进行单元测试

@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 for health checker is健康检查器的代码是

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

    }

}

Now when I execute this unit test I am getting 404 error.现在,当我执行此单元测试时,出现 404 错误。 If I see target then url in target is http://localhost:9998/health , which is right.如果我看到目标,则目标中的 url 是http://localhost:9998/health ,这是正确的。

I used this url in chrome but couldnot get anything.我在 chrome 中使用了这个 url,但什么也得不到。

This is done in jersey test framework这是在 jersey 测试框架中完成的

You don't use the right approach, a servlet filter is not meant to be used for this purpose, it is used to perform filtering tasks and here as you break the filter chain (you don't call chain.doFilter(request, response) ), you block the request such that you get a 404 error.您没有使用正确的方法,servlet 过滤器不打算用于此目的,它用于执行过滤任务,在这里您打破过滤器链(您不调用chain.doFilter(request, response) ),您阻止请求,从而收到404错误。

As you obviously use Jersey , you should rather create a rest component and test it with the Jersey Test Framework .由于您显然使用Jersey ,您应该创建一个 rest 组件并使用Jersey 测试框架对其进行测试

Your rest component that will return the status as a JSON object anytime the path /health is requested:您的其余组件将在任何时候请求路径/health将状态作为 JSON 对象返回:

@Path("/health")
public class HealthChecker {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response check() {
        return Response.status(Response.Status.OK)
            .entity("{\"status\":\"UP\"}")
            .build();
    }
}

Then we test that we actually get what we expect:然后我们测试我们实际上得到了我们期望的结果:

public class HealthCheckerTest extends JerseyTest {

    @Override
    protected Application configure() {
        return new ResourceConfig(HealthChecker.class);
    }

    @Test
    public void testCreateGroup() {
        Response response = target("/health").request()
            .accept(MediaType.APPLICATION_JSON)
            .get();
        Assert.assertEquals("{\"status\":\"UP\"}", response.readEntity(String.class));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM