Sub topic 13
Testing with 200 records
This chapter assumes you can write basic Apex classes, run SOQL and DML, and have read the earlier chapters on bulk SOQL, bulk DML, and the handler pattern. Here you will write a test that proves a trigger works at full batch size.
Why test with 200 records
A trigger receives up to 200 records per invocation. A test that saves one record cannot reveal a SOQL query inside a loop, because one query is far below the limit of 100 per synchronous transaction. The same code fails when 101 or more records arrive together.
A bulk test inserts or updates 200 records in one DML statement. If any governor limit is exceeded, the test fails with a limit exception. That makes the test an automatic check on your bulkification.
A bulk test for the Opportunity trigger
The handler from the previous chapter creates one Task for each opportunity that moves to Closed Won. This test checks it at full batch size.
@IsTest
private class OpportunityTriggerTest {
@IsTest
static void createsOneTaskPerWonOpportunityInBulk() {
List<Opportunity> opps = new List<Opportunity>();
for (Integer i = 0; i < 200; i++) {
opps.add(new Opportunity(
Name = 'Bulk ' + i,
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30)
));
}
insert opps;
for (Opportunity o : opps) {
o.StageName = 'Closed Won';
}
Test.startTest();
update opps;
Test.stopTest();
Integer taskCount = [SELECT COUNT() FROM Task WHERE WhatId IN :opps];
System.assertEquals(200, taskCount, 'Expected one task per won opportunity');
}
}How the test works
- Build 200 records in a list. The loop only creates objects in memory. It runs no queries and no DML.
- Insert them with one statement. This fires the trigger once with 200 records, which tests the insert path. On insert,
Trigger.oldMapis null, and the handler must cope with that. - Change the stage in memory. Nothing is saved yet.
- Wrap the update in
Test.startTest()andTest.stopTest(). Code between these calls gets a fresh set of governor limits. The setup work above does not count against the code under test. - Assert on the outcome. The query runs after
Test.stopTest(), and it checks that exactly 200 tasks exist.
Common errors
- Testing with one record only. The test passes while a query in a loop stays hidden. Always include a 200-record case.
- Asserting too little. A test that only checks "no exception was thrown" can pass even when the logic is wrong. Check the actual result, such as the task count.
- Using 200 separate DML statements. A loop that calls
insertfor each record does not produce a batch of 200. It produces 200 invocations of one record each. Insert the whole list at once. - Relying on org data. Tests do not see existing org data by default. Create every record the test needs.
Checking limits as part of the test
A bulk test works because the trigger runs inside the test transaction. If the trigger had a query in a loop, the update would throw a limit exception at the 101st query. You do not need to write extra code to detect that. The failure is the signal.
Exercise
Your trigger contains a SOQL query inside a for loop over Trigger.new. A test that inserts a single record passes. What change to the test would expose the problem, and why does it work?
Show answer
Insert or update 200 records in one DML statement. The loop then runs 200 times and issues 200 queries. That passes the limit of 100 SOQL queries per synchronous transaction, so Apex throws a System.LimitException and the test fails.
Exercise
Why do you put only the update opps; statement between Test.startTest() and Test.stopTest(), and not the setup code?
Show answer
Code between these calls gets its own fresh set of governor limits. Putting only the statement under test there means the setup inserts do not use up the limits that the trigger is being measured against.