我在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>
现在我想对此健康检查进行单元测试
@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);
}
}
健康检查员代码是
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
}
}
现在,当我执行此单元测试时,我收到404错误。如果我看到目标,则目标中的网址为http://localhost:9998/health,这是正确的。
我在chrome中使用了这个url但却无法得到任何东西。
这是在球衣测试框架中完成的
答案 0 :(得分:1)
你没有使用正确的方法,servlet过滤器不是用于此目的,它用于执行过滤任务,在这里打破过滤器链(你不要调用{{1} }),您阻止请求,以便收到[9, 291015400640036, 380069543210027, 380069652690027]
[110, 291907469820036, 357657110470027]
[10812641776701, 24735152712200, 24735152733701, 393122343520028]
[10812641776701, 24735152712200, 24735152733701, 393122343520028]
[10812638253700, 24735152712200, 24735152714301, 393122343520028]
[10812641776701, 24735152712200, 24735152733701, 393122343520028]
[10812638253700, 24735152712200, 24795683891101, 393122343520028]
[10812638253700, 24735152712200, 24735152714301, 393122343520028]
[10812638253700, 24735152712200, 24735153508400, 393122343520028]
[10812641776701, 24735152712200, 24735152733701, 393122343520028]
[10812638253700, 24735152712200, 24795683891101, 393122343520028]
[10812638253700, 24735152712200, 24735152714301, 393122343520028]
[10812638253700, 24735152712200, 24735153508400, 393122343520028]
[10812641776701, 24735152712200, 24735152733701, 393122343520028]
[10812638253700, 24735152712200, 24795683891101, 393122343520028]
[10812638253700, 24735152712200, 24735152714301, 393122343520028]
[10812638253700, 24735152712200, 24735153508400, 393122343520028]
[10812641776701, 24735152712200, 24735152733701, 393122343520028]
[6, 375895250670027, 392314597880056]
[10812638253700, 24735152712200, 24795683891101, 393122343520028]
错误。
当你明显使用 Jersey 时,你应该创建一个rest组件并使用Jersey Test Framework进行测试。
您的rest组件将在请求路径chain.doFilter(request, response)
的任何时候返回状态作为JSON对象:
404
然后我们测试我们实际上得到了我们期望的结果:
/health