我想模拟一个 Web 服务调用来测试我的代码。以下是我要模拟的代码片段。我想测试 callWebService() 方法。我想要一种在调用 callRestClientService(criteria) 时创建自己的 HttpResponse 的方法。我尝试使用 JMock 和 EasyMock 但无法获得所需的结果。首先,我相信我将无法模拟或创建自己的 HttpResponse。
即使我无法模拟网关调用,我也已经有一个可以调用的本地服务器,但我必须模拟服务器发回的回复以测试不同的场景。
任何人都可以帮我解决这个问题......谢谢!
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);
}
}
}