Find your content:

Search form

You are here

Testing WebServiceCallout with HttpCalloutMock

 
Share

I am trying to use the new HttpCalloutMock following the guide here.

This is a new feature, and I was hoping to make some better test cases for using web-service callouts that respond with a SOAP Message.

However, after setting up the HttpCalloutMock response, I've since discovered that the TestMethod continues to throw: TypeException: Methods defined as TestMethod do not support Web service callouts, test skipped

I would assume this is being thrown from the imported on the WS Callout definition at WebServiceCallout.invoke(), since this isn't a simple HTTPRequest is it even going to be possible to test my web-service callouts?

    @isTest
global class ApiListMtgListMeetingsResultTest implements HttpCalloutMock {
    // Implement this interface method 

global HTTPResponse respond(HTTPRequest req) {
    system.debug('Mock ApiListMtgListMeetingsResult');
    HttpResponse res = new HttpResponse();
    res.setHeader('Content-Type', 'text/xml;charset=UTF-8');
    res.setBody('<soap:Envelope>...</soap:Envelope>');
    res.setStatusCode(200);
    return res;
}

}

In the guide, setting the Test.Mock seems to capture any HTTP callouts via HttpRequest.send(), no explicit settings required.

Update

I guess I should clarify, I am trying to use HttpCalloutMock to respond to a call from:

WebServiceCallout.invoke()

Attribution to: jordan.baucke

Possible Suggestion/Solution #1

Thanks to this blog post from @metadaddy - I realized there is ANOTHER INTERFACE specifically for Webservices callouts.


Attribution to: jordan.baucke

Possible Suggestion/Solution #2

Without delving too deep, I've found such new feature errors to be stemming from old Api version on the class. Make sure you set the api version to 26.

None of the new features will be available unless the class is on the latest Api version. Try using the new String methods in a class with Api version less than 26!


Attribution to: techtrekker

Possible Suggestion/Solution #3

The one you are referring is for HTTP callouts .

Specifying a Mock Response for Testing Web Service Callouts When you create an Apex class from a WSDL, the methods in the auto-generated class call WebServiceCallout.invoke, which performs the callout to the external service. When testing these methods, you can instruct the Apex runtime to generate a fake response whenever WebServiceCallout.invoke is called. To do so, implement the WebServiceMock interface and specify a fake response that the Apex runtime should send. Here are the steps in more detail.

Please refer for SOAP API's

This is the method that makes a Web service callout.

public class WebSvcCallout {
    public static String callEchoString(String input) {
        docSample.DocSamplePort sample = new docSample.DocSamplePort();
        sample.endpoint_x = 'http://api.salesforce.com/foo/bar';

    // This invokes the EchoString method in the generated class 

    String echo = sample.EchoString(input);

    return echo;
}   
}

This is the test class containing the test method that sets the mock callout mode. It calls the callEchoString method in the previous class and verifies that a mock response is received.

@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); 
}
}

Attribution to: Mohith Shrivastava
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/2038

My Block Status

My Block Content