Basics of Writing a Salesforce Test Class

In this blog post, I will cover some basic concepts while writing your Test class in Salesforce.I will be using some code snippets to illustrate :

private class Test_CountContactOfAccount {
static testMethod void CountContactOfAccountUnitTest() {

seeAllData: Using this annotation isTest(seeAllData=true) you will be able to access the records in your organization.If you are fetching records from the database in your test class, then set this annotation value to true. There is few object like ‘opportunityLineItem’ for which we cannot create test data so to test with ‘opportunityLineItem’ we need to access this object records from our salesforce org so in that case, we will set the value of seeAllData to true.

But if you are inserting test records for an object and then you want to query them in your test class, in that case, you need to setup seeAllData to false. Otherwise, test class will not get executed.

Important Consideration for the isTest(SeeAllData=true) annotation:

If a test class is defined with the isTest(SeeAllData=true) annotation, then this annotation will be applied to all the test methods no matter whether the individual test methods are defined with the @isTest annotation or the testMethod keyword.

The isTest(SeeAllData=true) annotation is used to enable data access when applied to the class or the method level. However, if you are using isTest(SeeAllData=false) on a method,
it won’t restrict organization data access for that method if the containing class is defined with isTest(SeeAllData=true ) annotation. In this case, the method will have access to all the data in the organization.

The second annotation which we will cover is isTest(OnInstall=true) Annotation.

Use the @isTest(OnInstall=true) annotation to define which Apex tests are executed during the installation of the package. This annotation is used for tests in unmanaged or managed packages.
The test methods with this annotation or methods inside a test class which has this annotation will be executed during package installation. Test methods annotated with isTest(OnInstall=true) must pass in order for the package installation to be successful. It is no longer possible to bypass the failing tests during package installation. a test method or a class which doesn’t  have this annotation or if it defined with isTest(OnInstall=false) won’t be executed during installation.

Check out the below example:

public class OnInstallClass {
// Implement logic for the class.
public void method1(){
// Some code

private class OnInstallClassTest {
// This test method will be executed
// during the installation of the package.
static void test1() {
// Some test code

// Tests excluded from running during the
// the installation of a package.

static void test2() {
// Some test code

static testmethod void test3() {
// Some test code

Check out the Salesforce Platform Developer 1 course to understand more about Salesforce development and how to write test classes.

Salesforce Platform Developer 1 Certification Course


Leave a Reply

%d bloggers like this:
Skip to toolbar