• Nazi
  • NEWBIE
  • 30 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 6
    Replies
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();
  
    }
    }
    
  • December 29, 2016
  • Like
  • 0
 Hi
I called the batch class in my apex class method.when I am writting test class for that batch class its not covering the execute method.Is there any solution ?

Thanks.
  • December 09, 2016
  • Like
  • 0
Having 2L of records how to retrive in pagination.By using offset cannot use more than 2000 records.Is there is any way to resolve it?  
  • November 21, 2016
  • Like
  • 0
How to call batch class inside apex class method.Is there any way to call ? if so please give some example.
  • October 17, 2016
  • Like
  • 0
Is it possible to write a single test class to cover multiple classes?
  • October 12, 2016
  • Like
  • 0
Is there any test class for aura component in salesforce lightning? If not can i able to push my code to production without writing test classes?
  • October 03, 2016
  • Like
  • 0
 Hi
I called the batch class in my apex class method.when I am writting test class for that batch class its not covering the execute method.Is there any solution ?

Thanks.
  • December 09, 2016
  • Like
  • 0
How to call batch class inside apex class method.Is there any way to call ? if so please give some example.
  • October 17, 2016
  • Like
  • 0
Is it possible to write a single test class to cover multiple classes?
  • October 12, 2016
  • Like
  • 0
Is there any test class for aura component in salesforce lightning? If not can i able to push my code to production without writing test classes?
  • October 03, 2016
  • Like
  • 0