{"id":2990,"date":"2021-06-23T09:35:35","date_gmt":"2021-06-23T08:35:35","guid":{"rendered":"https:\/\/www.bluewavegroup.eu\/?p=2990"},"modified":"2023-01-17T13:20:05","modified_gmt":"2023-01-17T13:20:05","slug":"writing-unit-tests-for-restful-apex-methods","status":"publish","type":"post","link":"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/","title":{"rendered":"Writing Unit Tests for RESTful Apex Methods"},"content":{"rendered":"<div class=\"wpb-content-wrapper\" id=\"wpb-content-root\"><p>[vc_row][vc_column][vc_column_text]<\/p>\n<h4>A Step-by-Step Guide to Salesforce REST API Integration for Apex Developers (Part II)<\/h4>\n<p>Author: <a href=\"https:\/\/www.linkedin.com\/in\/ismail-basser-96354a140\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Ismail Basser, Salesforce Consultant<\/a> at Bluewave Technology<\/p>\n<hr class=\"wp-block-separator is-style-wide\" \/>\n<h3>Introduction<\/h3>\n<p>In <a href=\"https:\/\/bluewavecx.com\/2021\/04\/07\/inbound-rest-api-salesforce\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">a previous post<\/a>, we examined how a developer might handle data operations between Salesforce and an external system using a custom Apex class and REST API calls. We went through the process of establishing a connection with Salesforce using a Connected App, and reviewed in-detail how RESTful Apex methods handle HTTP GET and POST calls. As a natural continuation, we will now examine how to properly unit test those RESTful Apex methods. Specifically, how we can \u2018mock\u2019 the request that gets sent to Salesforce.<\/p>\n<h3>Begin with a Test Setup Method<\/h3>\n<p><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_testing_testsetup_using.htm\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Test Setup Methods<\/a> are a great way to create testable records once and then access them in subsequent test methods in the same test class. Not only are they time-saving when you need to create prerequisite record data but they can also reduce test execution times especially when you\u2019re working with many records. We will use a simple test setup method as follows:<\/p>\n<pre class=\"wp-block-code\"><code>@testSetup\r\n    static void dataSetup() {\r\n        Account acc = new Account(Name = 'IB Test Acc');\r\n        insert acc;\r\n    }<\/code><\/pre>\n<p>Now that we have a testable account record we can begin to author the unit tests specific to our RESTful Apex methods.<\/p>\n<h3>But wait, how do we &#8216;mock&#8217; a RestRequest?<\/h3>\n<p>When we write unit tests for RESTful Apex methods we won&#8217;t be communicating with a live external system. Instead, we&#8217;ll use an instance of the <a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexref.meta\/apexref\/apex_methods_system_restrequest.htm\" target=\"_blank\" rel=\"noreferrer noopener\">RestRequest<\/a> class to simulate external system requests and we can manipulate the behavior of those requests with the following key attributes.<\/p>\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<td><strong>httpMethod<\/strong> (Required)<\/td>\n<td>Returns one of the supported HTTP request methods. (GET, POST, etc&#8230;)<\/td>\n<\/tr>\n<tr>\n<td><strong>requestURI <\/strong>(Required)<\/td>\n<td>Returns or sets everything after the host in the HTTP request string.<\/td>\n<\/tr>\n<tr>\n<td><strong>requestBody <\/strong>(Optional)<\/td>\n<td>Returns or sets the body of the request.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h3>Unit Testing GET Methods<\/h3>\n<p>Let&#8217;s start with <span style=\"text-decoration: underline;\"><strong>a positive unit test for a REST API GET call<\/strong><\/span>. By a &#8216;positive test&#8217; here we don&#8217;t mean a method that passes but rather a method where the outcome is as expected and the functionality works as intended. For example, let&#8217;s retrieve the account record we created in our test setup method using SOQL. Then we can use the account&#8217;s Id to perform a mock REST API GET call. To accomplish this, an instance of a RestRequest is needed where the httpMethod is set to \u201cGET\u201d and the requestURI includes the account Id on the end. The request and the response are added to the RestContext like so&#8230;<\/p>\n<pre class=\"wp-block-code\"><code>@isTest \/\/positive unit test for the REST API GET call\r\n    static void testGetPositive() {\r\n        Account acc = [SELECT Id, Name FROM Account LIMIT 1];\r\n        RestRequest req = new RestRequest();\r\n        RestResponse res = new RestResponse();\r\n        req.requestURI = '\/services\/apexrest\/Account\/' + acc.Id;\r\n        req.httpMethod = 'GET';\r\n        RestContext.request = req;\r\n        RestContext.response= res;\r\n        Test.startTest();\r\n        SampleREST.doGet();\r\n        Test.stopTest();\r\n        SampleREST.AccountWrapper responseBody =(SampleREST.AccountWrapper)JSON.deserialize(res.responseBody.toString(), SampleREST.AccountWrapper.class);\r\n        System.assertEquals(acc.Id, responseBody.accid);\r\n        System.assertEquals(acc.Name, responseBody.accName);\r\n        System.assertEquals(200, res.statusCode);\r\n    }<\/code><\/pre>\n<p>Notice above how this unit test follows a natural and intuitive &#8220;given, when, then&#8221; pattern where the \u201cgiven\u201d conditions define the testable parameters, the \u201cwhen\u201d state is surrounded by <a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_testing_tools_start_stop_test.htm\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">.startTest() and .stopTest() methods<\/a> and \u201cthen\u201d there are assertions to confirm the outcome.<br \/>\nOk, let&#8217;s take a crack at <span style=\"text-decoration: underline;\"><strong>a negative unit test for a REST API GET call<\/strong><\/span>. Again, by a &#8216;negative test&#8217; we&#8217;re referring to the scenario where an account is not successfully retrieved and this is expected. To achieve this let&#8217;s use an account Id that does not exist in Salesforce. You can see in the assertion that the body of the response is \u201cNo account found with id 234576dsgfh36\u201d.<\/p>\n<pre class=\"wp-block-code\"><code>@isTest \/\/negative unit test for the REST API GET call\r\n    static void testGetNegative() {\r\n        RestRequest req = new RestRequest();\r\n        RestResponse res = new RestResponse();\r\n        req.requestURI = '\/services\/apexrest\/Account\/234576dsgfh36';\r\n        req.httpMethod = 'GET';\r\n        RestContext.request = req;\r\n        RestContext.response= res;\r\n        Test.startTest();\r\n        SampleREST.doGet();\r\n        Test.stopTest();\r\n        String responseBody = res.responseBody.toString();\r\n        System.assertEquals('No account found with id 234576dsgfh36', responseBody);\r\n        System.assertEquals(200, res.statusCode);\r\n    }<\/code><\/pre>\n<h3>Unit Testing POST Methods<\/h3>\n<p>Good news: The unit tests for a REST API POST call are very similar to the unit tests for a REST API GET call. The main difference is that here the RestRequest includes a \u201crequestBody\u201d.<br \/>\n<strong><span style=\"text-decoration: underline;\">A positive unit test for a REST API POST call<\/span><\/strong> will examine the creation of a Salesforce account record. The name and description of the account are set in an instance of a wrapper class used to pass the information into the body of the RestRequest. Next, we retrieve the resulting account record in Salesforce via SOQL and assert that the record details match the values from the body of the RestResponse.<\/p>\n<pre class=\"wp-block-code\"><code>@isTest \/\/positive unit test for the REST API POST call\r\n    static void testPostPositive() {\r\n        RestRequest req = new RestRequest();\r\n        RestResponse res = new RestResponse();\r\n        SampleREST.NewAccountRequest wrapper = new SampleREST.NewAccountRequest();\r\n        wrapper.name = 'Ismail Test Post';\r\n        wrapper.description = 'This is a test account';\r\n        req.requestURI = '\/services\/apexrest\/Account\/';\r\n        req.httpMethod = 'POST';\r\n        req.requestBody = Blob.valueOf(JSON.serializePretty(wrapper));\r\n        RestContext.request = req;\r\n   \tRestContext.response= res;\r\n        Test.startTest();\r\n        SampleREST.doPost();\r\n        Test.stopTest();\r\n        Account newAccountCreated = [SELECT Id, Name FROM Account WHERE Name = 'Ismail Test Post'];\r\n        SampleREST.AccountWrapper responseBody = (SampleREST.AccountWrapper)JSON.deserialize(res.responseBody.toString(), SampleREST.AccountWrapper.class);\r\n        System.assertEquals(newAccountCreated.Id, responseBody.accid);\r\n        System.assertEquals(newAccountCreated.Name, responseBody.accName);\r\n        System.assertEquals(201, res.statusCode);\r\n    }<\/code><\/pre>\n<p>Finally, let&#8217;s write <strong><span style=\"text-decoration: underline;\">a negative unit test for a REST API POST call<\/span><\/strong>. This time in the wrapper class sent to the RestRequest we will include null parameters. In Salesforce, we know to successfully create an account you must provide an account name and so when we pass in null we can be sure we will get back an HTTP status code of 500 (An Internal Server Error).<\/p>\n<pre class=\"wp-block-code\"><code>@isTest \/\/negative unit test for the REST API POST call\r\n    static void testPostNegative() {\r\n        RestRequest req = new RestRequest();\r\n        RestResponse res = new RestResponse();\r\n        SampleREST.NewAccountRequest wrapper = new SampleREST.NewAccountRequest();\r\n        wrapper.name = null;\r\n        wrapper.description = null;\r\n        req.requestURI = '\/services\/apexrest\/Account\/';\r\n        req.httpMethod = 'POST';\r\n        req.requestBody = Blob.valueOf(JSON.serializePretty(wrapper));\r\n        RestContext.request = req;\r\n   \tRestContext.response= res;\r\n        Test.startTest();\r\n        SampleREST.doPost();\r\n        Test.stopTest();\r\n        System.assertEquals(500, res.statusCode);\r\n    }<\/code><\/pre>\n<h3>Summary<\/h3>\n<p>The above examples for unit testing RESTful Apex Methods using HTTP GET and POST calls demonstrate the value and benefits of test setup methods, .startTest() and .stopTest() methods, meaningful assertions and both positive and negative test scenarios. I hope that this will form a solid jumping off point for you when approaching your own unit tests for RESTful Apex Methods. Wishing you much success in your Salesforce REST API Integrations. If you enjoyed this topic, then you might also enjoy the <a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/apex_integration_services\/apex_integration_webservices\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Apex Web Services Unit<\/a> on Trailhead.[\/vc_column_text][\/vc_column][\/vc_row][vc_row full_width=&#8221;stretch_row_content_no_spaces&#8221; row_gradient=&#8221;background: linear-gradient(112.25deg, rgb(0, 64, 110) -30.65%, rgb(0, 191, 218) 48.27%, rgb(0, 191, 218) 84.92%) repeat scroll 0% 0%, rgb(196, 196, 196) none repeat scroll 0% 0%;&#8221;][vc_column]\n<div class=\"wpcf7 no-js\" id=\"wpcf7-f4301-o1\" lang=\"en-US\" dir=\"ltr\" data-wpcf7-id=\"4301\">\n<div class=\"screen-reader-response\"><p role=\"status\" aria-live=\"polite\" aria-atomic=\"true\"><\/p> <ul><\/ul><\/div>\n<form action=\"\/ie\/wp-json\/wp\/v2\/posts\/2990#wpcf7-f4301-o1\" method=\"post\" class=\"wpcf7-form init\" aria-label=\"Contact form\" novalidate=\"novalidate\" data-status=\"init\">\n<fieldset class=\"hidden-fields-container\"><input type=\"hidden\" name=\"_wpcf7\" value=\"4301\" \/><input type=\"hidden\" name=\"_wpcf7_version\" value=\"6.2.2\" \/><input type=\"hidden\" name=\"_wpcf7_locale\" value=\"en_US\" \/><input type=\"hidden\" name=\"_wpcf7_unit_tag\" value=\"wpcf7-f4301-o1\" \/><input type=\"hidden\" name=\"_wpcf7_container_post\" value=\"0\" \/><input type=\"hidden\" name=\"_wpcf7_posted_data_hash\" value=\"\" \/>\n<\/fieldset>\n<div class=\"ctnContactSec\">\n\t<div class=\"container\">\n\t\t<div class=\"formWrap\">\n\t\t\t<div class=\"headingHead\">\n\t\t\t\t<h2 class=\"textUppercase fontAlter\">Contact Us\n\t\t\t\t<\/h2>\n\t\t\t\t<p>Any questions, or comments get in touch below.\n\t\t\t\t<\/p>\n\t\t\t<\/div>\n\t\t\t<div class=\"contactForm\">\n\t\t\t\t<p><span class=\"wpcf7-form-control-wrap\" data-name=\"text-971\"><input size=\"40\" maxlength=\"400\" class=\"wpcf7-form-control wpcf7-text wpcf7-validates-as-required formControl\" id=\"LastName\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Full Name\" value=\"\" type=\"text\" name=\"text-971\" \/><\/span><span class=\"wpcf7-form-control-wrap\" data-name=\"email-24\"><input size=\"40\" maxlength=\"400\" class=\"wpcf7-form-control wpcf7-email wpcf7-validates-as-required wpcf7-text wpcf7-validates-as-email formControl\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Email\" value=\"\" type=\"email\" name=\"email-24\" \/><\/span><span class=\"wpcf7-form-control-wrap\" data-name=\"text-43\"><input size=\"40\" maxlength=\"400\" class=\"wpcf7-form-control wpcf7-text wpcf7-validates-as-required formControl\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Job Title\" value=\"\" type=\"text\" name=\"text-43\" \/><\/span><span class=\"wpcf7-form-control-wrap\" data-name=\"text-151\"><input size=\"40\" maxlength=\"400\" class=\"wpcf7-form-control wpcf7-text wpcf7-validates-as-required formControl\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Company\" value=\"\" type=\"text\" name=\"text-151\" \/><\/span><span class=\"wpcf7-form-control-wrap\" data-name=\"textarea-405\"><textarea cols=\"40\" rows=\"10\" maxlength=\"2000\" class=\"wpcf7-form-control wpcf7-textarea wpcf7-validates-as-required formControl\" id=\"Message\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Message\" name=\"textarea-405\"><\/textarea><\/span><input class=\"wpcf7-form-control wpcf7-submit has-spinner btn btnDefaultWidth btnBgWhite\" id=\"submit\" type=\"submit\" value=\"Submit\" \/>\n\t\t\t\t<\/p>\n\t\t\t<\/div>\n\t\t<\/div>\n\t<\/div>\n<\/div><div class=\"wpcf7-response-output\" aria-hidden=\"true\"><\/div>\n<\/form>\n<\/div>\n[\/vc_column][\/vc_row]<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>[vc_row][vc_column][vc_column_text] A Step-by-Step Guide to Salesforce REST API Integration for Apex Developers (Part II) Author: Ismail Basser, Salesforce Consultant at Bluewave Technology Introduction In a previous post, we examined how a developer might handle data operations between Salesforce and an external system using a custom Apex class and REST API calls. We went through the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4205,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[7,10,13,15],"class_list":["post-2990","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles","tag-integration","tag-restful","tag-salesforce-development","tag-unit-tests"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Writing Unit Tests for RESTful Apex Methods | Bluewave IE<\/title>\n<meta name=\"description\" content=\"Let&#039;s examine how to properly unit test RESTful Apex methods. Specifically, how to \u2018mock\u2019 REST requests that get sent to Salesforce.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Writing Unit Tests for RESTful Apex Methods | Bluewave IE\" \/>\n<meta property=\"og:description\" content=\"Let&#039;s examine how to properly unit test RESTful Apex methods. Specifically, how to \u2018mock\u2019 REST requests that get sent to Salesforce.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/\" \/>\n<meta property=\"og:site_name\" content=\"Bluewave IE\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-23T08:35:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-17T13:20:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/bluewavecx.com\/ie\/wp-content\/uploads\/sites\/2\/2022\/09\/12.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1800\" \/>\n\t<meta property=\"og:image:height\" content=\"1013\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/blog\\\/writing-unit-tests-for-restful-apex-methods\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/blog\\\/writing-unit-tests-for-restful-apex-methods\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/#\\\/schema\\\/person\\\/3016812de924321d92720c9194e60684\"},\"headline\":\"Writing Unit Tests for RESTful Apex Methods\",\"datePublished\":\"2021-06-23T08:35:35+00:00\",\"dateModified\":\"2023-01-17T13:20:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/blog\\\/writing-unit-tests-for-restful-apex-methods\\\/\"},\"wordCount\":846,\"image\":{\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/blog\\\/writing-unit-tests-for-restful-apex-methods\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/09\\\/12.jpg\",\"keywords\":[\"Integration\",\"RESTful\",\"Salesforce Development\",\"Unit Tests\"],\"articleSection\":[\"Articles\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/blog\\\/writing-unit-tests-for-restful-apex-methods\\\/\",\"url\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/blog\\\/writing-unit-tests-for-restful-apex-methods\\\/\",\"name\":\"Writing Unit Tests for RESTful Apex Methods | Bluewave IE\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/blog\\\/writing-unit-tests-for-restful-apex-methods\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/blog\\\/writing-unit-tests-for-restful-apex-methods\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/09\\\/12.jpg\",\"datePublished\":\"2021-06-23T08:35:35+00:00\",\"dateModified\":\"2023-01-17T13:20:05+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/#\\\/schema\\\/person\\\/3016812de924321d92720c9194e60684\"},\"description\":\"Let's examine how to properly unit test RESTful Apex methods. Specifically, how to \u2018mock\u2019 REST requests that get sent to Salesforce.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/blog\\\/writing-unit-tests-for-restful-apex-methods\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/blog\\\/writing-unit-tests-for-restful-apex-methods\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/blog\\\/writing-unit-tests-for-restful-apex-methods\\\/#primaryimage\",\"url\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/09\\\/12.jpg\",\"contentUrl\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/09\\\/12.jpg\",\"width\":1800,\"height\":1013},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/blog\\\/writing-unit-tests-for-restful-apex-methods\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Writing Unit Tests for RESTful Apex Methods\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/#website\",\"url\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/\",\"name\":\"Bluewave IE\",\"description\":\"Just another bluewavegroup site\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/bluewavecx.com\\\/ie\\\/#\\\/schema\\\/person\\\/3016812de924321d92720c9194e60684\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/7186641f2ccd92cf6444aea05ab53d302332efd95634f833be3085ce466e00b6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/7186641f2ccd92cf6444aea05ab53d302332efd95634f833be3085ce466e00b6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/7186641f2ccd92cf6444aea05ab53d302332efd95634f833be3085ce466e00b6?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"http:\\\/\\\/bluewavegroup.flywheelstaging.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Writing Unit Tests for RESTful Apex Methods | Bluewave IE","description":"Let's examine how to properly unit test RESTful Apex methods. Specifically, how to \u2018mock\u2019 REST requests that get sent to Salesforce.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/","og_locale":"en_US","og_type":"article","og_title":"Writing Unit Tests for RESTful Apex Methods | Bluewave IE","og_description":"Let's examine how to properly unit test RESTful Apex methods. Specifically, how to \u2018mock\u2019 REST requests that get sent to Salesforce.","og_url":"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/","og_site_name":"Bluewave IE","article_published_time":"2021-06-23T08:35:35+00:00","article_modified_time":"2023-01-17T13:20:05+00:00","og_image":[{"width":1800,"height":1013,"url":"https:\/\/bluewavecx.com\/ie\/wp-content\/uploads\/sites\/2\/2022\/09\/12.jpg","type":"image\/jpeg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/#article","isPartOf":{"@id":"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/"},"author":{"name":"admin","@id":"https:\/\/bluewavecx.com\/ie\/#\/schema\/person\/3016812de924321d92720c9194e60684"},"headline":"Writing Unit Tests for RESTful Apex Methods","datePublished":"2021-06-23T08:35:35+00:00","dateModified":"2023-01-17T13:20:05+00:00","mainEntityOfPage":{"@id":"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/"},"wordCount":846,"image":{"@id":"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/#primaryimage"},"thumbnailUrl":"https:\/\/bluewavecx.com\/ie\/wp-content\/uploads\/sites\/2\/2022\/09\/12.jpg","keywords":["Integration","RESTful","Salesforce Development","Unit Tests"],"articleSection":["Articles"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/","url":"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/","name":"Writing Unit Tests for RESTful Apex Methods | Bluewave IE","isPartOf":{"@id":"https:\/\/bluewavecx.com\/ie\/#website"},"primaryImageOfPage":{"@id":"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/#primaryimage"},"image":{"@id":"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/#primaryimage"},"thumbnailUrl":"https:\/\/bluewavecx.com\/ie\/wp-content\/uploads\/sites\/2\/2022\/09\/12.jpg","datePublished":"2021-06-23T08:35:35+00:00","dateModified":"2023-01-17T13:20:05+00:00","author":{"@id":"https:\/\/bluewavecx.com\/ie\/#\/schema\/person\/3016812de924321d92720c9194e60684"},"description":"Let's examine how to properly unit test RESTful Apex methods. Specifically, how to \u2018mock\u2019 REST requests that get sent to Salesforce.","breadcrumb":{"@id":"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/#primaryimage","url":"https:\/\/bluewavecx.com\/ie\/wp-content\/uploads\/sites\/2\/2022\/09\/12.jpg","contentUrl":"https:\/\/bluewavecx.com\/ie\/wp-content\/uploads\/sites\/2\/2022\/09\/12.jpg","width":1800,"height":1013},{"@type":"BreadcrumbList","@id":"https:\/\/bluewavecx.com\/ie\/blog\/writing-unit-tests-for-restful-apex-methods\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/bluewavecx.com\/ie\/"},{"@type":"ListItem","position":2,"name":"Writing Unit Tests for RESTful Apex Methods"}]},{"@type":"WebSite","@id":"https:\/\/bluewavecx.com\/ie\/#website","url":"https:\/\/bluewavecx.com\/ie\/","name":"Bluewave IE","description":"Just another bluewavegroup site","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/bluewavecx.com\/ie\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/bluewavecx.com\/ie\/#\/schema\/person\/3016812de924321d92720c9194e60684","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/7186641f2ccd92cf6444aea05ab53d302332efd95634f833be3085ce466e00b6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/7186641f2ccd92cf6444aea05ab53d302332efd95634f833be3085ce466e00b6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7186641f2ccd92cf6444aea05ab53d302332efd95634f833be3085ce466e00b6?s=96&d=mm&r=g","caption":"admin"},"sameAs":["http:\/\/bluewavegroup.flywheelstaging.com"]}]}},"_links":{"self":[{"href":"https:\/\/bluewavecx.com\/ie\/wp-json\/wp\/v2\/posts\/2990","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluewavecx.com\/ie\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluewavecx.com\/ie\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluewavecx.com\/ie\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluewavecx.com\/ie\/wp-json\/wp\/v2\/comments?post=2990"}],"version-history":[{"count":2,"href":"https:\/\/bluewavecx.com\/ie\/wp-json\/wp\/v2\/posts\/2990\/revisions"}],"predecessor-version":[{"id":4911,"href":"https:\/\/bluewavecx.com\/ie\/wp-json\/wp\/v2\/posts\/2990\/revisions\/4911"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bluewavecx.com\/ie\/wp-json\/wp\/v2\/media\/4205"}],"wp:attachment":[{"href":"https:\/\/bluewavecx.com\/ie\/wp-json\/wp\/v2\/media?parent=2990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluewavecx.com\/ie\/wp-json\/wp\/v2\/categories?post=2990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluewavecx.com\/ie\/wp-json\/wp\/v2\/tags?post=2990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}