Skip to content

Instantly share code, notes, and snippets.

@surajp
Last active March 20, 2025 17:51
Show Gist options
  • Save surajp/4be11917989af434b7691d8ce1373227 to your computer and use it in GitHub Desktop.
Save surajp/4be11917989af434b7691d8ce1373227 to your computer and use it in GitHub Desktop.
Demonstrates increased limits in Apex tests
@IsTest
public class LimitsIllustratorTest {
private static final Integer RECORDS_TO_INSERT_PER_ITERATION = 9000;
private static final INTEGER SOQL_QUERIES_PER_ITERATION = 40;
private static final Integer FUTURE_METHOD_ITERATIONS = 3; //you can go upto 50 here theoretically, but Salesforce will kill the test after a while
@TestSetup
private static void setup() {
Test.startTest();
for (Integer i = 0; i < FUTURE_METHOD_ITERATIONS; i++) {
LimitsIllustratorTest.insertAccounts(i);
LimitsIllustratorTest.queryAccounts();
}
Test.stopTest(); //enclosing in test.starttest and test.stoptest ensures your future method executions are completed before your test methods run
}
@IsTest
private static void shouldHaveQueries() {
Test.startTest();
Assert.isTrue(Limits.getCpuTime() < 10, 'CPU time should have reset after @testsetup'); //CPU time is non-deterministic but < 10ms is a reasonable expectation here
Assert.areEqual(
RECORDS_TO_INSERT_PER_ITERATION * FUTURE_METHOD_ITERATIONS,
[SELECT COUNT() FROM Account],
'SOQL query limits should have reset after @testsetup'
);
Test.stopTest();
}
@future
private static void insertAccounts(Integer count) {
List<Account> acctsToInsert = new List<Account>();
for (Integer i = 0; i < RECORDS_TO_INSERT_PER_ITERATION; i++) {
acctsToInsert.add(new Account(Name = 'Test Account ' + (count * 10 + i)));
}
insert acctsToInsert;
}
@future
private static void queryAccounts() {
for (Integer i = 0; i < 40; i++) {
List<Account> accts = [SELECT Id FROM Account LIMIT 100];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment