Testing the application before moving it to Production is very important. It is a critical part of the software development life cycle. Testing also lets the developer understand the efficiency of the code. Test class in Salesforce also does the same thing.

What is test class in Salesforce

We know Salesforce runs on a multi-tenancy architecture where no particular could monopolize the resources. Test class in Salesforce are written to make the code efficient by having a good code coverage. Code coverage determined how many lines of your actual code is covered and useful so that you can remove the code which is not at all related to your expected functionality at all if any.

  • Test class must cover at least 75% of your Apex code, and all the test should be completed successfully.
  • If you are deploying an Apex to a production org, each unit test in your org namespace is executed by default unless you select “run specific class”.
  • Calls to System.debug are not counted as code coverage part.
  • Though only 75% of the Apex code must be covered but do not focus on the %, but focus on the positive or negative use cases and single or bulk record operations. 
  • Test class codes will not be considered in overall code coverage of the Apex. Means, if your test class has 200 lines or 500 lines it will not matter. The only thing which will matter is how many lines of actual Apex say, controller, trigger, etc is covered in the test class.
    • Triggers also must have some test coverage.
    • All triggers and classes must compile successfully.

How to write test class in Salesforce

You can write a test class in the Salesforce class code editor or using the developer console.

  • For Salesforce class editor you need to navigate setup→ Quick Find→ Apex Classes
  • For Developer console, you will need to open Developer console → File→ New→ Apex Class

Each test class will have the similar structure as mentioned below:

  • Test class in Salesforce starts with @isTest annotation which shows that this particular apex class is a test class.
  • Setup of test data. This includes the creation of any data needed by your class. 
  • Starting the test by calling Test.startTest() to reset the governor limits.
  • Calling your class/method
  • Stopping the test by calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish.
  • Asserting that your changes have worked
  • ​If you have made any change o the record, you need to query for the updates
  • Run System.assert, System.assertNotEquals,System.assertEquals to verify that you got the correct data.

Test class example

This is the simple Apex class we have created:

test class in salesforce
public Class CreateAccounts{
public Account createAccount(String name)  //this is a method to create account
{
Account acc = new Account();
acc.Name = name;
return acc;
}
}

This is a simple code that will return an Account with the name which we will pass as a parameter.

Below is the Test class that is written for the apex class:

Apex test class

And to check the coverage of the class you will need to run the test class by clicking on the button Run Test:

Run test class in salesforce
@isTest
public class CreateAccountTest
{
  static testMethod void testInsertAccount()
  {
  CreateAccounts ca = new CreateAccounts();
  ca.createAccount('TestclassAcc1');
  }
}

I prefer writing code using developer console, We can write the same Apex class and test class in the developer console as well:

developer console test class

In the above image, you can see there is a button Run Test which you can use to run this test class and check the code coverage:

developer console

Run the test class with the Run test button, expand the results in the Tests section of the console. And click on the particular class on the bottom right overall Code coverage section.

You will see what are the lines which are covered, in our case 4/4 means 100% code is covered.

test class methods

All test methods are static. Now you may ask why are they static?

test methods are not called explicitly when you run a test class for execution. When you run your apex tests these methods need to be executed whether they are called or not.

Some of the most used test methods are :

  • isRunningTest() Returns true if the code being executed currently was called by code contained in a test method, false otherwise. This method should be used if you need to run different code based on whether it was being called from a test.
  • StartTest() It points in your test code when your test actually starts. Use this method when you need to consider the governor limits.
  • stopTest() It points in your test code when your test actually stops. Use this method together with the startTest method.

For the complete list, you can check out Salesforce test Methods

test class best practices in salesforce

  • IDs should not be hardcoded for an sObject in test methods.
  • Name of the test class must be appended with the test keyword at the end of the class name. example MyClassNameTest
  • Test class should start with @Test annotation
  • Code coverage should be at least 75% to be deployed in Production.
  • Use isTest(SeeAllData = true) on class methods in exceptional cases only where the sObjects don’t allow DML operation e.g. PriceBook creation.
  • Any asynchronous method testing should have Test.stopTest in conjunction with Test.startTest

For Hands-on do check Get Started with Apex Unit Tests.

If you are preparing for Salesforce Interview check out: Salesforce Interview questions you must know in-2020

Categories: Salesforce Blog

0 Comments

Did you Like the content? Let me know