Sub topic 8
Testing with 200 records
This chapter assumes you can write an @IsTest method, use Assert.areEqual, and read basic SOQL and DML. It also assumes you know the per-transaction governor limits, such as 100 SOQL queries and 150 DML statements.
Why 200 is the number
Salesforce hands triggers records in chunks of up to 200. A data load, an API call, or a flow can all deliver a full chunk. Code that works for one record can still fail on 200. A SOQL query inside a for loop is the classic case. It uses 1 query for one record but 200 for a full chunk, which breaks the limit of 100.
A test that inserts one record cannot catch this. A bulk test uses a full chunk of 200 records and checks two things:
- The code stays within governor limits.
- The code gives correct results for every record in the chunk, not just the first.
The structure of a bulk test
Each step in a bulk test has a job.
Here is the service and its test:
public class AccountService {
public static void flagLargeAccounts(List<Account> accounts) {
for (Account a : accounts) {
if (a.AnnualRevenue != null && a.AnnualRevenue >= 1000000) {
a.Rating = 'Hot';
}
}
update accounts;
}
}
@IsTest
private class AccountServiceTest {
@TestSetup
static void setup() {
List<Account> accts = new List<Account>();
for (Integer i = 0; i < 200; i++) {
accts.add(new Account(
Name = 'Test Account ' + i,
AnnualRevenue = Math.mod(i, 2) == 0 ? 2000000 : 500
));
}
insert accts;
}
@IsTest
static void flagsOnlyLargeAccountsInBulk() {
List<Account> accts = [SELECT Id, AnnualRevenue, Rating FROM Account];
Test.startTest();
AccountService.flagLargeAccounts(accts);
Test.stopTest();
Integer hot = [SELECT COUNT() FROM Account WHERE Rating = 'Hot'];
Assert.areEqual(100, hot, 'Exactly the 100 large accounts should be flagged');
}
}What each part does
- Mixed data. Half the accounts have a revenue of 2,000,000 and half have 500. The test therefore checks that the code both flags and skips records. If every record matched, a bug that flags everything would pass.
@TestSetup. The method runs once, and each test method then starts with its own copy of that data. A change one test makes is rolled back before the next test runs.Test.startTest()andTest.stopTest(). Everything between the two calls gets a fresh set of governor limits. The 200 inserts insetupdo not count against the code under test. IfflagLargeAccountsran a query per record, the test would fail inside this block, which is the point of the test.- The re-query. The assertion reads from the database, not from the in-memory list. This confirms the
updatewas saved. It also picks up changes made by triggers or workflows that the list in memory would not show. - The expected count. The
100follows from the data: the even values ofifrom 0 to 198. The failure message says what was expected, so a failing run is easy to read.
Common errors
- Too few records. A loop of 5 records passes with a SOQL query in a loop. Use a full 200.
- Missing fields in the query.
SELECT Id FROM Accountfollowed by readingRatingthrows anSObjectException. List every field the code reads. - Weak assertions.
Assert.isTrue(hot > 0)passes if only one record was flagged. Assert the exact number. - Expecting data to carry over. Records created inside one test method do not exist in the next. Put shared data in
@TestSetup.
Exercise
In the test above, the Account query runs before Test.startTest(). Why is that sensible, and why does the final COUNT() query come after Test.stopTest()?
Show answer
The first query only prepares the input, so it should not use the fresh limits that are meant for the code under test. The COUNT() query is a check made after the code has finished, so it also belongs outside the measured block. It reads the saved database state, which confirms that the update was committed and that the flagged count is exactly 100.
Exercise
A test inserts 200 accounts that all have AnnualRevenue of 2000000, then asserts that 200 are flagged. Why is this a weaker test than the one in this chapter?
Show answer
A bug that flags every account would pass. Mixed data, with some records that should be flagged and some that should not, checks that the code makes the right choice for each record.