+ Start a Discussion
Aurélien.LavalAurélien.Laval 

Web service class test with a static resource as result

Hello, I'm developping a test class to cover an Apex class which call a web service.

I have generated the client class thanks to WSDL2Apex with importing the WSDL.

The web service return a XML content which is parsed and transformed in a 'Result' object which contains a lot of others objects.

I have read the documentation (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm" target="_blank)) and it is said to specify a mock response.

Is there a way to give a XML content as a static resource to the mock response to simulate the web service response or I must create a Result object with all values which is the parsed result?

Thanks to advance.
Ashish_SFDCAshish_SFDC
Hi , 


You have two options.

Option 1: Programmatically generate a mock call-out
In the following example, a class which implements HttpCalloutMock is used to create a fake response:

HttpMockCalloutYour unit test code will then invoke the test class and use the setMock method to call the test code, such as the following:

Test.setMock(HttpCalloutMock.class, new HttpCalloutMockImpl());

Option 2: Use a static resource to upload a fake response
In the next example, an XML file named mockResponse was created and uploaded to Salesforce as a static resource. The following code could be used in your unit test to load the XML file and create a mock response:

StaticResourceMockCallout

http://saramorgan.net/2013/08/28/more-about-web-services-and-unit-testing/


Regards,
Ashish
Aurélien.LavalAurélien.Laval
Thanks for your response. 

It will be very useful for my futur project.