Testing Bulk Operations
By the end of this lesson, you'll be able to:
- Write a test that exercises trigger logic against 200 records at once
- Explain why testing with a single record can hide bulk-related bugs
Prerequisites: Code Coverage Requirements
Why one record isn't enough
SOQL/DML-inside-a-loop code often works fine with a single record, since the loop only runs once — it passes review and single-record tests, then hits governor limits the moment someone does a real bulk data load or Data Loader import in production. A bulk-safe test is the only way to catch this before it ships.
Writing a bulk test
Build a List of 200 records — the real maximum trigger batch size — in a loop, insert them all in a single DML statement, then assert the expected outcome across the whole batch, not just one spot-checked record.
A realistic 200-record bulk test
@isTest
static void triggerHandlesA200RecordBulkInsert() {
List<Account> accounts = new List<Account>();
for (Integer i = 0; i < 200; i++) {
accounts.add(new Account(Name = 'Bulk Account ' + i));
}
Test.startTest();
insert accounts;
Test.stopTest();
List<Account> inserted = [SELECT Id FROM Account];
System.assertEquals(200, inserted.size());
}
200 is the real batch size Salesforce uses for trigger invocations — a test at this scale would immediately fail if the trigger handler had SOQL or DML inside a per-record loop.
Exercise
Write a test that inserts 200 Contacts in one bulk DML statement and asserts all 200 were created.
Show hint
Build the List<Contact> in a for loop, then use a single insert statement outside the loop.
Testing Bulk Operations — Quick Check
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
A test that only inserts one record can pass even when the underlying code has SOQL or DML inside a loop — testing with a full bulk batch (200 records, matching Salesforce's actual trigger batch size) is the only reliable way to catch that class of bug before production.