The Test.startTest() and Test.stopTest() methods in Salesforce Apex are used to delineate a specific section of code within a test method. This is useful for isolating and measuring the performance of a specific piece of code and for ensuring that asynchronous code, such as Batch Apex or future methods, is executed within the test context in the following example.
Step 1: You can create the new class MyBatchClass in Developer Console.
public class MyBatchClass implements Database.Batchable<sObject> {
public Database.QueryLocator start(Database.BatchableContext context) {
‘SELECT Id, Email FROM Contact WHERE Email = null OR Email = \’\”; return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext context, List<Contact> contacts) {
for (Contact con : contacts) {
if (String.isBlank(con.Email)) {
con.Email = ‘[email protected]’;
}
}
update contacts;
}
public void finish(Database.BatchableContext context) { // Optional: Add any finishing logic here
}
}
Step 2: Now you can create the test class MyBatchClassTest and run the test for it.
@isTest
private class MyBatchClassTest {
@isTest
static void testBatchClass() {
// Create test data
List<Contact> testContacts = new List<Contact>(); for (Integer i = 0; i < 200; i++) {
testContacts.add(new Contact(
FirstName = ‘Test’,
LastName = ‘Contact’ + i,
Email = ”
));
}
insert testContacts;
MyBatchClass myBatch = new MyBatchClass(); Database.executeBatch(myBatch);
List<Contact> updatedContacts = [SELECT Id, Email FROM Contact WHERE Email = ‘[email protected]’]; System.assertEquals(testContacts.size(), updatedContacts.size(), ‘Match the number of test contacts’);
for (Contact con : updatedContacts) { System.assertEquals(‘[email protected]’, con.Email, ‘Email should be updated to [email protected]’);
}
}
}
The provided Apex code is an example of Batch Apex, a mechanism for processing large datasets asynchronously. The MyBatchClass class updates contact records with empty email addresses to “[email protected]”. The corresponding test class, MyBatchClassTest, creates test contacts, runs the batch job, and verifies the expected updates. The use of Test.startTest() and Test.stopTest() ensures proper testing of asynchronous behavior. This code structure allows for efficient processing of data in chunks, addressing governor limits and bulk data scenarios.
Salesforce provides different environments to support the various stages of software development, testing, and production for organizations. These environments are designed to help ensure that changes and updates to Salesforce configurations and customizations are thoroughly tested and validated before being deployed to the live, operational environment. Here’s an overview of these different environments.
Full Copy Sandbox
A Full Copy Sandbox is typically used for comprehensive testing, quality assurance, and training purposes. It replicates your entire production org, including all data and metadata. It allows you to test changes and configurations in an environment that closely resembles your production org. This environment is useful for testing complex scenarios, large-scale data migrations, and performance testing.
Partial Copy Sandbox
A Partial Copy Sandbox is similar to a Full Copy Sandbox, but it contains a subset of your production data. It is useful for testing scenarios that require a representative sample of data, but not the entire dataset. This environment strikes a balance between having realistic data for testing and reducing the storage requirements. It is commonly used for functional testing, user acceptance testing, and load testing and typically come with a storage limit of 5 GB.