Salesforce provides an interface for testing callouts named HttpCalloutMock used to cover remote callouts. While adequate for simple callouts, in the real world you often need something more flexible, as in the case of multiple and varying responses from the same or varying endpoints. More precise testing and coverage can be obtained by extending the standard interface. Join us as we demonstrate a solution to use to enable the flexibility required for complex integration and synchronization apps.
4. • Who: Developers
• What: Code written… about code
• When: Production Org bound code
• Where: Sandboxes and developer orgs
• Why: Safety first
Apex Test Coverage
Who, What, When, Where, and Why
5. Apex Test Coverage
Who, What, When, Where, and Why
Works!
Doesn’t
Work!
Should Good work!
!
Shouldn’t ! (silence)
6. Apex Test Coverage
A Bit More of “How”
Grandma’s Famous Recipe
1. Write code to do
something
2. Create test class
3. Mark with @isTest
4. Create test method
5. Create test data
6. Invoke code being tested
7. Assert (+ or -)
8. • Exist/defined in a separate
class
• Returns single callout
response
• WebServiceMock for WSDL
based SOAP callouts
• HttpCalloutMock for testing
Http callouts
• Defined by content of text file
in Static Resource
• Returns response for a single
endpoint
• Similar to
StaticResourceCalloutMock
• Static Resource contains
response for multiple
endpoints
WebServiceMock and
HttpCalloutMock Interfaces
StaticResourceCalloutMock MultiStaticResourceCalloutMock
Documentation: bit.ly/df15mock3Documentation: bit.ly/df15mock1 Documentation: bit.ly/df15mock2
Options for Testing HTTP Callouts
Out-of-the-box Tools
9. • Class with single method
• Method makes a callout
• Testing with:
• HttpCalloutMock Interface
• StaticResourceCalloutMock
Upcoming Examples
Examples 1 and 2: Single Callout
Class
Callout Method
External API Endpoint
Request
Response
10. • Class with three methods
• Two methods make different callouts
• One method calls both callout methods
• Testing with:
• MultiStaticResourceCalloutMock
Upcoming Examples
Example 3: Multiple Callouts
External API Endpoint 2Request
Response
Class
External API Endpoint 1
Request
Response
Both Callouts
Callout Method 2
Callout Method 1
13. • Batch job
• Import of Contacts from remote system
• Remote system has an API
• Two endpoints
• /api/ContactCount
• /api/Contacts
Remote Import via Batch
Overview
14. • /api/ContactCount
• Returns # of Contact records to be imported
• Result can change during span of batch execution
• /api/Contacts
• Returns x number of contacts
• Paginated
• Page=1 – returns 1st 200 records
• Page=2 – returns 2nd 200 records
• …and so on
Remote Import via Batch
Meet the Endpoints
15. 1. Batch is called
2. Start() method
3. Each execute() iteration performs 2 callouts
4. 1st checks total # of records available
5. 2nd imports records
6. End of execute determines if more
records?
7. Execute may run for many iterations
8. If no more records or limits, finish()
9. Restart cycle
10. End
Remote Import via Batch
Batch Flow
16. • Class with three methods
• Two methods make different callouts
• One method calls both callout methods
• Simulate batch w/ a loop
Upcoming Examples
Example 4: Multiple Everything
Contact DataRequest
Response
Class
Callout Method 1
Contact Count
Request
ResponseCallout Method 2
x10
17. Limitations
Working with Our Tools
• HttpCalloutMock Interface and StaticResourceMock
• Can only handle one endpoint
• Single response
• Single setup prior to invoking actual callouts
• MultiStaticResourceCalloutMock
• Handle multiple endpoints
• Single response for each
• Single setup prior to invoking actual callouts
18. Problems
Continued…
• Challenge
• Use one or more standard tool
• Extend the usage
• Create queue of expected responses per endpoint
• Consider re-usability
19. Response Generator
The Playlist Structure
Method
GET/POST/etc
Endpoint
/api/endpoint
Responses
[0] response 1
[1] response 2
[x] response x
• Like music at a party
• Some want to rock
• Some want to dance
• Sometimes you want to hear the same song again
23. Testing the Solution
TestCalloutClass
1. Count or Contact callout
2. # of loop iteration
3. Count data 10, 12, 12, …
1. Use 1st, discard
2. Use 2nd, don’t discard
4. Contact data increments
1. Provided per page response
24. Resources
Blog.crmscience.com
• Blog Post: http://bit.ly/df15_kirk_blog
• Unmanaged Package with all examples: http://bit.ly/df15_kirk_pack
• Link to deck: http://bit.ly/df15_kirk_deck
25. Share Your Feedback, and Win a GoPro!
3
Earn a GoPro prize entry for
each completed survey
Tap the bell to take a survey2Enroll in a session1