Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Enhanced Web Service Testing
Kirk Steffke
AppExchange Practice Lead
kirk@crmscience.com
@kirkevonphilly
A Better Mock Stru...
• Test Coverage Overview
• Review of Native Testing Methods
• WebServiceMock and HttpCalloutMock
Interfaces
• StaticResour...
Test Coverage Overview
• Who: Developers
• What: Code written… about code
• When: Production Org bound code
• Where: Sandboxes and developer orgs...
Apex Test Coverage
Who, What, When, Where, and Why
Works!
Doesn’t
Work!
Should Good work!
!
Shouldn’t ! (silence)
Apex Test Coverage
A Bit More of “How”
Grandma’s Famous Recipe
1. Write code to do
something
2. Create test class
3. Mark ...
Native Testing Strategies
The Tools that are Already in your Toolbox
• Exist/defined in a separate
class
• Returns single callout
response
• WebServiceMock for WSDL
based SOAP callouts
• Http...
• Class with single method
• Method makes a callout
• Testing with:
• HttpCalloutMock Interface
• StaticResourceCalloutMoc...
• Class with three methods
• Two methods make different callouts
• One method calls both callout methods
• Testing with:
•...
Code Demos
Examples 1, 2, and 3
Complex Scenario
A hypothetical, but real-world model to help you digest
• Batch job
• Import of Contacts from remote system
• Remote system has an API
• Two endpoints
• /api/ContactCount
• /api/...
• /api/ContactCount
• Returns # of Contact records to be imported
• Result can change during span of batch execution
• /ap...
1. Batch is called
2. Start() method
3. Each execute() iteration performs 2 callouts
4. 1st checks total # of records avai...
• Class with three methods
• Two methods make different callouts
• One method calls both callout methods
• Simulate batch ...
Limitations
Working with Our Tools
• HttpCalloutMock Interface and StaticResourceMock
• Can only handle one endpoint
• Sin...
Problems
Continued…
• Challenge
• Use one or more standard tool
• Extend the usage
• Create queue of expected responses pe...
Response Generator
The Playlist Structure
Method
GET/POST/etc
Endpoint
/api/endpoint
Responses
[0] response 1
[1] response...
Response Generator
The “Count” Playlist
Method
GET
Endpoint
Contact Count
Responses
[0] 10 Contacts
[1] 12 Contacts
[x] 12...
Response Generator
The “Data” Playlist
Method
GET
Endpoint
Contact Data
Responses
[0] Data 0-199
[1] Data 200-399
[9] Data...
Code Demo
Example 4
Testing the Solution
TestCalloutClass
1. Count or Contact callout
2. # of loop iteration
3. Count data 10, 12, 12, …
1. Us...
Resources
Blog.crmscience.com
• Blog Post: http://bit.ly/df15_kirk_blog
• Unmanaged Package with all examples: http://bit....
Share Your Feedback, and Win a GoPro!
3
Earn a GoPro prize entry for
each completed survey
Tap the bell to take a survey2E...
Questions
Find One of Us in a Lab Coat
Thank you
Upcoming SlideShare
Loading in …5
×

Enhanced Web Service Testing: A Better Mock Structure

592 views

Published on

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.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Enhanced Web Service Testing: A Better Mock Structure

  1. 1. Enhanced Web Service Testing Kirk Steffke AppExchange Practice Lead kirk@crmscience.com @kirkevonphilly A Better Mock Structure
  2. 2. • Test Coverage Overview • Review of Native Testing Methods • WebServiceMock and HttpCalloutMock Interfaces • StaticResourceCalloutMock • MultiStaticResourceCalloutMock • Sample Scenarios • Better Mock Structure • Setup • Review Agenda During this session
  3. 3. Test Coverage Overview
  4. 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. 5. Apex Test Coverage Who, What, When, Where, and Why Works! Doesn’t Work! Should Good work! ! Shouldn’t ! (silence)
  6. 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 -)
  7. 7. Native Testing Strategies The Tools that are Already in your Toolbox
  8. 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. 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. 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
  11. 11. Code Demos Examples 1, 2, and 3
  12. 12. Complex Scenario A hypothetical, but real-world model to help you digest
  13. 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. 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. 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. 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. 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. 18. Problems Continued… • Challenge • Use one or more standard tool • Extend the usage • Create queue of expected responses per endpoint • Consider re-usability
  19. 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
  20. 20. Response Generator The “Count” Playlist Method GET Endpoint Contact Count Responses [0] 10 Contacts [1] 12 Contacts [x] 12 Contacts
  21. 21. Response Generator The “Data” Playlist Method GET Endpoint Contact Data Responses [0] Data 0-199 [1] Data 200-399 [9] Data x
  22. 22. Code Demo Example 4
  23. 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. 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. 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
  26. 26. Questions Find One of Us in a Lab Coat
  27. 27. Thank you

×