How to Write Test Cases – Testing – Salesforce Certified Platform Developer I Study Guide

9.1  How to Write Test Cases

Unit tests in Apex are written using the Apex testing framework, which provides a set of classes and methods specifically designed for testing Apex code. These tests are typically written by developers to validate the functionality of their code and ensure that it meets the desired requirements.

In Salesforce, before moving the code to production, a minimum of 75% code coverage is required. This means that at least 75% of your Apex code must be covered by unit tests. Code coverage is a measure of how much of your Apex code is executed during the running of your tests. Each line of code that is executed during a test is considered as covered, while lines that are not executed are considered as uncovered. To check the overall code coverage in Salesforce, you can go in Developer Console to the Tests tab and check the Overall Code Coverage panel, which displays the code coverage percentage for every Apex class in the organization that has been tested. Alternatively you can go with Salesforce user interface to Setup and then to Apex Test Execution.

The @isTest annotation in Apex is used to define a class or method as a test class or test method, respectively. This annotation is used to indicate that the code within the annotated class or method is intended for testing purposes and should not be counted against the organization’s limit for Apex code. Test classes and methods are used to verify the behavior and functionality of Apex classes and triggers. They are executed separately from the main code and are used to ensure that the code is working as expected. Test methods take no arguments and commit no data to the database.

Positive and negative tests are two different types of tests used to validate the behavior of a system or a specific piece of code. Positive tests typically cover scenarios where valid inputs are provided, and the system is expected to produce the correct output or behavior. Examples of positive tests include

–– Testing a login functionality with valid credentials

–– Verifying that a calculation method returns the correct result with valid input values

–– Checking that a form validation process accepts valid data

Negative tests are crucial to ensuring the system’s robustness and its ability to handle errors gracefully. Examples of negative tests include

–– Providing invalid login credentials and verifying that the system rejects them

–– Testing a calculation method with invalid or out-of-range input values and ensuring it handles the errors appropriately

–– Entering invalid data into a form and checking that the validation process detects and rejects it

Write unit tests in Developer Edition or a sandbox to ensure that each unit of code like class or method works as expected. Now, we will revisit the first trigger example ContactTrigger from Chapter 5 and create test class by using three different assert methods.

Step 1: Open Developer Console ➤ File ➤ New ➤ Apex Class “TestContactTrigger” ➤ Insert the code

@isTest

private class TestContactTrigger {

@isTest

static void testContactTriggerWithSystemEquals() {

  • Test case using System.assertEquals
  • Create a new contact without setting FirstName and LastName

Contact testContact = new Contact();

insert testContact;

  • Retrieve the contact from the database to ensure trigger
  • logic is applied

Contact insertedContact = [SELECT Id, FirstName, LastName FROM Contact WHERE Id = :testContact.Id];

  • Verify that the trigger correctly set the standard field values
  • using System.assertEquals

System.assertEquals(‘Max’, insertedContact.FirstName, ‘FirstName should be set to “Max”.’); System.assertEquals(‘Blank’, insertedContact.LastName, ‘LastName should be set to “Blank”.’);

}

@isTest

static void testContactTriggerWithSystemAssertNotEquals() {

  • Test case using System.assertNotEquals
  • Create a new contact without setting FirstName and LastName

Contact testContact = new Contact();

insert testContact;

  • Retrieve the contact from the database to ensure trigger logic
  • is applied

Contact insertedContact = [SELECT Id, FirstName, LastName FROM Contact WHERE Id = :testContact.Id];

  • Verify that the trigger did not set unexpected values using
  • System.assertNotEquals

System.assertNotEquals(‘Unexpected Value’, insertedContact.FirstName, ‘FirstName should not be “Unexpected Value”.’); System.assertNotEquals(‘Unexpected Value’, insertedContact.LastName, ‘LastName should not be “Unexpected Value”.’);

}

@isTest

static void testContactTriggerWithSystemAssert() {

  • Test case using System.assert
  • Create a new contact without setting FirstName and LastName Contact testContact = new Contact();

insert testContact;

  • Retrieve the contact from the database to ensure trigger logic
  • is applied

Contact insertedContact = [SELECT Id, FirstName, LastName FROM Contact WHERE Id = :testContact.Id];

  • Verify that the trigger correctly set the standard field values
  • using System.assert

System.assert(insertedContact.FirstName == ‘Max’, ‘FirstName

should be set to “Max”.’);

System.assert(insertedContact.LastName == ‘Blank’, ‘LastName

should be set to “Blank”.’);

}

}

Step 2: Save new created test class and click the button “Run Test.”

Step 3: Check the test result of the three methods in the tab “Test.”

\ 1.\ Use System.assertEquals method to ensure that the actual values match the expected values. If the values don’t match, the test fails.

\ 2.\ Use System.assertNotEquals method to ensure that the actual values do not match the unexpected values. If the values match, the test fails.

\ 3.\ Use System.assert method with equality checks to verify that the actual values match the expected values. If the Boolean conditions are not met, the test fails. Note that System.assert is a more general assertion, and it can be used for various conditions beyond equality checks.