• SatS
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies

Hi,

         

     I am write a test class for batch apex,my code coverage is 33%, it is covered only start method.

 

How can i write test class for execute method?

 

 

 

Testclass for Batch Class   

 

@isTest

private class testtrainingbatchcls {

static testmethod void m1() {


 List<training__c> lst=new List<training__c>();


for(integer i=0;i<1500;i++) {
training__c t=new training__c();
t.name = 'T'+i;
lst.add(t);
 }

 trainingbatchcls obj=new trainingbatchcls();
Database.executeBatch(obj);

}

 }

 

 

Batch Apex Class

 

 

global class trainingbatchcls implements Database.batchable <sobject>{


global list<training__c> start(Database.BatchableContext bc){


List<training__c> lst=new List<training__c>();
for(integer i=0;i<1500;i++){


training__c t=new training__c();
t.name = 'T'+i;
lst.add(t);

}
return lst;
}



global void execute(Database.BatchableContext bc,list<training__C>lst) {

insert lst;

}


global void finish(Database.BatchableContext bc){

list<string> toaddress = new list<string>();
toaddress.add('shravankumarbagam@yahoo.com');


String subject = 'Batch class run complete';
String body = '';

Messaging.singleEmailMessage mail = new Messaging.singleEmailMessage();

mail.setToAddresses(toaddress);


mail.setSenderDisplayName('Salesforce Outbound Notification');
mail.setSubject(subject);
mail.setPlainTextBody(body);
mail.setUseSignature(false);

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

}

 

 

}

 

Hi everyone,

i have complete the test cass for my Batch apex class. when i was test that code in Developer and Test sandbox its covered 100%. when i deploy that code to QA its show only 27% without any errors.

 

when i check that code DEBUG LOG i got this error....

 

|EXCEPTION_THROWN|[EXTERNAL]|System.UnexpectedException: No more than one executeBatch can be called from within a testmethod. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.
12:18:21.557|FATAL_ERROR|System.UnexpectedException: No more than one executeBatch can be called from within a testmethod. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.

 

here is the Batch apex and corresponding test classes.

 

Apex Class:

 

global class ParentContactDefaultDeliverToUpdate implements Database.Batchable<sObject>,Schedulable{

  global String Query;
   
    global ParentContactDefaultDeliverToUpdate(){
     
      Query = 'SELECT Id, otc_contact_id__c, Default_Deliver_To_OTC_Contact_Id__c, Parent_OTC_Contact_Id__c, Needs_Parent_OTC_Contact_Update__c FROM OTC_Contact__c WHERE Needs_Parent_OTC_Contact_Update__C=TRUE';
    }

    global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
    }
     global void execute(SchedulableContext SC){
        ParentContactDefaultDeliverToUpdate obj = new ParentContactDefaultDeliverToUpdate();
        database.executebatch(obj);
        
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope){
    
       Set<String> parentOtcExtIds = new Set<String>(); 
      // Set<String> ddToOtcExtIds = new Set<String>();       
       
       for(sObject s : scope){
           OTC_Contact__c otcCon = (OTC_Contact__c)s;
           if((otcCon.Parent_OTC_Contact_Id__c != NULL)
               || (otcCon.Default_Deliver_To_OTC_Contact_Id__c != NULL) ){
               parentOtcExtIds.add(otcCon.Parent_OTC_Contact_Id__c);
               parentOtcExtIds.add(otcCon.Default_Deliver_To_OTC_Contact_Id__C);
           }
          /* if(otcCon.Default_Deliver_To_OTC_Contact_Id__c != NULL && otcCon.Default_Deliver_To__c == NULL){
               ddToOtcExtIds.add(otcCon.Default_Deliver_To_OTC_Contact_Id__c);
           }*/
       }
       
       List<OTC_Contact__c> OtcContacts = [Select id,Otc_Contact_Id__c from OTC_Contact__c where Otc_Contact_Id__c IN :parentOtcExtIds];
       Map<String,Id> otcContactMap = new Map<String,Id>();
       for(OTC_Contact__c otc : OtcContacts){
           otcContactMap.put(otc.Otc_Contact_Id__c,otc.Id);
       }
       
       for(sObject s : scope){
           OTC_Contact__c otcCon = (OTC_Contact__c)s;
            if(otcCon.Parent_OTC_Contact_Id__c != NULL && otcContactMap.containsKey(otcCon.Parent_OTC_Contact_Id__c)){
                otcCon.Parent_OTC_Contact__c = otcContactMap.get(otcCon.Parent_OTC_Contact_Id__c);
            }
            if(otcCon.Default_Deliver_To_OTC_Contact_Id__c != NULL && otcContactMap.containsKey(otcCon.Default_Deliver_To_OTC_Contact_Id__c )){
                otcCon.Default_Deliver_To__c = otcContactMap.get(otcCon.Default_Deliver_To_OTC_Contact_Id__c );            
            }
          otcCon.Needs_Parent_OTC_Contact_Update__c = FALSE;
          otcCon.Parent_OTC_Contacts_Updated__c = TRUE;  
       }
       update scope;
       
    }
 
    global void finish(Database.BatchableContext BC){
    }
}

 

TEST CLASS:


@isTest

Private class ParentContactDefaultDeliverToUpdate_Test
{
  static testMethod void Testclass(){  

  
   Account acc = new Account(Name = 'Test Name',phone = '53453455435', Customer_Segment__c = 'Bookstore',Account_Status__c = 'Active',
                                    INTG_Physical_Address_Line_1__c = 'Street', 
                                    INTG_Physical_Address_Line_2__c = 'Apt 3',
                                    INTG_Physical_City__c = 'Tarrytown',
                                    INTG_Physical_State_Province_Region__c = 'New York',
                                    INTG_Physical_Country__c = 'USA',
                                   INTG_Physical_Postal_Code__c = '94538'); 
    insert acc; 
    
     Unit__c ut1 = New Unit__c(Name = ' test', Account__c = acc.id);
    
    insert ut1; 
    
    Contact con = new Contact(FirstName = 'Test Name', accountId = acc.id, LastName = 'Test', Phone = '5345345545', Contact_Status__c = 'Active',
                                    INTG_Physical_Address_Line_1__c = 'Street', 
                                    INTG_Physical_Address_Line_2__c = 'Apt 3',
                                    INTG_Physical_City__c = 'Tarrytown',
                                    INTG_Physical_State_Province_Region__c = 'New York',
                                    INTG_Physical_Country__c = 'USA',
                                   INTG_Physical_Postal_Code__c = '94538');     
    insert con;

    OTC_Contact__c otc1 = new OTC_Contact__c(name= 'test', Last_Name__c = 'test', Contact__c = con.id, OTC_Contact_Type__c='AR', 
                                             Otc_Contact_Id__c = '123ARUNISON');
    insert otc1;
    
    OTC_Contact__c otc2 = new OTC_Contact__c(name= 'test', Last_Name__c = 'test', Contact__c = con.id, otc_contact_type__c='SOLDTO',
                                             Otc_Contact_Id__c = '12345SOLDTOUNISON', parent_otc_contact_id__c='123ARUNISON', parent_otc_contact__c=otc1.id, needs_parent_otc_contact_update__c=TRUE);
    insert otc2;
    
    OTC_Contact__c otc3 = new OTC_Contact__c(name='test3', Last_Name__c='test contact', Contact__c= con.id, otc_Contact_type__c='DELIVERTO',
                                               otc_contact_id__c='321DELIVERTOUNISON');
    insert otc3;                                           
    
    OTC_Contact__c otc4 = new OTC_Contact__c (Name = 'Test3', Last_Name__c = 'test3', contact__c = con.id,
                                              Otc_Contact_Id__c='1234SHIPTOUNISON', otc_contact_type__c='SHIPTO', default_deliver_to_otc_contact_id__c='321DELIVERTOUNISON', 
                                              default_deliver_to__c=otc3.id, needs_parent_otc_contact_update__c=TRUE);
    insert otc4;
    
      
    Test.StartTest();  
    
// Schedule the job
        ParentContactDefaultDeliverToUpdate obj1 = new ParentContactDefaultDeliverToUpdate();
      
   //  obj1.query='SELECT Id, otc_contact_id__c, Default_Deliver_To_OTC_Contact_Id__c, Parent_OTC_Contact_Id__c, Needs_Parent_OTC_Contact_Update__C FROM OTC_Contact__c WHERE Needs_Parent_OTC_Contact_Update__C=\' ' + TRUE + '\'  LIMIT=200';
    ID batchprocessid = Database.executeBatch(obj1);
   // OrderTransactionUpdate obj = new OrderTransactionUpdate();
  //  database.executebatch(obj);
  ParentContactDefaultDeliverToUpdate p = new ParentContactDefaultDeliverToUpdate();        
   String sch = '0 0 8 13 2 ?';        
  system.schedule('Schedule Test', sch, p);
  
 Test.stopTest();
       }

 }

 

can anyone help me out with this issue....ITS URGENT

 

thanks in advance....