mocking web service
I would like to mock a web service call to test my code. The following is a code snippet of which I want to mock. I would like to test callWebService() method. I want a way to create my own HttpResponse when callRestClientService(criteria) is called. I tried using JMock and EasyMock but unable to get the desired result. At the first instance I believe that I will not be able to mock or create my own HttpResponse.
Even if I am not able to mock the gateway call, I already have a local server to which I can make call to, but I would have to mock the reply sent back by the server to test different scenarios.
Can anyone help me with this.... Thanks!!
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);
}
}
}
If I understand you right, just use something like an embedded Jetty or Simple HTTP server. I tend to use Simple because it has generally better documentation. You can very easily set it to return whatever you want for a test. Depending on the complexity you need, you can even embed a mock inside the server, letting you perform normal mocking operations on the mock, which are translated into verifying HTTP requests and preparing HTTP responses.
SoapUI is a open source tool created to test web services and it supports service mocking very well. Go through this tutorial on how to do it.
精彩评论