{"id":5351,"date":"2016-02-22T02:51:04","date_gmt":"2016-02-22T02:51:04","guid":{"rendered":"http:\/\/www.jitendrazaa.com\/blog\/?p=5351"},"modified":"2017-05-10T14:49:13","modified_gmt":"2017-05-10T14:49:13","slug":"test-setup-method-in-apex-test-class","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/test-setup-method-in-apex-test-class\/","title":{"rendered":"Test Setup method in Apex"},"content":{"rendered":"<p style=\"text-align: justify;\">In <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex\/faq-writing-test-class-in-salesforce\/\">previous\u00a0blog post<\/a>, we discussed best practices of writing Test classes in Apex. In old school way, if we wanted to create records for test methods, helper or utility classes were favorite option for\u00a0developers.<\/p>\n<p style=\"text-align: justify;\">However this approach has few disadvantages<\/p>\n<ul>\n<li style=\"text-align: justify;\">Multiple Test classes could depend on these utility methods<\/li>\n<li style=\"text-align: justify;\">It creates tight coupling between classes<\/li>\n<li style=\"text-align: justify;\">Deployment could be slow if unnecessary test record being created for test methods<\/li>\n<li style=\"text-align: justify;\">If class to create test record is not marked with <strong>@isTest<\/strong> then it will count against allowed apex code in instance<\/li>\n<li style=\"text-align: justify;\">Test classes needs to explicitly call method to create test records<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<h3>Test Setup (@testSetup) Methods in Test Class<\/h3>\n<p>Apex has introduced new method in Test class known as &#8220;Test Setup&#8221;. Following are some highlights and considerations of using Test Setup method in Test classes :<\/p>\n<ul>\n<li>It needs to be marked with <strong>@testSetup<\/strong> annotation in Test class<\/li>\n<li>One Test class can have only one\u00a0<strong>@testSetup<\/strong> methods<\/li>\n<li>These test setup methods are implicitly invoked before each test methods<\/li>\n<li>These methods can be used to create test records specific to Test class<\/li>\n<li>It helps to keep Test classes isolated and independent of helper or utility classes<\/li>\n<li>It can&#8217;t be used if Test class is marked with <strong>@isTest(SeeAllData=true)<\/strong><\/li>\n<li>If error occurs in setup method then entire test class fails<\/li>\n<li>If non-test method is called from setup method then no code coverage is calculated for non-test method<\/li>\n<li>If multiple set up method is written in Test class then sequence of execution of those methods are not guaranteed<\/li>\n<\/ul>\n<p>Below class shows sample code of Test method in action. It creates 200 lead records in test setup method (<strong>@testSetup<\/strong>) and checks whether 200 records are created or not in test method.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@iSTest\r\npublic class LeadProcessorTest {   \r\n\r\n    \/\/below test setup method will be invoked\r\n    \/\/ implicitly before every test methods \r\n    @testsetup\r\n    static void createLead(){\r\n        List&lt;Lead&gt; lstLead = new List&lt;Lead&gt;();        \r\n        for(Integer i = 0 ; i&lt;200 ; i++) {\r\n            lstLead.add(new Lead(lastName = 'testLName'+i , Company = 'Salesforce'));\r\n        } \r\n        insert lstLead ;\r\n    }\r\n    \r\n   \/\/Check whether records created in test method\r\n   \/\/is accessible or not\r\n    public static testMethod void test(){\r\n        System.assertEquals(200, &#x5B;SELECT COUNT() FROM Lead Where company = 'Salesforce']);\r\n    }\r\n}\r\n<\/pre>\n<p>Resource : <a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_testing_testsetup_using.htm\">official Salesforce documentation<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to use setup method in Apex test classes<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[197],"class_list":["post-5351","post","type-post","status-publish","format-standard","hentry","category-salesforce","tag-testclass"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3694,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex\/faq-writing-test-class-in-salesforce\/","url_meta":{"origin":5351,"position":0},"title":"FAQ and Best Practices of Test Classes in Apex","author":"Jitendra","date":"February 15, 2014","format":false,"excerpt":"Here I am going to share few answers related to Test Classes, which is being asked many times to me by novice Programmers. Please feel free to post Comments if your question is not answered here. I will try my best to add those Questions in this article. If you\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1241,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/step-by-step-salesforce-tutorial-creating-trigger-and-test-cases-6-of-6\/","url_meta":{"origin":5351,"position":1},"title":"Step by Step Salesforce Tutorial \u2013 Creating Trigger and Test cases \u2013 6 of 6","author":"Jitendra","date":"October 11, 2010","format":false,"excerpt":"Step by Step Salesforce Tutorial \u2013 Creating Trigger and test cases \u2013 6 of 6 tutorials series","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Creating Trigger in Salesforce using force.com IDE","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Creating-Trigger-in-Salesforce-using-force.com-IDE.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":5152,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/using-test-loaddata-to-import-records-with-relationship\/","url_meta":{"origin":5351,"position":2},"title":"Using Test.loadData to import records with relationship","author":"Jitendra","date":"January 6, 2016","format":false,"excerpt":"There are many resources and documents available around how to use Test.loadData to create test records in Apex class. As per best practice of writing Test classes in Apex, Its good idea to store master data (aka Seed, Reference data) in static resource and load\u00a0it in Test classes using \"Test.loadData\"\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Test.loadData and Static resource in Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":3050,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/my-favorite-top-10-features-of-winter-13-release-salesforce\/","url_meta":{"origin":5351,"position":3},"title":"My Favorite Top 10 Features of Winter 13 release &#8211; Salesforce","author":"Jitendra","date":"August 24, 2012","format":false,"excerpt":"Dear Friends, I am very excited to write this article about the cool Winter 13 features which i have added in My List. There are lots of lots of new features added in this release and i have made the list of my top 10 favorite features. Yesterday Salesforce published\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Winter 13 Release Notes","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Salesforce-Winter-13-Release-Notes-300x212.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":5562,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/summer-16-top-features\/","url_meta":{"origin":5351,"position":4},"title":"Salesforce Summer 16 &#8211; My favorite top 20 features","author":"Jitendra","date":"June 28, 2016","format":false,"excerpt":"List of Salesforce Summer 16 features","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"User Switcher in Salesforce Summer 16","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/06\/User-Switcher-in-Salesforce-Summer-16.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":4942,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-winter-16-top-features\/","url_meta":{"origin":5351,"position":5},"title":"Salesforce Winter 16 &#8211; Top features","author":"Jitendra","date":"October 28, 2015","format":false,"excerpt":"You can watch recording of my live stream channel as well to know more on Winter 16 features.\u00a0Powerpoint presentation used in livestream can be accessed from here. 1. Object Manager Objects are objects, even if they are standard or custom objects. Salesforce simplified navigation to access Objects, we can access\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Setup Audit Trail - Salesforce Winter 16","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/10\/Setup-Audit-Trail.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/10\/Setup-Audit-Trail.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/10\/Setup-Audit-Trail.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/10\/Setup-Audit-Trail.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=5351"}],"version-history":[{"count":11,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5351\/revisions"}],"predecessor-version":[{"id":6039,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5351\/revisions\/6039"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=5351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=5351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=5351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}