How do I unit test code that includes a callout?












9














This is a canonical question and answer developed by the community to help address common questions. If you've been directed here, or your question has been closed as a duplicate, please look through the resources here and use them to shape more specific questions. To browse all canonical questions and answers, including more unit test resources, navigate to the canonical-qa tag.



How do I unit test code that includes a SOAP or REST-based callout, or indirectly calls code that makes a callout? I'm getting an error saying that callouts are not supported.










share|improve this question





























    9














    This is a canonical question and answer developed by the community to help address common questions. If you've been directed here, or your question has been closed as a duplicate, please look through the resources here and use them to shape more specific questions. To browse all canonical questions and answers, including more unit test resources, navigate to the canonical-qa tag.



    How do I unit test code that includes a SOAP or REST-based callout, or indirectly calls code that makes a callout? I'm getting an error saying that callouts are not supported.










    share|improve this question



























      9












      9








      9


      4





      This is a canonical question and answer developed by the community to help address common questions. If you've been directed here, or your question has been closed as a duplicate, please look through the resources here and use them to shape more specific questions. To browse all canonical questions and answers, including more unit test resources, navigate to the canonical-qa tag.



      How do I unit test code that includes a SOAP or REST-based callout, or indirectly calls code that makes a callout? I'm getting an error saying that callouts are not supported.










      share|improve this question















      This is a canonical question and answer developed by the community to help address common questions. If you've been directed here, or your question has been closed as a duplicate, please look through the resources here and use them to shape more specific questions. To browse all canonical questions and answers, including more unit test resources, navigate to the canonical-qa tag.



      How do I unit test code that includes a SOAP or REST-based callout, or indirectly calls code that makes a callout? I'm getting an error saying that callouts are not supported.







      unit-test callout httpcalloutmock webservicemock canonical-qa






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      asked Dec 27 '18 at 16:25


























      community wiki





      David Reed























          1 Answer
          1






          active

          oldest

          votes


















          8














          As part of the isolation of the test context, Salesforce does not allow your code to make REST or SOAP callouts during test execution. This includes all code that's executed in test context, even if it is executed indirectly by the code you're explicitly testing, and it includes callouts to Salesforce itself.



          To test code that makes callouts, you must develop a Mock class, which mocks the remote server during the test and constructs an appropriate response that's returned to your code. Mock classes may generate a response in code (for example, by constructing objects, serializing them, and returning the resulting JSON), or by returning a result stored in a Static Resource.



          Salesforce makes available two Mock interfaces (HttpCalloutMock, for REST calls, and WebServiceMock, for SOAP calls), as well as the StaticResourceCalloutMock and MultiStaticResourceCalloutMock implementations. You can use these built-in implementations to test your callouts by providing result data in a Static Resource, which the mock will return to your code in test context. Keep in mind that you must call Test.setMock() to configure your mock in the test context prior to invoking the code that makes a callout.



          In many cases, you'll need to use multiple Mock classes, returning different response bodies or status codes to exercise different logic paths in your code. Each unit test would supply an appropriate Mock for the code path whose behavior it's intended to test.



          Mock classes are not required to test inbound REST and web service classes. Instead, the classes may be called directly (with appropriate inputs), in a way similar to testing other Apex code.



          Resources



          To learn how to develop Mock classes, complete these Trailhead modules:





          • Apex REST Callouts

          • Apex SOAP Callouts


          For more in-depth information, explore these sections in the Apex Developer Guide:




          • Testing HTTP Callouts

          • Test Web Service Callouts


          • HttpCalloutMock Interface


          • WebServiceMock Interface


          • StaticResourceCalloutMock Class


          • MultiStaticResourceCalloutMock Class






          share|improve this answer























          • Multiple mock classes can often be avoided by using dependency injection to the mock class' constructor allowing variable responses in the respond() method
            – cropredy
            Dec 28 '18 at 2:23











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "459"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f244797%2fhow-do-i-unit-test-code-that-includes-a-callout%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          8














          As part of the isolation of the test context, Salesforce does not allow your code to make REST or SOAP callouts during test execution. This includes all code that's executed in test context, even if it is executed indirectly by the code you're explicitly testing, and it includes callouts to Salesforce itself.



          To test code that makes callouts, you must develop a Mock class, which mocks the remote server during the test and constructs an appropriate response that's returned to your code. Mock classes may generate a response in code (for example, by constructing objects, serializing them, and returning the resulting JSON), or by returning a result stored in a Static Resource.



          Salesforce makes available two Mock interfaces (HttpCalloutMock, for REST calls, and WebServiceMock, for SOAP calls), as well as the StaticResourceCalloutMock and MultiStaticResourceCalloutMock implementations. You can use these built-in implementations to test your callouts by providing result data in a Static Resource, which the mock will return to your code in test context. Keep in mind that you must call Test.setMock() to configure your mock in the test context prior to invoking the code that makes a callout.



          In many cases, you'll need to use multiple Mock classes, returning different response bodies or status codes to exercise different logic paths in your code. Each unit test would supply an appropriate Mock for the code path whose behavior it's intended to test.



          Mock classes are not required to test inbound REST and web service classes. Instead, the classes may be called directly (with appropriate inputs), in a way similar to testing other Apex code.



          Resources



          To learn how to develop Mock classes, complete these Trailhead modules:





          • Apex REST Callouts

          • Apex SOAP Callouts


          For more in-depth information, explore these sections in the Apex Developer Guide:




          • Testing HTTP Callouts

          • Test Web Service Callouts


          • HttpCalloutMock Interface


          • WebServiceMock Interface


          • StaticResourceCalloutMock Class


          • MultiStaticResourceCalloutMock Class






          share|improve this answer























          • Multiple mock classes can often be avoided by using dependency injection to the mock class' constructor allowing variable responses in the respond() method
            – cropredy
            Dec 28 '18 at 2:23
















          8














          As part of the isolation of the test context, Salesforce does not allow your code to make REST or SOAP callouts during test execution. This includes all code that's executed in test context, even if it is executed indirectly by the code you're explicitly testing, and it includes callouts to Salesforce itself.



          To test code that makes callouts, you must develop a Mock class, which mocks the remote server during the test and constructs an appropriate response that's returned to your code. Mock classes may generate a response in code (for example, by constructing objects, serializing them, and returning the resulting JSON), or by returning a result stored in a Static Resource.



          Salesforce makes available two Mock interfaces (HttpCalloutMock, for REST calls, and WebServiceMock, for SOAP calls), as well as the StaticResourceCalloutMock and MultiStaticResourceCalloutMock implementations. You can use these built-in implementations to test your callouts by providing result data in a Static Resource, which the mock will return to your code in test context. Keep in mind that you must call Test.setMock() to configure your mock in the test context prior to invoking the code that makes a callout.



          In many cases, you'll need to use multiple Mock classes, returning different response bodies or status codes to exercise different logic paths in your code. Each unit test would supply an appropriate Mock for the code path whose behavior it's intended to test.



          Mock classes are not required to test inbound REST and web service classes. Instead, the classes may be called directly (with appropriate inputs), in a way similar to testing other Apex code.



          Resources



          To learn how to develop Mock classes, complete these Trailhead modules:





          • Apex REST Callouts

          • Apex SOAP Callouts


          For more in-depth information, explore these sections in the Apex Developer Guide:




          • Testing HTTP Callouts

          • Test Web Service Callouts


          • HttpCalloutMock Interface


          • WebServiceMock Interface


          • StaticResourceCalloutMock Class


          • MultiStaticResourceCalloutMock Class






          share|improve this answer























          • Multiple mock classes can often be avoided by using dependency injection to the mock class' constructor allowing variable responses in the respond() method
            – cropredy
            Dec 28 '18 at 2:23














          8












          8








          8






          As part of the isolation of the test context, Salesforce does not allow your code to make REST or SOAP callouts during test execution. This includes all code that's executed in test context, even if it is executed indirectly by the code you're explicitly testing, and it includes callouts to Salesforce itself.



          To test code that makes callouts, you must develop a Mock class, which mocks the remote server during the test and constructs an appropriate response that's returned to your code. Mock classes may generate a response in code (for example, by constructing objects, serializing them, and returning the resulting JSON), or by returning a result stored in a Static Resource.



          Salesforce makes available two Mock interfaces (HttpCalloutMock, for REST calls, and WebServiceMock, for SOAP calls), as well as the StaticResourceCalloutMock and MultiStaticResourceCalloutMock implementations. You can use these built-in implementations to test your callouts by providing result data in a Static Resource, which the mock will return to your code in test context. Keep in mind that you must call Test.setMock() to configure your mock in the test context prior to invoking the code that makes a callout.



          In many cases, you'll need to use multiple Mock classes, returning different response bodies or status codes to exercise different logic paths in your code. Each unit test would supply an appropriate Mock for the code path whose behavior it's intended to test.



          Mock classes are not required to test inbound REST and web service classes. Instead, the classes may be called directly (with appropriate inputs), in a way similar to testing other Apex code.



          Resources



          To learn how to develop Mock classes, complete these Trailhead modules:





          • Apex REST Callouts

          • Apex SOAP Callouts


          For more in-depth information, explore these sections in the Apex Developer Guide:




          • Testing HTTP Callouts

          • Test Web Service Callouts


          • HttpCalloutMock Interface


          • WebServiceMock Interface


          • StaticResourceCalloutMock Class


          • MultiStaticResourceCalloutMock Class






          share|improve this answer














          As part of the isolation of the test context, Salesforce does not allow your code to make REST or SOAP callouts during test execution. This includes all code that's executed in test context, even if it is executed indirectly by the code you're explicitly testing, and it includes callouts to Salesforce itself.



          To test code that makes callouts, you must develop a Mock class, which mocks the remote server during the test and constructs an appropriate response that's returned to your code. Mock classes may generate a response in code (for example, by constructing objects, serializing them, and returning the resulting JSON), or by returning a result stored in a Static Resource.



          Salesforce makes available two Mock interfaces (HttpCalloutMock, for REST calls, and WebServiceMock, for SOAP calls), as well as the StaticResourceCalloutMock and MultiStaticResourceCalloutMock implementations. You can use these built-in implementations to test your callouts by providing result data in a Static Resource, which the mock will return to your code in test context. Keep in mind that you must call Test.setMock() to configure your mock in the test context prior to invoking the code that makes a callout.



          In many cases, you'll need to use multiple Mock classes, returning different response bodies or status codes to exercise different logic paths in your code. Each unit test would supply an appropriate Mock for the code path whose behavior it's intended to test.



          Mock classes are not required to test inbound REST and web service classes. Instead, the classes may be called directly (with appropriate inputs), in a way similar to testing other Apex code.



          Resources



          To learn how to develop Mock classes, complete these Trailhead modules:





          • Apex REST Callouts

          • Apex SOAP Callouts


          For more in-depth information, explore these sections in the Apex Developer Guide:




          • Testing HTTP Callouts

          • Test Web Service Callouts


          • HttpCalloutMock Interface


          • WebServiceMock Interface


          • StaticResourceCalloutMock Class


          • MultiStaticResourceCalloutMock Class







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 27 '18 at 19:31


























          community wiki





          2 revs, 2 users 98%
          David Reed













          • Multiple mock classes can often be avoided by using dependency injection to the mock class' constructor allowing variable responses in the respond() method
            – cropredy
            Dec 28 '18 at 2:23


















          • Multiple mock classes can often be avoided by using dependency injection to the mock class' constructor allowing variable responses in the respond() method
            – cropredy
            Dec 28 '18 at 2:23
















          Multiple mock classes can often be avoided by using dependency injection to the mock class' constructor allowing variable responses in the respond() method
          – cropredy
          Dec 28 '18 at 2:23




          Multiple mock classes can often be avoided by using dependency injection to the mock class' constructor allowing variable responses in the respond() method
          – cropredy
          Dec 28 '18 at 2:23


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Salesforce Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f244797%2fhow-do-i-unit-test-code-that-includes-a-callout%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents