Tips and Tricks for Test Class | Techila
single,single-post,postid-1779,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive

Tips and Tricks for Test Class


Tips and Tricks for Test Class

Why we need to write test class? Some people will answer like we need code coverage to deploy code to production, as we don’t have right to write code in production directly. Yes it is correct but not 100 percent. Test classes basically help us to test our functionality of code which is important to develop a better application. So we should not concentrate only on code coverage, all though it is required.

We should write proper test case to test all the scenarios. We should try to achieve 100 % coverage with all test scenarios like positive, negative and bulk unit test.

Below points should be considered when writing a test class:-

  • Test class must start with @isTest annotation if API version is above 25.
  • Test environment can support @testVisible and @testSetUp annotations as well
  • Unit test is to test particular block of code working properly or not.
  • Unit test method takes no argument(s), commits no data to the database, sends no email, and always flagged with testMethod keyword.
  • To deploy to production at least 75% code coverage is required and all the test cases should pass.
  • debug statements are not counted as a part of apex code coverage.
  • Test method and test classes are not counted as a part of code coverage.
  • We should not just focus on the percentage of code coverage, but we should make sure that every use case should be covered viz. Positive behavior, negative behavior, bulk action and single action.
  • Single Action: To verify that the single record produces the correct and expected result.
  • Bulk Action: Any apex trigger, class or extension must be invoked for 1-200 records.
  • Positive Behavior: Test every expected behavior that occurs through every expected permutation.
  • Negative Behavior: Test for future date, negative values, etc.
  • Restricted User: Test whether a user with restricted access is used in your code.
  • Conditional and ternary operators are not considered executed unless both positive and negative branches are executed.
  • Unit tests are class methods that verify whether a particular piece of code is error free or not.
  • Annotation @isTest in the test method is equivalent to testMethod keyword. Test method should always be static and DO NOT use void return type. Test class and method’s default Access Specifier is always ‘Private’ and hence no other Access Specifier is used.
  • Classes with isTest annotation can’t be an interface or enum. Test method code can’t be invoked by non test request. Salesforce API 28.0 and onwards, test methods cannot reside inside non test classes.
  • Use @Testvisible annotation to make visible private methods inside test classes. Test method cannot be used to test web service callouts. Instead use mock callouts. You cannot send email from test method.
  • User, profile organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent, ApexPage we can access without (seeAllData=true).
  • SeeAllData=true will not work for API 23 version earlier. Accessing static resource test records in test class e.g. List<Account> accList=Test.loadData(Account,SobjectType,’ResourceName’).
  • Create Test Factory class with isTest annotation to exclude from organization code size limit.
  • @testSetup to create test records once in a method and use in every test method in the test class.
  • We can run unit test for a specific class, set of classes and all classes. We can run unit test by using Salesforce Standard UI, IDE, Console, and API. Maximum number of test classes run per 24 hour of period is not grater of 500 or 10 multiplication of test classes of your organization. As apex runs in system mode so the permission and record sharing are not taken into account. So we need to use system.runAs to enforce record sharing.
  • runAs will not enforce user permission or field level permission.
  • Every test to runAs count against the total number of DML issued in the process.
  • Below code snippet help you to start writing test class :-


  • Test class definition :-

Sample 1-


private class MyTestClass {}


  • Sample 2 – Grant test classes to access all data in the organization.


private class MyTestClass {}

  • Sample 3: Specify whether test class execute during package installation or not


private class MyTestClass {}

  • Test method definition :-

Sample 1 – with testmethod keyword.

static testMethod void testName() {

// code_block


  • Sample 2 – Here we can avoid testMethod with @isTest annotation which will help us to get benefit of parameters individually incase not there in class level.

@isTest static void testName() {

// code_block


Below tips helps us to move towards 100 % code coverage:-

  1. Tips for standard controller

ApexPages.standradController con=ApexPages.StandradController(Needs to pass the instance of the standard/Custom Object);

  1. StandardSetcontroller

ApexPages.standradSetController con=ApexPages.StandradSetController(Needs to pass the list of the standard/Custom Object);

  1. For wrapper class

ClassName.WrapperclassName wrp=new ClassName.WrapperclassName();

  1. Test code catch block.

We need to create an exception in test method to cover .We can do in two different ways one suppose we want an exception on update .we can do like below.

Trick 1 –

Account acc =new Account();


insert/update acc;


catch(Exception ex){}

As mandatory fields are missing it will throw exception and it will cover the catch block.

Trick 2 – We need to add two line of code in class like below.


DML statement;


Integer intTest =20/0;


catch(Exception ex){

ApexPages.Message msg = new




There is a class named Test in apex which has some method which helps us to write some useful test case Test class method.

Thank you,


“Know more about Salesforce Consulting at Techila”

Author: techila

No Comments

Post A Comment