Hey team, today I’d like to walk you through an Apex test class that performs integration testing for standard Salesforce objects like Account, Contact, and Opportunity. Whether you’re new to testing in Salesforce or just need a refresher, this article will help you break down and understand how to write an effective test class in Apex.
Here’s the piece of code we’ll be discussing:
View the complete code example
@isTest
public class AccountContactOpportunityIntegration {
@isTest
static void testAccountContactOpportunityIntegration() {
// Start the test
Test.startTest();
// Step 1: Create a test Account
Account testAccount = new Account(
Name = 'Integration Test Account'
);
insert testAccount;
// Step 2: Create a test Contact linked to the Account
Contact testContact = new Contact(
FirstName = 'Integration',
LastName = 'Test',
Email = '[email protected]',
AccountId = testAccount.Id
);
insert testContact;
// Step 3: Create a test Opportunity linked to the Account
Opportunity testOpportunity = new Opportunity(
Name = 'Integration Test Opportunity',
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30),
AccountId = testAccount.Id
);
insert testOpportunity;
// Step 4: Retrieve and validate relationships between Account, Contact, and Opportunity
Account retrievedAccount = [SELECT Id, Name, (SELECT Id, FirstName, LastName FROM Contacts), (SELECT Id, Name FROM Opportunities) FROM Account WHERE Id = :testAccount.Id];
// Assert Account properties
System.assertEquals('Integration Test Account', retrievedAccount.Name, 'Account Name should match.');
// Assert Contact association
System.assertEquals(1, retrievedAccount.Contacts.size(), 'There should be one Contact associated with the Account.');
System.assertEquals('Integration', retrievedAccount.Contacts[0].FirstName, 'Contact First Name should match.');
System.assertEquals('Test', retrievedAccount.Contacts[0].LastName, 'Contact Last Name should match.');
// Assert Opportunity association
System.assertEquals(1, retrievedAccount.Opportunities.size(), 'There should be one Opportunity associated with the Account.');
System.assertEquals('Integration Test Opportunity', retrievedAccount.Opportunities[0].Name, 'Opportunity Name should match.');
// End the test
Test.stopTest();
}
}
Step-by-Step Breakdown
1. Test Class and Method Structure
@isTest
Annotation: This indicates that the class and method are specifically meant for testing. It ensures that the code is executed in a test context and doesn’t affect your actual database.- Method Declaration: The method
testAccountContactOpportunityIntegration
is defined asstatic
since test methods should not rely on instance variables or state.
2. Starting and Stopping a Test
Test.startTest()
andTest.stopTest()
: These methods are used to separate the test execution from the setup. When you start a test, Salesforce provides a fresh set of governor limits. This is crucial when testing bulk operations or scenarios that require SOQL or DML statements.
3. Creating Mock Data
Step 1: Create a Test Account:Account testAccount = new Account( Name = 'Integration Test Account' );
insert testAccount;
We create an Account
object with a Name
value. This object is then inserted into the database using the insert
DML statement.
Step 2: Create a Test Contact Linked to the Account:Contact testContact = new Contact( FirstName = 'Integration', LastName = 'Test', Email = '[email protected]', AccountId = testAccount.Id );
insert testContact;
A Contact
is created and linked to the previously created Account
using the AccountId
field. This ensures the proper parent-child relationship between the records.
Step 3: Create a Test Opportunity Linked to the Account:Opportunity testOpportunity = new Opportunity( Name = 'Integration Test Opportunity', StageName = 'Prospecting', CloseDate = Date.today().addDays(30), AccountId = testAccount.Id );
insert testOpportunity;
Similar to the Contact
, we create an Opportunity
and link it to the Account
. Key fields like StageName
and CloseDate
are required to make the record valid.
4. Validating Relationships
After creating the mock data, we retrieve the Account
along with its related Contact
and Opportunity
using SOQL subqueries:
Account retrievedAccount = [SELECT Id, Name, (SELECT Id, FirstName, LastName FROM Contacts), (SELECT Id, Name FROM Opportunities) FROM Account WHERE Id = :testAccount.Id];
- Subqueries:
(SELECT Id, FirstName, LastName FROM Contacts)
retrieves all relatedContact
records.(SELECT Id, Name FROM Opportunities)
retrieves all relatedOpportunity
records.
5. Assertions
Assertions ensure that the data and relationships are correct:
- Verify Account Data:
System.assertEquals('Integration Test Account', retrievedAccount.Name, 'Account Name should match.');
Ensures theAccount
name matches the expected value. - Verify Contact Data:
System.assertEquals(1, retrievedAccount.Contacts.size(), 'There should be one Contact associated with the Account.'); System.assertEquals('Integration', retrievedAccount.Contacts[0].FirstName, 'Contact First Name should match.');
Ensures theContact
is correctly linked to theAccount
and has the expected data. - Verify Opportunity Data:
System.assertEquals(1, retrievedAccount.Opportunities.size(), 'There should be one Opportunity associated with the Account.');
System.assertEquals('Integration Test Opportunity', retrievedAccount.Opportunities[0].Name, 'Opportunity Name should match.');
Ensures theOpportunity
is correctly linked to theAccount
.
Why This Test is Important
- Data Integrity: Verifies that the relationships between standard objects are set up correctly.
- Real-World Scenarios: Simulates business use cases where
Accounts
often have relatedContacts
andOpportunities
. - Validation: Ensures data consistency and accuracy across multiple objects.
- Governor Limits: Helps test how the code performs under real-world constraints.