2

When an apex test method attempts to make a web service callout something like the following is logged:

23:11:45.288 (4288851000)|EXCEPTION_THROWN|[116]|System.TypeException: Methods defined as TestMethod do not support Web service callouts, test skipped

23:11:45.288 (4288996000)|METHOD_EXIT|[346]|01p40000000Gykn|XYZ.FooWebService.BasicHttpBinding_IAdBookConnectNS.FooMethod()

23:11:45.289 (4289007000)|METHOD_EXIT|[39]|01p40000000Gy7G|XYZ.FooWebServiceWrapper.fooMethod()

23:11:45.291 (4291299000)|FATAL_ERROR|System.TypeException: Methods defined as TestMethod do not support Web service callouts, test skipped

23:11:45.291 (4291318000)|CODE_UNIT_FINISHED|Test_FooWebServiceWrapper.fooMethodTest

23:11:45.291 (4291326000)|EXECUTION_FINISHED

Not attempting to make the web service callout makes sense, as you don't want irreversible changes occurring every time you run the automated tests. (Plus there is the potential delay due to the callout latency).

I know that I can use Test.isRunningTest() to avoid the callout when testing and return suitable mock data.

However, if the callout is attempted, the only accurate indication that the test case failed is buried in the log file. It doesn't get explicitly marked as failed or skipped.

In addition to the log messages, there is a mismatch between the Tests Run count and the displayed Test Successes, but you would need to be checking for that to pick it up.

Mismatch between Tests Run count and Test Successes count due to web service callout

Is it possible to force test cases to explicitly fail when a test method attempts a web service callout?

One basic solution I've found is to catch the TypeException and explicitly fail the test method. I tried re-throwing the exception, but there must be some property on it that makes it fail silently. E.g.

try {
    fooWebServiceInstance.FooMethod();
} catch (System.TypeException ex) {
    System.assert(false, ex.getMessage());
}

1 Answer 1

4

If you haven't already, you might want to check out what's coming in Winter '13 in the Support for Testing Callouts section to consider how you might design/refactor things down the road.

My personal preference has been to roll my own mock support, creating a mock mock framework ;) But I think things are going to get much cleaner with the HttpCalloutMock interface.

To your question, I don't know if what your attempting to do is possible. But even if it was, I'm unclear as to why you would really want to do such a thing instead of always preparing mock callout responses to actually test the rest your code in the given execution context. Unless you're dealing with code that isn't in your control, an Eclipse global search for HTTPRequest and proper mock handling throughout is the recipe I'd recommend.

3
  • I'm dealing with an imported WSDL that has been run through wsdl2apex. I have no control over the implementation of the web service, but the generated apex class runs into many thousands of lines. I'll typically prevent calls to WebServiceCallout.invoke using Test.isRunningTest(), but they sometimes slip through. When they do get missed, the test cases don't pick up the issue as they silently fail and don't show the issue. If wsdl2apex is run again I can't rely on the test cases to pickup where Test.isRunningTest has been left out. Sep 10, 2012 at 0:34
  • That is a burden for sure. It might be worth the effort to wrap your wsdl2apex-generated class inside your own custom class which won't get stomped on in the future. Then you can handle all of your callouts with appropriate mocks from there. Granted it's not a very appealing alternative at that scale.
    – Adam
    Sep 10, 2012 at 1:47
  • Just having a bit of a rant, and this isn't in any way directed at you @Adam, but is really is a burden. Due to the number of lines involved I need to keep the code coverage up even though it is all generated code. And 70% of it won't be used but came in via the WSDL. One of these days I'll make my own replacement for wsdl2apex with options to prune out elements and auto generate some basic tests. Sep 10, 2012 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.