Sara Has No Limits

Good Programmers Simplify, Automate and Document

More about Web Services and Unit Testing

One of the limitations of Salesforce unit tests is that they do not make web service call-outs. So, how do you satisfy unit testing requirements for any apex code that does web service call-outs?  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

In a previous post, I included notes from the premier training about Unit Testing. I recently viewed Podcasts of the Spring 2013 class, Apex programming on the Force.com platform and took some additional notes about testing. This post is a portion of those notes.

Categories: Developer Certification, Web Services

Tags: , , , ,

1 reply

  1. No limits!

    Sent from my iPhone

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 31 other followers