top of page

Tips and Tricks for Apex Testing

The Unit Test is a test performed by the developers to ensure that the performance is as good as expected while considering both the positive & Negative Tests. It is therefore mandatory to test your code before deploying it to Production and it should have a minimum of 75% encoding code i.e. if your code is of 100 lines then 75 lines should be covered in the test class otherwise the code will not get deployed into the production.

For example, if you have a trigger of "before insert" on Contacts, just create a new Contact in the test class to get the code entry.

Some Test Scenarios to Remember Before Writing a Test Class

Create all records in test class from scratch:

Test class cannot be connected to your organization's database (unless you specify @seeAllData = true ), so you need to create records to test. The second and most important thing is that if an item in the production environment is deleted or changed, the test class is unlikely to fail because it has created its own item.

Always use the @testSetup method:

You only need to create the record once and use it throughout your test class, so if you don't use @testSetup, you will have to create a new record set for different methods each time.

Always use System.assertEquals() to check whether your code has produced the expected result:

The syntax/format for System.assertEquals is “System.assertEquals(‘expected value’, ‘actual value’)”. It basically returns the boolean value depending on the values inside it, if both the values are the same it will return true otherwise false. For example: if code changes stage field of account from ‘Cold’ to ‘Hot’ then, “System.assertEquals('Hot', accList[0].Stage);”

Always use @isTest annotation instead of testMethod:

It is more flexible to use the @isTest annotation instead of the testMethod keyword, as you can specify parameters in the annotation, and also we can say that the @isTest annotation is considered the advanced version of the testMethod keyword.

Always use Test.startTest() and Test.stopTest():

It verifies that real code tests occur within the fresh governor limits. These methods can help to reset the governor limits before running the actual test code.

For example,


//your code……..


Test code for bulk records:

Assume only one record is being successfully inserted into the trigger but failed for 10 records (one trigger can handle up to 200 records). So always test your code against both single and multiple records at the same time.


Salesforce records that are created using test methods are not included in the database. They come back when the test is done. This behavior is ready for testing because you do not need to clean up test data after testing. By default, the Apex test class cannot access credentials available in the organization without access to setup and metadata objects, such as user or profile items. Set test data for your tests. Creating test data makes your testing more powerful and prevents failures caused by missing or changed data in the organization. You can create test data directly in your test method or by using the utility test class.

Follow us for more such posts...


bottom of page