You need to sign in to do that
Don't have an account?
Middha
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]
Any ideas how to create mock objects in apex??
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.
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.
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();
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