You need to sign in to do that
Don't have an account?
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
database.execute( batchV); just below
batchV.LogoTransfertosemiadmin(); line in test method
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
Try wrapping it in Test.startTest() and Test.endTest(). Like so:
Can you debug your start method?
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
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...
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
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:
Test Class:
The above fails on the Finish method...
This one fails on Execute:
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?
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
Are your asserts statements being passed which you have for your batch class testing?
As of now, there are no system.assert statements in my test method.
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.
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.
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.
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...