Java 模拟web服务
我想模拟一个web服务调用来测试我的代码。 下面是我想模拟的代码片段。 我想测试callWebService()方法。我想要一种在调用callRestClientService(criteria)时创建自己的HttpResponse的方法。我尝试使用JMock和EasyMock,但无法得到预期的结果。首先,我相信我将无法模拟或创建自己的HttpResponse 即使我不能模拟网关调用,我已经有了一个本地服务器,可以对其进行调用,但我必须模拟服务器发送回的应答,以测试不同的场景 谁能帮我做这个。。。。 谢谢Java 模拟web服务,java,easymock,jmock,Java,Easymock,Jmock,我想模拟一个web服务调用来测试我的代码。 下面是我想模拟的代码片段。 我想测试callWebService()方法。我想要一种在调用callRestClientService(criteria)时创建自己的HttpResponse的方法。我尝试使用JMock和EasyMock,但无法得到预期的结果。首先,我相信我将无法模拟或创建自己的HttpResponse 即使我不能模拟网关调用,我已经有了一个本地服务器,可以对其进行调用,但我必须模拟服务器发送回的应答,以测试不同的场景 谁能帮我做这个。。
如果我没弄错的话,就使用嵌入式或服务器之类的东西。我倾向于使用Simple,因为它通常有更好的文档。您可以很容易地将其设置为返回测试所需的任何内容。根据您需要的复杂性,您甚至可以在服务器中嵌入一个mock,让您在mock上执行正常的mock操作,这将转换为验证HTTP请求和准备HTTP响应。SoapUI是一个开源工具,用于测试web服务,它非常支持服务mock。你说“我试过使用JMock和EasyMock,但没能得到想要的结果”,你能把你试过的贴出来吗?给我们一些可以合作的东西。
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);
}
}
}