+ Start a Discussion
alxalx 

Test Classes for Batch Apex

hey all,

 

Does anyone know if i can deploy classes to live SF without meeting the test class requirement?

 

If not, i need some help getting to 75% coverage on the following batch class:

 

 

global class countRefContacts implements Database.Batchable<sObject>{
	global final String gstrQuery = 'select ID, Number_of_Reference_Contacts__c from Account';
    global Database.QueryLocator start(Database.BatchableContext BC){
		return Database.getQueryLocator(gstrQuery);
    } 
    global void execute(Database.BatchableContext BC, List<sObject> scope){
 		List<Account> listAccount = new List<Account>();
      		for(SObject objSObject : scope){
				Account objAccount = (Account)objSObject;
				Integer intNumberOfRefs = [SELECT count()FROM Contact WHERE AccountID = :objAccount.ID and Email != null and Binary_Reference_Contact__c = 1];
			  	if (intNumberOfRefs <> objAccount.Number_of_Reference_Contacts__c){
			  		objAccount.Number_of_Reference_Contacts__c = intNumberOfRefs;
			  		listAccount.add(objAccount);
			  	}
		    }	         
       if (listAccount.size() > 0 ) {update listAccount;}
    }
    	global void finish(Database.BatchableContext BC){
    }

}

 

 

What i have so far:

 

public class testCountRefContacts{
	static testMethod void testCountRefContacts(){
		
	List<Account> accns = new List<Account>();
	List<Contact> cons = new List<Contact>();
	for (Integer i = 0; i<199; i++){
		Account a = new Account(name = 'test' + i, Id = 'id' + i);
		Contact c = new Contact(LastName = 'testcon', AccountId = 'id' + i);
		accns.add(a);
		cons.add(c);
	}
	
	insert accns;
	insert cons;
	
	Test.StartTest();
	countRefContacts count = new countRefContacts();

	ID batchprocessid = Database.executeBatch(count);
	
	Test.StopTest();
	
	for(Account a : accns){
		System.assertEquals(0, a.Number_of_Reference_Contacts__c);
	}
	}
}

 

help!

 

 

 

hisrinuhisrinu

You need to write something like below.

 

 

 

public class testCountRefContacts{
	static testMethod void testCountRefContacts(){
		
	List<Account> accns = new List<Account>();
	List<Contact> cons = new List<Contact>();
	for (Integer i = 0; i<199; i++){
		Account a = new Account(name = 'test' + i, Id = 'id' + i);
		Contact c = new Contact(LastName = 'testcon', AccountId = 'id' + i);
		accns.add(a);
		cons.add(c);
	}
	
	insert accns;
	insert cons;
	
	Test.StartTest();
         countRefContacts c = new countRefContacts();
	ID batchprocessid = Database.executeBatch(c.gstrQuery);
	
	Test.StopTest();
	
	for(Account a : accns){
		System.assertEquals(0, a.Number_of_Reference_Contacts__c);
	}
	}
}

 The better option would be remove final keyword in the main class and pass only the test record ids in the test class.. so that it works better

 

alxalx

Srini,

 

I'm not sure what you mean..

 

adding the .gstrQuery yields me a Method does not exist error.

 

As for the last part of your advice.. I have no idea what final you are talking about...