5

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()
4
  • I followed the guide exactly and it worked on the first try. I also switched it from the example JSON to XML as you listed and it worked.
    – Mike Chale
    Oct 8, 2012 at 18:44
  • Did your example use WebServiceCallout.invoke() Oct 8, 2012 at 18:46
  • 1
    No, I just copy/pasted the example in the guide. If you can provide a small test method with invoke() I'll try it out.
    – Mike Chale
    Oct 8, 2012 at 18:47
  • So the final analysis is, this is probably not possible? Oct 9, 2012 at 20:40

3 Answers 3

6

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!

5
  • Hmmm I'll try updating the classes. Oct 8, 2012 at 18:08
  • @techtrekker:No where in the guide mentions we need to update to API 26.0 .Only in one place that in batch classes we need to make the class of API 26.0 .Hence voted down Oct 8, 2012 at 18:22
  • 2
    None of the new features will be available unless the class is on the latest Api version. Try using the new Strin methods in a class with Api version less than 26! Oct 8, 2012 at 18:26
  • @jordan what version were your classes on ? Oct 8, 2012 at 18:28
  • 1
    most are in v25, but I'm updating the interfaces, test method classes, etc. to v26 right now. Oct 8, 2012 at 18:32
3

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

3
  • 1
    RTFM, d00d! ;-)
    – metadaddy
    Oct 16, 2012 at 2:47
  • 1
    Isn't that the class that Mohith Kumar mentioned in his answer (salesforce.stackexchange.com/a/2040/142) or am I missing something?
    – Mike Chale
    Oct 22, 2012 at 10:31
  • He mentioned it. I'll edit the response to show the implementation of the class, as reading the source code doesn't make that clear. Oct 22, 2012 at 15:02
2

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); 
}
}
6
  • That's the same guide I posted. There is no mention of changing the implementation specifically for WS Callouts in that documentation. Oct 8, 2012 at 18:14
  • Dont be in hurry in voting down.There is a documentation and i have posted above from same Oct 8, 2012 at 18:17
  • There you go voted down for your query Oct 8, 2012 at 18:17
  • The implement that you provided doesn't implement HttpCalloutMock I didn't want to return the string, I wanted to use the HttpcalloutMock to capture the Webservicecallout.invoke() and have it return a proper response. Oct 8, 2012 at 18:22
  • I downvoted, because your initial response was just a link to the documentation, I removed my downvote. Oct 8, 2012 at 18:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .