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
PXForcePXForce 

Test Coverage for batch job

I have a simple batch job (the code is pasted below.) and a test method for the job. My class is not getting the coverage that s required (I am only getting 13%). Could anyone please tell me whats wrong with the code or if i am missing something. Its preventing me from moving stuff into production through ecllipse as its bringin down the average test coverage to 72% (75% required) on all classes and triggers.

global class UpdateAccounts implements Database.Batchable<sobject>  
{
	global Database.QueryLocator start(Database.BatchableContext BC)
	{
		return Database.getQueryLocator([Select Id,Name from Account]);
	}
	
	global void execute(Database.BatchableContext BC , List<sobject> scope)
	{
		List<Account> accsToUpdate = new List<Account>();
			
		For(sobject s : scope)
		{
			Account acc  = (Account)s;
			
			accsToUpdate.add(acc);
			
		}
		
		update accsToUpdate;
	}
	
	global void finish(Database.BatchableContext BC)
	{
		Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

		mail.setToAddresses(new String[] {'abakshi@prophix.com'});
		mail.setReplyTo('abakshi@prophix.com');
		mail.setSenderDisplayName('UpdateAccounts Event Batch Processing');
		mail.setSubject('UpdateAccounts Batch Process Completed');
		mail.setPlainTextBody('UpdateAccounts Batch Process has completed');

		Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
	}

	private static testMethod void testUpdateAccounts()
	{
		User user = [Select Id , Name from User where Id = '00530000003oM6i'];
		
		List <Account> accns = new List<Account>();
      	for(integer i = 0; i<200; i++)
      	{
         Account a = new Account(name='testAccount'+'i', 
                     Ownerid = user.ID); 
         accns.add(a);
      	}
   
   		insert accns;
		
		Test.startTest();
		UpdateAccounts batchProcess = new UpdateAccounts();
		ID batchProcessId = Database.executeBatch(batchProcess,90);
		Test.stopTest();
		
		System.assertEquals(0,0);
	}
}

 

 

BritishBoyinDCBritishBoyinDC

Are you getting an error when you try and deploy it?? Typically, I set the actual query to be executed as part of the batch object creation process (i.e. it's a variable you can set on the object). Part of the reason is that you can then more effectively set what is tested, and limit it more easily to act on your test records.

 

I suspect the deployment is failing because the test is failing because you are not limiting the scope of the query - a batch test can only be run once - i.e. 200 records. I know you are creating 200 records, but when you run the batch, it isn't being filtered as far as I can tell, so it is executing a batch against the whole query locator in Prod...and then failing, so won't let you deploy it...?

 

Take a look at this post - it is based on the docs, and the code I amended uses the concept I am talking about, and is pretty similar to what you are trying to do. I'd re-work the code to be set up like this example, and I think you'll be in good shape: