+ Start a Discussion
MiddhaMiddha 

Testing We Service Callouts

Hi,
 
I am trying to integrate my application with some 3rd party application using their Web Service. I have created 2 different APEX classes that manipulate the input and at the end call the webservice.
 
When i write test methods to test these classes i get "Methods defined as TestMethod do not support Web service callouts, test skipped".
 
Is there any way to test the methods in these classes so that i can get a code coverage of 75% and move it to production instance?
 
[GM]
Rich CRich C
I have also ran into this and need to find an answer. I can understand why tests shouldn't make external calls, so there needs to be a way to make an httpResponse mock object that can be used in place of one created from a 'real' http call. Or failing that, code making an external call should not be counted in code coverage stats.

Any ideas how to create mock objects in apex??
Ron HessRon Hess
testing your code with web service callouts will leave a few lines of code unexecuted, this is manageable by putting very little code after the callout or http.send();

instead, just exit the method after the send and breakout subsequent functionality into separate methods, these are then callable from test methods by constructing (mock) response objects and passing these into these separate methods.

If you can post a bit of code that is showing as uncovered in your test coverage report, it may help to take a look at specifics.
Rich CRich C
I've been refactoring the unreachable code out to new methods, that works pretty good. But I got stuck on the method below. The input parameter is a HttpResponse, and I run some conditional logic based on the response code. But I don't know a way to create a mock HttpResponse with a valid and invalid response code.
My work around was to change the input parameter to an int and pass in the response code that way. However that workaround becomes tedious quite fast when I want to use other attributes of the HttpResponse in the method. Why break apart a perfectly good object into parameter values when I _really need_ the object in the method?

Below is the method to test, and an example test method:

private static Boolean get_return_result(HttpResponse res)
{
if (res.getStatusCode() == 200 )
{
System.debug('no Problem calling url: ' + url + ', status: ' + res.getStatusCode());
// Other code using the HttpResponse...
return true;
}
else
{
System.debug('Problem calling url: ' + url + ', status: ' + res.getStatusCode());
// Some code using the HttpResponse...
return false;
}
}

static testMethod void test_result_status()
{

HttpResult res = new HttpResult();

// Doesn't work, how can I set the desired status code???
// trying to test true branch
res.setStatusCode(200);

Boolean x = get_return_result(res);
System.assertEquals(x, true);


// Doesn't work... same as above
// trying to test false branch
res.setStatusCode(400);
x = get_return_result(res);
System.assertEquals(x, false);
}

Hopefully this example illustrates the issue.
I don't how to create mock objects in apex, is there a way? It would be helpful to have mock HttpRequest and HttpResult objects available that we could set the desired properties on and pass into our test methods.

Thanks for your help,

Rich C.
Ron HessRon Hess
Unfortunatly, i think you will have to change the input parameter to an int and pass in the response code that way.

You can also pass the request to that routine, then you can invoke the routine from your test methods.

you could also wrap HttpRequest into your own object which has a working code setter, then construct that in your code and in your test methods.

I can file a feature request to get
setStatusCode();




KlebKleb
There is a Salesforce IdeaExchange entry created for this issue. Please vote on it.
 
 
I too have this problem, and it's quite serious, particularly for SOAP-based web services (as opposed to REST). When the web service is accessed via a proxy class generated from WSDL, it adds a great deal of code to the total count (which ultimately becomes unreachable because of this problem). Also, it's a bad idea to get into a pattern of altering the generated proxy class.
BijayBijay

Hi ,

 

I have parse one WSDl for createAccount Relationshipship as per the Apex Develoiper guide ,

i was trying to test by providing a mock reponse

global class YourWebServiceMockImpl implements WebServiceMock {
global void doInvoke(
Object stub,
Object request,
Map<String, Object> response,
String endpoint,
String soapAction,
String requestName,
String responseNS,
String responseName,
String responseType) {
// Create response element from the autogenerated class.
// Populate response element.
// Add response element to the response parameter, as follows:
response.put('response_x', responseElement);
}
}

 

and test class would be in this format

 

@isTest
private class WebSvcCalloutTest {
@isTest static void testEchoString() {
// This causes a fake response to be generated
Test.setMock(WebServiceMock.class, new WebServiceMockImpl());
// Call the method that invokes a callout
String output = WebSvcCallout.callEchoString('Hello World!');
// Verify that a fake result is returned
System.assertEquals('Mock response', output);
}
}

 

But according to my WSDL i dnt have any response element in the response wrapper class,so how to test my Apex class which call out the WSDl class.

 

Please reply its urgent and i am stuck.

Thanks in Advance

Atul GuptaAtul Gupta
Guys I'm running into the same issue....any tips ?