I have a mock class for a web service that implements WebServiceMock.
With some requests the actual web service being mocked will return a SOAP Fault that results in a System.CalloutException in Apex. E.g. passing in an invalid external ID.
How can I mock the SOAP fault response that results in a CalloutException?
I have code that catches these exceptions that I'd like to test using the mock. I'd prefer not to insert special test cases in the actual code using Test.isRunningTest()
to simulate the response.
A sample response from the web service looks like:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring xml:lang="en-US">Validation Error(s) occurred during Foo Get.</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>
I cannot throw a new instance of System.CalloutException. Doing so gives the error:
Type cannot be constructed: System.CalloutException
I cannot extend System.CalloutExceptions. The compiler gives me the error:
LocalCalloutException: Non-virtual and non-abstract type cannot be extended: System.CalloutException
Usually I would put an instance of the class specified in the responseType
parameter into the response
parameter dictionary with the key 'response_x'. I tried switching this out with a SOAP fault:
response.put('response_x', '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><s:Fault><faultcode>s:Client</faultcode><faultstring xml:lang="en-US">Validation Error(s) occurred during Foo Get.</faultstring></s:Fault></s:Body></s:Envelope>');
This resulted in a (here FooBar_element
is the type specified in responseType
):
System.TypeException: Collection store exception putting String into MAP<String,FooBar_element>
responseType
parameter into theresponse
parameter dictionary with the key 'response_x'. Short of throwing an exception that dictionary seems to be the only output mechanism. I'll see if I can put a SOAP fault into it.FooBar_element
here will be an arbitrary inner apex class that Wsdl2Apex generated for the SOAP response. It will have members to hold the data, plus a correspondingmembername_type_info
String[]. There will also be string[] for apex_schema_type_info and field_order_type_info. That's about it. There is no inheritance involved.