@TestSetup Method – Testing – Salesforce Certified Platform Developer I Study Guide

9.4 @TestSetup Method

Here’s an example that demonstrates the usage of @TestSetup:

@isTest

private class ContactTriggerTest {

@TestSetup

static void setupTestData() {

// Create a test account

Account testAccount = new Account(Name = ‘Test Account’); insert testAccount;

// Create a test contact without setting standard field values

Contact testContactWithoutValues = new Contact( FirstName = ‘John’,

LastName = ‘Doe’,

AccountId = testAccount.Id

);

insert testContactWithoutValues;

}

@isTest

static void testContactTrigger() {

// Get test data

List<Contact> testContacts = [SELECT Id, FirstName, LastName FROM Contact];

  • Set up test data with desired field values List<Contact> updatedContacts = new List<Contact>(); for (Contact con : testContacts) {

con.FirstName = ‘Max’; con.LastName = ‘Blank’; updatedContacts.add(con);

}

  • Update the contacts

update updatedContacts;

// Verify that standard field values are set by the trigger

List<Contact> reloadedContacts = [SELECT Id, FirstName, LastName FROM Contact WHERE Id IN :testContacts];

for (Contact con : reloadedContacts) { System.assertEquals(‘Max’, con.FirstName); System.assertEquals(‘Blank’, con.LastName);

}

}

@isTest

static void testAnotherFunctionality() {

  • Since @TestSetup has already created test data, you can
  • use it here

List<Contact> testContacts = [SELECT Id, FirstName, LastName FROM Contact];

  • Add your test logic here for another functionality
  • For example, you can update the test contacts with new values for (Contact con : testContacts) {

con.Description = ‘New Description’;

}

  • Update the contacts

update testContacts;

  • Verify the results if needed
  • For example, you can query the updated contacts and assert
  • their new values

List<Contact> updatedContacts = [SELECT Id, Description FROM Contact WHERE Id IN :testContacts];

for (Contact con : updatedContacts) { System.assertEquals(‘New Description’, con.Description);

}

}

}

In the @TestSetup method, you’re creating a test account and a test contact without setting standard field values. So, it runs once before any test method in the class. The test account and contact created here serve as the common test data for the subsequent test methods in the same class.

Then two test methods testContactTrigger and testAnotherFunctionality use the test data created in @TestSetup for their respective test scenarios.

\ 1.\ In testContactTrigger, you’re updating the FirstName and LastName fields of the test contacts and verifying that a trigger on the Contact object correctly sets the standard field values. This method demonstrates testing a specific trigger functionality.

\ 2.\ In testAnotherFunctionality, you’re using the same set of test contacts to test another functionality updating the Description field. This method showcases how you can reuse the common test data created in setupTestData for testing additional functionalities without having to recreate the data.

Now, let’s address the SeeAllData=true attribute. In Apex, the SeeAllData=true attribute is used to indicate that test methods should have access to all data in the organization, including the organization’s existing data as in the following example.

@isTest(SeeAllData=true)

private class TestWithSeeAllData {

// Using SeeAllData=true, so no @TestSetup method is required

@isTest

static void testContactTrigger() {

// Get test data

List<Contact> testContacts = [SELECT Id, FirstName, LastName FROM Contact];

  • Set up test data with desired field values List<Contact> updatedContacts = new List<Contact>(); for (Contact con : testContacts) {

con.FirstName = ‘Max’; con.LastName = ‘Blank’; updatedContacts.add(con);

}

  • Update the contacts

update updatedContacts;

// Verify that standard field values are set by the trigger

List<Contact> reloadedContacts = [SELECT Id, FirstName, LastName FROM Contact WHERE Id IN :testContacts];

for (Contact con : reloadedContacts) { System.assertEquals(‘Max’, con.FirstName); System.assertEquals(‘Blank’, con.LastName);

}

}

@isTest

static void testAnotherFunctionality() { // Get test data

List<Contact> testContacts = [SELECT Id, FirstName, LastName FROM Contact];

  • Add your test logic here for another functionality
  • For example, you can update the test contacts with new values for (Contact con : testContacts) {

con.Description = ‘New Description’;

}

  • Update the contacts

update testContacts;

  • Verify the results if needed
  • For example, you can query the updated contacts and assert
  • their new values

List<Contact> updatedContacts = [SELECT Id, Description FROM Contact WHERE Id IN :testContacts];

for (Contact con : updatedContacts) { System.assertEquals(‘New Description’, con.Description);

}

}

}

In this modified example, I’ve removed the @TestSetup method because with SeeAllData=true, the test methods can access all data in the org directly.

However, it is generally not recommended to use SeeAllData=true because it can lead to test code dependency on specific data that may change over time. The @TestSetup annotation and SeeAllData=true cannot be used together. When @TestSetup is used, it automatically creates the required test data, so there is no need to access existing data using SeeAllData=true. It is best practice to use @TestSetup to create the necessary data for your tests, ensuring that your tests are self-contained and independent of external data.