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
SFDC_TTLSFDC_TTL 

Batch execute methos id not covered in test class

Hi,

 

I have a scenario of calling a class which inturn invokes the batch class. In normal scenario, the batch is getting invoked from the class, but coming to test method, batch is invoked execute method is not covered at all. Even the below line is in RED only.

 

global void execute(Database.BatchableContext ctx, List<Account> acclist)      {

1. Test class

private class Test_OwnerShipChanges

 Static testMethodvoid nonverifiedmailintimation()

{

 VerOwnrChangetoSemiAdmin batchV=new VerOwnrChangetoSemiAdmin();

 batchV.LogoTransfertosemiadmin();

}

}

 

2. Actual class

 

public class VerOwnrChangetoSemiAdmin

{

public void LogoTransfertosemiadmin()

{

batch_VerOwnrChangetoSemiAdmin verchange =new batch_VerOwnrChangetoSemiAdmin(query,Logolist,semiadmin.id,semiadmin.Email);

ID myBatchJobID = Database.executeBatch(verchange,1);

}

}

 

3.Batch class

 

globalclass batch_VerOwnrChangetoSemiAdmin implementsDatabase.Batchable<sObject> {

 

globalfinal String query;

globalfinal Id semiadminid;

globalfinal String semiadmemail;

globalList<Account> paracclist;

 

global batch_VerOwnrChangetoSemiAdmin(String qq,List<Account> logolist,Id semiadm,String semiadminemail)

{

query=qq;

semiadminid=semiadm;

semiadmemail=semiadminemail;

paracclist=logolist;

}

globalDatabase.QueryLocator start(Database.BatchableContext ctx)

{

 

returnDatabase.getQueryLocator(query);

}

globalvoid execute(Database.BatchableContext ctx, List<Account> acclist)

{

// some logic

}

 

}

globalvoidfinish(Database.BatchableContext ctx)

{

}

 

}

The execute method from batch class is not covered in the test coverage. Start method is getting invoked.

 

Please suggest

 

Thanks

 

 

Bhawani SharmaBhawani Sharma
Use
database.execute( batchV); just below
batchV.LogoTransfertosemiadmin(); line in test method
SFDC_TTLSFDC_TTL

Hi,

 

It is giving an error that

 

Save error: Method does not exist or incorrect signature: database.execute(VerOwnrChangetoSemiAdmin)

 

because i don't need to call my batch class explicitly. It is being called from the class which is called in test method.

 

privateclassTest_SMEOwnerShipChanges

{

 

StatictestMethodvoidnonverifiedmailintimation()

{

VerOwnrChangetoSemiAdmin batchV=new VerOwnrChangetoSemiAdmin();

batchV.LogoTransfertosemiadmin();

}

}

 

Thanks

Sean TanSean Tan

Try wrapping it in Test.startTest() and Test.endTest(). Like so:

 

private class Test_SMEOwnerShipChanges
{
	static testMethod void nonverifiedmailintimation()
	{
		Test.startTest();
		VerOwnrChangetoSemiAdmin batchV=new VerOwnrChangetoSemiAdmin();
		batchV.LogoTransfertosemiadmin();
		Test.stopTest();
	}
}

 

Bhawani SharmaBhawani Sharma

Can you debug your start method?

SFDC_TTLSFDC_TTL

global Database.QueryLocator start(Database.BatchableContext ctx)
    {
      
      system.debug('***********'+query);
      return Database.getQueryLocator(query);
    } 
    
    
  global void execute(Database.BatchableContext ctx, List<Account> acclist)
  {

   //some logic

  }

 

When i am running the test method, start method is covered and the debug stmt in start is printed. only the execute method is not covered.

 

test.statrttest() and test.stoptest() are also present in my test class.

 

 

Till then my debug log size has reached the maximum level (2 MB)

 

 

Thanks

Sean TanSean Tan

These may seem like silly suggestions here, but it seems all the code hasn't been posted (Since you did mention you're setting Test.startTest and Test.endTest in your test method already but in the code shown does not have it).

 

Are you inserting all the test data required for the start method to actually return anything? If not are you trying to rely on data that exists in the org? (Not good practice).

 

If you're trying to rely on data that exists in the org than you'll need to set SeeAllData as true...

SFDC_TTLSFDC_TTL

Hi,

 

I donot rely on my organizational data. Created the fresh test data suitable for test class. Hence the batch start method is called. The execute method is not getting invoked at all.

 

Thanks

Sean TanSean Tan

Start would be called regardless if data exists or not as long as the Test.startTest / Test.endTest is called. If nothing matches the return of the SOQL call than the execute won't run.

 

For example a quick test:

 

Bach Class:

global class BatchTest implements Database.Batchable<sObject>{
	
	global BatchTest()
	{
	}
	
	global Database.QueryLocator start(Database.BatchableContext BC)
	{
		return Database.getQueryLocator('SELECT Id FROM Account WHERE Name = \'DUMMYMASDLASHDJASHDASDSA\'');
	}
	
	global void execute(Database.BatchableContext BC, List<sObject> scope)
	{
		System.assert(false, 'EXECUTE HIT');
	}
	
	global void finish(Database.BatchableContext BC)
	{
		System.assert(false, 'FINISH HIT');
	}
}

 Test Class:

@isTest(SeeAllData=false)
private with sharing class TestBatchTest 
{
	private static testMethod void test_Batch()
	{
		Test.startTest();
		Database.executeBatch(new BatchTest());
		Test.stopTest();
	}
}

The above fails on the Finish method...

 

This one fails on Execute:

@isTest(SeeAllData=false)
private with sharing class TestBatchTest 
{
	private static testMethod void test_Batch()
	{
		insert new Account(Name='DUMMYMASDLASHDJASHDASDSA');		
		Test.startTest();
		Database.executeBatch(new BatchTest());
		Test.stopTest();
	}
}

 Again based off the limited information I see, I'm really guessing it's something to do with the query not returning any records. Can you check if your final method is being hit?

SFDC_TTLSFDC_TTL

   global void finish(Database.BatchableContext ctx)
     {
      system.debug('****BATCHFINISHED****');
     }

}

 

The debug statement is neither in RED nor in BLUE. Only the finish method first line is in blue.

 

To check the same in debug log, the log size has reached the maximum level. Because of this maximum log size, is there any limitation that the further code will not be executed?

 

Please suggest.

 

Thanks

 

Bhawani SharmaBhawani Sharma

Are your asserts statements being passed which you have for your batch class testing?

Bhawani SharmaBhawani Sharma
Also, please reset your debug log to error only. So if there will be any error, it will display. Unnecessary log will not be displayed.
SFDC_TTLSFDC_TTL

As of now, there are no system.assert statements in my test method.

Bhawani SharmaBhawani Sharma

Can you put assert statement for batch testing? I think, your batch is working find, but somehow SFDC is not showing line covered. It happens sometime. I have seen that.

Sean TanSean Tan

Maximum debug shouldn't stop your execute method from running.

 

How complex is your query? Try simplifying it to something very straightforward and change your test data to insert very simple data that matches the simply query.

SFDC_TTLSFDC_TTL

1. My query is very simple and is passed to batch class via a class.

 

public classSME_VerOwnrChangetoSemiAdmin

{

publicvoidLogoTransfertosemiadmin()

{

String query='select Name,ownerId,OwnerIDMId__c,OwnerEmail__c from Account where parentid IN '+ss+' and CombiAccType__c=\''+CombiAccType+'\''+' and Inactivate__c=\''+Inactivate+'\''+' and AccLocked__c!=\''+AccLocked+'\''+' and NumOppLockd__c=0'+' and Rectyp__c=\''+RecType+'\''+ ' and owner.id!=\''+semiadmin.id+'\'';

 

batch_VerOwnrChangetoSemiAdmin verchange = new batch_VerOwnrChangetoSemiAdmin(query,Logolist,semiadmin.id,semiadmin.Email); 
ID myBatchJobID = Database.executeBatch(verchange,1);

 

}

}


2. My batch class is

 

global class batch_VerOwnrChangetoSemiAdmin implements Database.Batchable<sObject> {
   
global final String query;
global final Id semiadminid;
global final String semiadmemail;
global List<Account> paracclist;
 
  global batch_VerOwnrChangetoSemiAdmin(String qq,List<Account> logolist,Id semiadm,String semiadminemail)
  {
    query=qq;
    semiadminid=semiadm;
    semiadmemail=semiadminemail;
    paracclist=logolist;
  }
 
 global Database.QueryLocator start(Database.BatchableContext ctx)
    {
      
      system.debug('*****120BATCHQUERYRESULT******'+query);
      return Database.getQueryLocator(query);
    }
   
   
  global void execute(Database.BatchableContext ctx, List<Account> acclist)
  {
//some logic
  }
global void finish(Database.BatchableContext ctx)
     {
      system.debug('****BATCHFINISHED****');
     }
 }


 All the parameters are passed properly and the query is also printed in a correct format(system.debug in start method). Some how, the execute method is not getting invoked and hence i am not able to find the way forward.

Sean TanSean Tan

Try simplifying the query even more, maybe even something as small as this:

 

select Name, ownerId,OwnerIDMId__c,OwnerEmail__c from Account limit 1

 

See if the execute runs with the test data you're inserting...