我已經在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錯誤。如果我看到目標,那麼目標中的url是http://localhost:9998/health,這是正確的。
我在鉻中使用此網址,但無法得到任何東西。
這在球衣測試框架做
你爲什麼使用過濾器而不是servlet?我認爲404可能是因爲沒有servlet映射到該路徑。 – cproinger
你在哪裏定義你班上的路徑?註解? –
路徑是好的,因爲這工作正常,當我真正運行的應用程序,我可以打http:// localhost:8080 /健康 – anand