{"id":4752,"date":"2020-08-23T01:00:44","date_gmt":"2020-08-23T06:30:44","guid":{"rendered":"https:\/\/www.emizentech.com\/blog\/?p=4752"},"modified":"2022-01-21T11:41:22","modified_gmt":"2022-01-21T11:41:22","slug":"test-classes-in-salesforce","status":"publish","type":"post","link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/test-classes-in-salesforce.html","title":{"rendered":"Best Practices for Test Classes in Salesforce"},"content":{"rendered":"<p>In Salesforce it is most important to write test classes while deploying the code on Production. It helps us to do not write unused code in apex classes. While writing test classes we need to remember these testing principles.<\/p>\n<p>To deploy to production at-least 75% code coverage is required, but your focus should not be on the percentage of code that is covered. Instead, you should make sure that every use case of your application is covered, including positive and negative cases, as well as bulk and single records. This should lead to 75% or more of your code being covered by unit tests.<\/p>\n<p>1. <strong>@isTest<\/strong> must be used before starting the test class if the class version is more than 25.<\/p>\n<p>2. To define the test method, you can either use <strong>\u201c@isTest\u201d or \u201ctestMethod\u201d<\/strong> keyword with method.<\/p>\n<p>3. Stating with the salesforce API 28.0 test method cannot reside inside non test classes.<\/p>\n<p>4. Always use <strong>@testSetup<\/strong> method dedicated to create test records for that class. This is newly added annotation and very powerful.<\/p>\n<p>5. If possible Do not use seeAllData=true, Create your Own Test Data. <strong>SeeAllData=true<\/strong> will not work for <strong>API 23<\/strong> <strong>version<\/strong> earlier. User, profile, organization, AsyncApexjob, Corntrigger, Record Type, Apex Class, Apex Component, Apex Page we can access without <strong>(seeAllData=true)<\/strong>.<\/p>\n<p>6. Use <strong>Test.startTest()<\/strong> to reset Governor limits in Test methods.<\/p>\n<p>7. If you are doing any Asynchronous operation in code, then do not forget to call <strong>Test.stopTest()<\/strong> to make sure that operation is completed.<\/p>\n<p>8. Use <strong>System.runAs()<\/strong> method to enforce OWD and Profile related testings. This is very important from Security point of View. <strong>System.runAs()<\/strong> will not enforce user permission or field level permission.<\/p>\n<p>9. Use As much as Assertions like <strong>System.AssertEquals<\/strong> or <strong>System.AssertNotEquals<\/strong>.<\/p>\n<p>10. Always test Batch Capabilities of your code by passing 20 to 100 records.<\/p>\n<p>11. Always try to pass null values in every method. This is the area where most of program fails, unknowingly.<\/p>\n<p>12. Please use call out mock to test web-service call out.<\/p>\n<p>13. Maximum number of test classes run per 24 hours of period is not grater of 500 or 10 multiplication of test classes of your organization.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Test_Method_Syntax\"><\/span>Test Method Syntax<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Test methods take no arguments and have the following syntax:<\/p>\n<pre>@isTest static void testName() { \r\n\t\t\/\/ code_block \r\n}<\/pre>\n<p>Alternatively, a test method can have this syntax:<\/p>\n<pre>static testMethod void testName() { \r\n\t\t\/\/ code_block \r\n}<\/pre>\n<p>Test methods must be defined in test classes, which are classes annotated with\u00a0<strong>isTest<\/strong>. This sample class shows a definition of a test class with one test method.<\/p>\n<pre>@isTest \r\nprivate class MyTestClass { \r\n\t\t@isTest static void myTest() { \r\n\t\t\t\/\/ code_block \r\n\t\t} \r\n}<\/pre>\n<p><strong>Example of a test class with Trigger and Apex class both:<\/strong><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Apex_Trigger\"><\/span>Apex Trigger:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>trigger HelloWorldTrigger on Book__c (before insert) {\r\n\t\tBook__c[] books = Trigger.new;\r\n\t\tMyHelloWorld.applyDiscount(books);\r\n}<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Apex_Trigger_Helper_Class\"><\/span>Apex Trigger Helper Class:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>public class MyHelloWorld {\r\n\t\tpublic static void applyDiscount(Book__c[] books) {\r\n\t\t\tfor (Book__c b :books){\r\n\t\t\t\tb.Price__c *= 0.9;\r\n\t\t\t}\r\n\t\t}\r\n}<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Test_Class\"><\/span>Test Class:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>@isTest\r\nprivate class HelloWorldTestClass {\r\n \tstatic testMethod void validateHelloWorld() {\r\n \t\tBook__c b = new Book__c(Name='Behind the Cloud', Price__c=100);\r\n \t\tSystem.debug('Price before inserting new book: ' + b.Price__c);\r\n \t\tTest.startTest();\r\n \t\t\/\/ Insert book\r\n \t\tinsert b;\r\n \t\tTest.stopTest();\r\n \t\t\/\/ Retrieve the new book\r\n \t\tb = [SELECT Price__c FROM Book__c WHERE Id =:b.Id];\r\n \t\tSystem.debug('Price after trigger fired: ' + b.Price__c);\r\n \t\t\/\/ Test that the trigger correctly updated the price\r\n \t\tSystem.assertEquals(90, b.Price__c);\r\n\t\t}\r\n}<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Test_Class_for_Standard_Controller\"><\/span>Test Class for Standard Controller:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>@isTest \r\npublic class ExtensionTestClass {\r\n \tstatic testMethod void testMethod1() {\r\n \t\tAccount testAccount = new Account();\r\n \t\ttestAccount.Name='Test Account record' ;\r\n \t\tinsert testAccount;\r\n\r\n \t\tTest.StartTest(); \r\n  \t\tApexPages.StandardController sc = new \t\t\t\t\tApexPages.StandardController(testAccount);\r\n\t\tmyControllerExtension testAccPlan = new myControllerExtension(sc);\r\n\r\n  \t\tPageReference pageRef = Page.AccountPlan; \/\/ Add your VF page Name here\r\n  \t\tpageRef.getParameters().put('id', String.valueOf(testAccount.Id));\r\n  \t\tTest.setCurrentPage(pageRef);\r\n\r\n  \t\t\/\/testAccPlan.save(); call all your function here\r\n \t\tTest.StopTest();\r\n\t}\r\n}<\/pre>\n<p>Emizentech provides various <a href=\"https:\/\/multisitelocal.ezxdemo.com\/salesforce.html\">salesforce development<\/a> and <a href=\"https:\/\/multisitelocal.ezxdemo.com\/salesforce-consulting.html\">salesforce consulting services<\/a> for Salesforce app exchange, Paradot, Einstein, marketing cloud, IoT, and many more with the assistance of experienced <a href=\"https:\/\/multisitelocal.ezxdemo.com\/hire-salesforce-developer.html\">salesforce developers<\/a>. If you have a project in mind then let us know your requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Salesforce it is most important to write test classes while deploying the code on Production. It helps us to do not write unused code in apex classes. While writing test classes we need to remember these testing principles. To deploy to production at-least 75% code coverage is required, but your focus should not be<\/p>\n","protected":false},"author":39,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"MSN_Categories":"Uncategorized","MSN_Publish_Option":false,"MSN_Is_Local_News":false,"MSN_Is_AIAC_Included":"Empty","MSN_Location":"[]","MSN_Add_Feature_Img_On_Top_Of_Post":false,"MSN_Has_Custom_Author":false,"MSN_Custom_Author":"","MSN_Has_Custom_Canonical_Url":false,"MSN_Custom_Canonical_Url":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[87],"tags":[],"class_list":{"0":"post-4752","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-salesforce-development"},"modified_by":"emizentech","featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"Virendra Sharma","author_link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/author\/salesforce"},"_links":{"self":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/4752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/comments?post=4752"}],"version-history":[{"count":0,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/4752\/revisions"}],"wp:attachment":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media?parent=4752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/categories?post=4752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/tags?post=4752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}