You need to sign in to do that
Don't have an account?
![Nazi Nazi](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Test class is not covering execute method in batch class
Hi my test class is not covering execute method in batch class.Can anyone please help?
Bacth class:
global class BatchAddAccount implements Database.Batchable<Sobject>
{
global string scope;
string strAccountNames;
string strLeafnodeId;
global BatchAddAccsount(string AccountNames,string leafNodeId)
{
strAccountNames=AccountNames;
strLeafnodeId =leafNodeId;
}
global Database.QueryLocator start(Database.BatchableContext bc)
{
String[] strAccoutname = strAccountNames.split(',');
set<string> Straccnames =new set<string>();
for (String item : strAccoutname)
{
if(item!='')
{
Straccnames.add(item);
}
system.debug('Straccnames.add'+Straccnames);
}
scope='select id, name from Territory_Account__c where id IN:Straccnames ORDER BY Name';
return Database.getQueryLocator(scope);
}
global void execute(Database.BatchableContext bc,list<Territory_Account__c> scope)
{
List<Accounts_Territory__c> AccountToInsert = new List<Accounts_Territory__c> ();
for (Territory_Account__c terracc : scope)
{
List<Accounts_Territory__c> Accterritory =[select id,name,Territory_Account__c from Accounts_Territory__c where Territory__c=:strLeafnodeId and Territory_Account__c=:terracc.id];
if(Accterritory.size()==0)
{
Accounts_Territory__c terracct = new Accounts_Territory__c ();
terracct.Name = terracc.name;
terracct.Territory__c=strLeafnodeId;
terracct.Territory_Account__c=terracc.id;
AccountToInsert.add(terracct);
}
}
insert AccountToInsert;
}
global void finish(Database.BatchableContext bc)
{
}
}
Test Class:
@isTest
private class batchtestaddacc
{
public static testmethod void mymethod()
{
Territory__c terr = new Territory__c (name='Indiaa');
insert terr;
Territory_Account__c acc = new Territory_Account__c(name='LG-Ericsson1');
acc.Name='LG-Ericsson1';
insert acc;
list<Accounts_Territory__c> accterritoty =new list<Accounts_Territory__c>();
Accounts_Territory__c accterr = new Accounts_Territory__c();
accterr.name=acc.Name;
accterr.Territory_Account__c=acc.id;
accterr.Territory__c=terr.id;
accterritoty.add(accterr);
insert accterritoty;
Test.StartTest();
BatchAddAccsount Addacc= new BatchAddAccsount(accterr.id,terr.id);
ID batchprocessid = Database.executeBatch(Addacc);
Test.StopTest();
}
}
Bacth class:
global class BatchAddAccount implements Database.Batchable<Sobject>
{
global string scope;
string strAccountNames;
string strLeafnodeId;
global BatchAddAccsount(string AccountNames,string leafNodeId)
{
strAccountNames=AccountNames;
strLeafnodeId =leafNodeId;
}
global Database.QueryLocator start(Database.BatchableContext bc)
{
String[] strAccoutname = strAccountNames.split(',');
set<string> Straccnames =new set<string>();
for (String item : strAccoutname)
{
if(item!='')
{
Straccnames.add(item);
}
system.debug('Straccnames.add'+Straccnames);
}
scope='select id, name from Territory_Account__c where id IN:Straccnames ORDER BY Name';
return Database.getQueryLocator(scope);
}
global void execute(Database.BatchableContext bc,list<Territory_Account__c> scope)
{
List<Accounts_Territory__c> AccountToInsert = new List<Accounts_Territory__c> ();
for (Territory_Account__c terracc : scope)
{
List<Accounts_Territory__c> Accterritory =[select id,name,Territory_Account__c from Accounts_Territory__c where Territory__c=:strLeafnodeId and Territory_Account__c=:terracc.id];
if(Accterritory.size()==0)
{
Accounts_Territory__c terracct = new Accounts_Territory__c ();
terracct.Name = terracc.name;
terracct.Territory__c=strLeafnodeId;
terracct.Territory_Account__c=terracc.id;
AccountToInsert.add(terracct);
}
}
insert AccountToInsert;
}
global void finish(Database.BatchableContext bc)
{
}
}
Test Class:
@isTest
private class batchtestaddacc
{
public static testmethod void mymethod()
{
Territory__c terr = new Territory__c (name='Indiaa');
insert terr;
Territory_Account__c acc = new Territory_Account__c(name='LG-Ericsson1');
acc.Name='LG-Ericsson1';
insert acc;
list<Accounts_Territory__c> accterritoty =new list<Accounts_Territory__c>();
Accounts_Territory__c accterr = new Accounts_Territory__c();
accterr.name=acc.Name;
accterr.Territory_Account__c=acc.id;
accterr.Territory__c=terr.id;
accterritoty.add(accterr);
insert accterritoty;
Test.StartTest();
BatchAddAccsount Addacc= new BatchAddAccsount(accterr.id,terr.id);
ID batchprocessid = Database.executeBatch(Addacc);
Test.StopTest();
}
}
Let us know if this will help you