Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing Http Callouts in Apex

Testing Http Callouts in Apex

Callout methods can't be tested with the traditional approach because the execution context. However, Apex provides a few built-in helper and interface classes that you can use to test callouts by specifying the response body, type and status code in a static resource for each endpoint. Let's showcase the right way for testing these methods.

Andrea Morales

March 16, 2016
Tweet

More Decks by Andrea Morales

Other Decks in Technology

Transcript

  1. Safe harbor statement under the Private Securities Litigation Reform Act

    of 1995: This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize or if any of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the forward-looking statements we make. All statements other than statements of historical fact could be deemed forward-looking, including any projections of subscriber growth, earnings, revenues, or other financial items and any statements regarding strategies or plans of management for future operations, statements of belief, any statements concerning new, planned, or upgraded services or technology developments and customer contracts or use of our services. The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our service, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth, interruptions or delays in our Web hosting, breach of our security measures, risks associated with possible mergers and acquisitions, the immature market in which we operate, our relatively limited operating history, our ability to expand, retain, and motivate our employees and manage our growth, new releases of our service and successful customer deployment, our limited history reselling non- salesforce.com products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the financial results of salesforce.com, inc. is included in our annual report on Form 10-K for the most recent fiscal quarter ended July 31, 2011. This document and others are available on the SEC Filings section of the Investor Information section of our Web site. Any unreleased services or features referenced in this or other press releases or public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently available. Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.
  2. Agenda • review of current available Integration tools • callouts

    testing challenges • features that enhance the testing of callouts • demo
  3. • Apex Web Services • expose the apex logic we

    write, so external service can invoke it • Callouts • enables us to integrate with external applications • call external web services • send an http request from our code Apex Integration Utilities
  4. Web Services ▪ Expose our apex class methods as a

    REST or SOAP web service operation. ▪ REST Service ▪ Soap Service https://instance.salesforce.com/services/apexrest/Account/
  5. Callouts ▪ Apex allows you to integrate with external SOAP

    and REST Web services using callouts. • Web service callouts to SOAP web services • use XML • generally relies on a WSDL document for code generation • HTTP callouts • typically uses REST with JSON
  6. Callouts : SOAP WebService ▪ WSDL2Apex is an open source

    code generator that takes a WSDL file and generates the Apex stubs for it – resolves • construction of SOAP XML • data transmission • parsing the response XML into apex objects – open source project since September 2014
  7. Callouts : Http callouts ▪ Apex supports for HTTP Services

    with several built in classes : – HTTP: used for initiating an HTTP request and response. – HttpRequest: programmatically creates HTTP requests. – HttpResponse: handles the HTTP response returned by HTTP.Send()
  8. Body getBody() setBody(body) getBodyAsBlob() setBodyAsBlob(body) getBodyDocument() setBodyDocument(document) getCompressed() setCompressed(flag) EndPoint

    getEndpoint() setEndpoint(endpoint) Header getHeader(key) setHeader(key, value) Method getMethod() setMethod(method) setClientCertificateName(certDevName) setTimeout(timeout) toString() Callouts : Http Request class
  9. Body getBody() setBody(body) getBodyAsBlob() setBodyAsBlob(body) getBodyDocument() Header getHeader(key) setHeader(key, value)

    getHeaderKeys() Status getStatus() setStatus(status) getStatusCode() setStatusCode(statusCode) getXmlStreamReader() toString() Callouts : Http Response class
  10. And then we wanted to distribute our app… Developer! You

    didn't file at least 75% of your paperwork last night.
  11. Testing Callouts ▪ Apex test method do not support callouts,

    so tests will fail. ▪ Old way : Test.isRunningTest() and branch our logic ▪ Workaround ▪ Mock callouts on testing runtime ▪ implemented as Static Resources ▪ as apex classes that implement a given interface ▪ Test.setMock () on our test class
  12. Test a Callout with StaticResourceCalloutMock ▪ This class uses the

    content of a text file stored in a StaticResource , to return as response body, along with other http response attributes ▪ StaticResourceCalloutMock class : ▪ setHeader(headerName, headerValue) ▪ setStaticResource(resourceName) ▪ setStatus(httpStatus) ▪ setStatusCode(httpStatusCode)
  13. Test a Callout with MultiStaticResourceCalloutMock ▪ Returns response for multiple

    endpoints ▪ maps endPoint to mock response ▪ setHeader(headerName, headerValue) ▪ setStaticResource(endpoint, resourceName) ▪ setStatus(httpStatus) ▪ setStatusCode(httpStatusCode)
  14. Test a Callout by Implementing the HttpCalloutMock ▪ is a

    separate class ▪ returns response for a single endpoint ( but we can workaround this)
  15. Test a SOAP Callout implementing WebServiceMock ▪ is a separate

    class ▪ returns response for a single endpoint
  16. Demo ▪ Url shorter component – send requests to TinyUrl

    or Bitly services • TinyUrl • unauthenticated • test code uses httpCalloutMock Class • Bitty • authenticated • test code uses StaticResourceMock and MultiStaticResource Mock
  17. Resources + Kudos Integration and Apex Utilities Apex Integration Services

    Examples • Test Coverage Patter for Multi-Callout Methods • Testing Apex Callouts using HttpCalloutMock
  18. Thank You! Testing Http Callouts if there's a will, there's

    a way … Uruguay Salesforce Developer Group March 10, 2016 #UYSDUG Andrea Morales App Curator/Team Lead @andrem2b #uysdug #trailhead