Enhanced Web Service Testing Kirk Steffke AppExchange Practice Lead [email protected] @kirkevonphilly A Better Mock Structure
Salesforce
Enhanced Web Service TestingKirk SteffkeAppExchange Practice [email protected]@kirkevonphillyA Better Mock Structure
Test Coverage OverviewReview of Native Testing MethodsWebServiceMock and HttpCalloutMock InterfacesStaticResourceCalloutMockMultiStaticResourceCalloutMockSample ScenariosBetter Mock StructureSetupReviewAgendaDuring this session
Test Coverage Overview
Who: DevelopersWhat: Code written about codeWhen: Production Org bound codeWhere: Sandboxes and developer orgsWhy: Safety firstApex Test CoverageWho, What, When, Where, and Why
Apex Test CoverageWho, What, When, Where, and WhyWorks!Doesnt Work!ShouldGood work!!Shouldnt!(silence)
Apex Test CoverageA Bit More of How
Grandmas Famous RecipeWrite code to do somethingCreate test classMark with @isTestCreate test methodCreate test dataInvoke code being testedAssert (+ or -)
Native Testing StrategiesThe Tools that are Already in your Toolbox
Exist/defined in a separate classReturns single callout responseWebServiceMock for WSDL based SOAP calloutsHttpCalloutMock for testing Http calloutsDefined by content of text file in Static ResourceReturns response for a single endpointSimilar to StaticResourceCalloutMockStatic Resource contains response for multiple endpointsWebServiceMock and HttpCalloutMock InterfacesStaticResourceCalloutMockMultiStaticResourceCalloutMockDocumentation: bit.ly/df15mock3Documentation: bit.ly/df15mock1Documentation: bit.ly/df15mock2Options for Testing HTTP CalloutsOut-of-the-box Tools
Class with single methodMethod makes a calloutTesting with:HttpCalloutMock InterfaceStaticResourceCalloutMock
Upcoming ExamplesExamples 1 and 2: Single CalloutClassCallout MethodExternal API EndpointRequestResponse
Class with three methodsTwo methods make different calloutsOne method calls both callout methodsTesting with:MultiStaticResourceCalloutMock
Upcoming ExamplesExample 3: Multiple CalloutsExternal API Endpoint 2RequestResponseClassExternal API Endpoint 1RequestResponse
Both CalloutsCallout Method 2Callout Method 1
Code DemosExamples 1, 2, and 3
Complex ScenarioA hypothetical, but real-world model to help you digest
Batch jobImport of Contacts from remote systemRemote system has an APITwo endpoints/api/ContactCount/api/ContactsRemote Import via BatchOverview
/api/ContactCountReturns # of Contact records to be importedResult can change during span of batch execution/api/ContactsReturns x number of contactsPaginatedPage=1 returns 1st 200 recordsPage=2 returns 2nd 200 recordsand so onRemote Import via BatchMeet the Endpoints
Batch is calledStart() methodEach execute() iteration performs 2 callouts1st checks total # of records available2nd imports recordsEnd of execute determines if more records?Execute may run for many iterationsIf no more records or limits, finish()Restart cycleEndRemote Import via BatchBatch Flow
Class with three methodsTwo methods make different calloutsOne method calls both callout methodsSimulate batch w/ a loop
Upcoming ExamplesExample 4: Multiple EverythingContact DataRequestResponseClassCallout Method 1Contact CountRequestResponseCallout Method 2
x10
LimitationsWorking with Our Tools
HttpCalloutMock Interface and StaticResourceMockCan only handle one endpointSingle responseSingle setup prior to invoking actual calloutsMultiStaticResourceCalloutMockHandle multiple endpointsSingle response for eachSingle setup prior to invoking actual callouts
ProblemsContinued
ChallengeUse one or more standard toolExtend the usageCreate queue of expected responses per endpointConsider re-usability
Response GeneratorThe Playlist Structure
MethodGET/POST/etcEndpoint/api/endpointResponses[0] response 1
[1] response 2
[x] response x
Like music at a partySome want to rockSome want to danceSometimes you want to hear the same song again
Response GeneratorThe Count Playlist
MethodGETEndpointContact CountResponses[0] 10 Contacts
[1] 12 Contacts
[x] 12 Contacts
Response GeneratorThe Data Playlist
MethodGETEndpointContact DataResponses[0] Data 0-199
[1] Data 200-399
[9] Data x
Code DemoExample 4
Testing the SolutionTestCalloutClass
Count or Contact callout# of loop iterationCount data 10, 12, 12, Use 1st, discardUse 2nd, dont discardContact data incrementsProvided per page response
ResourcesBlog.crmscience.com
Blog Post: http://bit.ly/df15_kirk_blogUnmanaged Package with all examples: http://bit.ly/df15_kirk_packLink to deck: http://bit.ly/df15_kirk_deck
Share Your Feedback, and Win a GoPro!
3Earn a GoPro prize entry for each completed survey
Tap the bell to take a survey
2
Enroll in a session
1
QuestionsFind One of Us in a Lab Coat
Thank you