Print this page

Apex Test Class Limit 500

Knowledge Article Number 000025380
Description How to avoid Apex Test Class query rows governor limit of 500?
Resolution You could work around this limitation by making a small change on the code
being tested: 

integer maxSize;

if (isTestMode) {
maxSize = 500;
} else {
maxSize = 1000;
List<Account> acts = [SELECT ID FROM Account LIMIT :maxSize];

You also need to do something like this to make it really work:

public class aaPage75 {
private Boolean isTestMode = false;

public aaPage75() {

public PageReference SampleMethod() {
System.debug('In Code');
if (!isTestMode) {
System.debug('This should be shown only in regular execution');
System.debug('In Code');
return null;
private static TestMethod void myTestMethod() {
aaPage75 ts = new aaPage75();
ts.isTestMode = true;

Basically you have a private boolean variable (isTestMode) in your class
method that is set to true, but you set it to true on your test method. Of
course if the test method is not part of the class like in this sample, then
the variable must be public.

promote demote