function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Test case governor limits

Hi All.


I am have trouble with governor limits when I run test cases for my application.


System.Exception: Too many SOQL queries: 21Trigger.dsContactTrigger: line 388, column 32


My application has 5 batch processes which I need to test and triggers on update insert and delete for account contact and lead.




Best Answer chosen by Admin (Salesforce Developers) 

If you want to test Batch process, In test you can run/test only one batch and for eah batch there should be 200 records if it is more than it then it fails, hence you need to set a limit in your query for Test, means make a query variable in batch apex class.

see the line mark as red.

like sample:


suppose this is the batch apex:

global class OwnerReassignment implements Database.Batchable<sObject>{
String query;
String email;
Id toUserId;
Id fromUserId;
global database.querylocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);}
global void execute(Database.BatchableContext BC, List<sObject> scope){
List<Account> accns = new List<Account>();
for(sObject s : scope){Account a = (Account)s;
update accns;
global void finish(Database.BatchableContext BC){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {email});

mail.setSenderDisplayName('Batch Processing');
mail.setSubject('Batch Process Completed');
mail.setPlainTextBody('Batch Process has completed');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });



Test Class:



public static testMethod void testBatch() {
user u = [SELECT ID, username FROM User
WHERE username=''];
user u2 = [SELECT ID, username FROM User
WHERE username=''];
String u2id =;


// Create 200 test accounts - this simulates one execute.
// Important - the test framework only allows you to
// test one execute.
List <Account> accns = new List<Account>();
for(integer i = 0; i<200; i++){
Account a = new Account(name='testAccount'+'i',
Ownerid = u.ID);
insert accns;
OwnerReassignment reassign = new OwnerReassignment();
reassign.query='SELECT ID, Name, Ownerid
FROM Account
WHERE ownerid=\'' + + '\'
reassign.fromUserId = u.Id;
reassign.toUser.Id = u2.Id;
ID batchprocessid = Database.executeBatch(reassign);
System.AssertEquals(database.countquery('SELECT count()'
+' FROM Account WHERE ownerid=:u2ID'), 200);