当端点正在调用另一个应用程序时,我想为端点编写单元测试。 到目前为止,我可以测试调用是否具有正确的URI或方法类型,但是我想模拟响应。
public Response getFormById(Integer formId) {
Invocation invocation = prepareRequestForGettingForm(formId);
Response response = invocation.invoke();
return response;
}
public Invocation prepareRequestForGettingForm(Integer formId) {
return ClientBuilder
.newClient()
.register(authenticator)
.target("localhost:8080" + "/form" + formId)
.request(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.build("get");
}
测试:
@Test
public void request_getFormById_shouldContainsProperEndpointAddress() {
String expectedUri = expectedUriBase + formId;
Invocation invocation = mock.prepareRequestForGettingForm(formId);
String uri = ((ClientInvocation) invocation).getUri().toString();
assertEquals(uri, expectedUri);
when(invocation.invoke()).thenReturn(Response.ok().build());
Response response = formServiceBean.getFormById(formId);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}
当我尝试模拟invocation.invoke时,我遇到了错误:
javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request
at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:317)
所以有人可以告诉我如何模拟吗?
是否还可以使用Rest-Assure,但稍后再进行代码覆盖率测试是否明智?