Best Practices in Salesforce to Write Test Classes for 100% Code Coverage

Introduction

Test classes in Salesforce is an Apex Class that is used to test logics written in classes.

We write Salesforce Test Classes for:

  • Apex Classes
  • Apex Triggers
  • Batch Apex Classes
  • Queueable Apex Classes
  • Visualforce Controllers
  • Visualforce Extensions
  • Future Apex Classes

Test Classes in Salesforce ensures that every logic is working as expected in both positive and negative testing as required. Test classes don’t count overall coverage in class. Test classes Witten with @isTest annotation on the top of apex class or on the top of the methods that we want to test.Syntax:

@isTest
private class MyApexTestClass {
    @isTest static void myTestMethod() {
        // code block;
    }
}

Use of Test Method to Write test Class:

Once Apex class is completed with your proper line of code and working properly or not, it must go with Testing in order to check whether your code function is properly working. the production. And also your apex line of code must cover at least 75% of code coverage before deploying in the production environment. testMethod keyword( on method) is written in Apex Class.

Example:

Static testMethod void methodName(){};

Use of @isTest Annotation to Write test Class:

@isTest Annotation is used with a class name or with method(function) name in test Class. Which defines class is written for testing the apex code, and also these classes are not counted against our organization limit of apex classes.

Example:

@isTest
private class MyApexTestClass {
    @isTest static void myTestMethod() {
        // code block
    }
}

@TestVisible Annotation Use:

This annotation helps to access private and protected (Data or function) members of other classes into the test class. And test classes can access those members for testing code. This annotation is written before the methods(functions).

Example:

Public class MyTestClass {
    @TestVisible private String name=’Test’;
    @TestVisible private Static void myTestMethod(){}
}

Use of IsTest (SeeAllData=true) Annotation to Write test Class:

Avoid using this annotation unless it is needed because this grants access to all data in the organization.

Use Method Test.startTest() and Test.stopTest():

Use these methods to test governor limits and increase governor limits of the salesforce.

Use of System.Assert() Statement to Write test Class :

It accepts two arguments(Parameters), 1st (required) is the condition to test and the 2nd is used to display a message in case the condition provided fails.

Syntax:

System.assert(input1 == input2,”Show Message”);

Use of System.AssertEquals to Write test Class: It accepts three parameters(arguments), the first two (mandatory) are the variables that will be tested for equality or inequality, and the third one (optional)  is used to display a message in case the condition gets failed.

Syntax:

System.assertEquals(inputValue1, inputValue1,”ShowDisplayMessage”);

Avoid Hard-Coded IDs in test Class:

Do not hardcode ids of any sObjects inside the test method of the test class, but if a constant needs to be asserted, then make use of constant class (custom Settings) because hard coding strings in unit tests may result in test failure when things like picklist values get changed.

Use of System.runAs() method to Write test Class:

System.runAs() helps us to write test methods that change user contexts to either an existing user (or a new user). You can use runAs() only in a test method. But runs() does not validate

Create,Read, Update and Delete(CRUD) or field-level security permissions in your test class.

Popular Salesforce Blogs