You need to sign in to do that
Don't have an account?

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: 21 | Trigger.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.
Thanks
Hector.
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;
if(a.Ownerid==fromUserId){
a.Ownerid=toUserId;
accns.add(a);
}
}
update accns;
}
global void finish(Database.BatchableContext BC){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {email});
mail.setReplyTo('batch@acme.com');
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='testuser1@acme.com'];
user u2 = [SELECT ID, username FROM User
WHERE username='testuser2@acme.com'];
String u2id = u2.id;
// Create 200 test accounts - this simulates one execute.
// Important - the Salesforce.com 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);
accns.add(a);
}
insert accns;
Test.StartTest();
OwnerReassignment reassign = new OwnerReassignment();
reassign.query='SELECT ID, Name, Ownerid
FROM Account
WHERE ownerid=\'' + u.id + '\'
LIMIT=200';
reassign.email='admin@acme.com';
reassign.fromUserId = u.Id;
reassign.toUser.Id = u2.Id;
ID batchprocessid = Database.executeBatch(reassign);
Test.StopTest();
System.AssertEquals(database.countquery('SELECT count()'
+' FROM Account WHERE ownerid=:u2ID'), 200);
}
}