我想嘲笑一个web服务调用来测试我的代码。 以下是我想要模拟的代码片段。 我想测试callWebService()方法。我想在调用callRestClientService(条件)时创建自己的HttpResponse。我尝试使用JMock和EasyMock,但无法获得所需的结果。首先,我相信我将无法模拟或创建自己的HttpResponse。嘲笑web服务
即使我不能模拟网关呼叫,我已经有一个本地服务器可以拨打到,但我必须嘲笑服务器发送的回复来测试不同的场景。
任何人都可以帮助我这个.... 谢谢!
public class RestClientServiceResponse
{
public HttpResponse callRestClientService(final RestClientServiceCriteria criteria) throws IOException
{
final HttpUriRequest request = buildHttpUriRequest(criteria);
return executeRestClientServiceCall(request);
}
public HttpResponse executeRestClientServiceCall(final HttpUriRequest request) throws IOException
{
final HttpClient client = new DefaultHttpClient();
final HttpResponse httpResponse = client.execute(request);
return httpResponse;
}
}
public class CallWebService
{
public void callWebService()
{
HttpResponse httpResponse = null;
try
{
httpResponse = restClient.callRestClientService(criteria);
}
catch (final Exception e)
{
System.out.println(e);
}
}
}
你说:“我尝试使用JMock和EasyMock,但无法获得所需的结果”,你可以发布你已经尝试过吗?给我们一些工作。 –