+ Start a Discussion
viswanadham Aviswanadham A 

How to cover the excute method in test class ? below is my test class .

Hi ,
Here i have paste my batch apex and test class but excute method is not coverd.  how to i can cover the in test class. pls help me.
global class R3_deleteQuoteWrapper implements Database.Batchable<sObject>{

   global Database.QueryLocator start(Database.BatchableContext BC){
      
      
    String Query = 'Select id from Quote_Wrapper__c where CreatedDate < LAST_N_MONTHS:3';      
      
      return Database.getQueryLocator(Query);
   }

   global void execute(Database.BatchableContext BC, List<Quote_Wrapper__c> scope){
   
        delete scope;
    }

   global void finish(Database.BatchableContext BC){
   }
  }
@istest
public class R3_deleteQuoteWrapper_test
{
    static list<Quote_Wrapper__c> Quote = new list<Quote_Wrapper__c>();
    
 static testmethod void testdata()
 {
     Quote_Wrapper__c QC=new Quote_Wrapper__c();
     //QC.Name='testing';
     QC.CurrencyType__c='USD';
     insert QC;
     
      Test.startTest(); 
      R3_deleteQuoteWrapper R3data=new R3_deleteQuoteWrapper();
              Database.BatchableContext bc;
              Database.executeBatch(R3data);
      Test.stopTest();
 }
}
User-added image

Regards,
Viswa
 
Lalit Mistry 21Lalit Mistry 21
Follow below steps to get your class tested.
Step 1:
Create a csv file with header values having field api name and include created datetime field in csv file.
CSV file could be as below (you can include all the required fields if needed). Important point to note here is the date format for datetime field. Datetime field should be in the format of YYYY-MM-DDThh:mm:SS
ID,CreatedByID,CreatedDate
2001N000000rJFHdIAO,005N0000002mZVsIAM,2015-11-20T05:15:58.000Z
3001N000000t2DBNIA2,005N0000002mZVsIAM,2015-11-20T03:55:40.000Z
You will also need to include rest other required fields in csv file. Just ensure the header of csv file holds field api name.

Step 2: 
Create a public static resource and load the csv file created in step 1.

Step 3:
Create a test class as below (assuming static resource name is QuoteWrapperLoad)
@isTest
private class R3_deleteQuoteWrapperTest {
    public static testmethod void testBatch() {
        // Load the test records from the static resource
        List<sObject> ls = Test.loadData(Quote_Wrapper__c.sObjectType, 'QuoteWrapperLoad');
         
        System.Test.startTest();
        R3_deleteQuoteWrapper  batch = new R3_deleteQuoteWrapper();
        Database.executeBatch(batch);
        System.Test.stopTest();
        //asserts goes here
    }
}
This should help you test your batch class.
Mark this as best answer to help others if it solves your problem
Amit Chaudhary 8Amit Chaudhary 8
Please update your Batch job like below
global class R3_deleteQuoteWrapper implements Database.Batchable<sObject>
{

   global Database.QueryLocator start(Database.BatchableContext BC)
   {
		String Query = 'Select id from Quote_Wrapper__c where CreatedDate < LAST_N_MONTHS:3';      
		if( Test.isRunningTest() )
		{
			Query = 'Select id from Quote_Wrapper__c ';      
		}
	  
		return Database.getQueryLocator(Query);
   }

   global void execute(Database.BatchableContext BC, List<Quote_Wrapper__c> scope){
   
        delete scope;
    }

   global void finish(Database.BatchableContext BC){
   }
   
}
Then try below test class
@istest
public class R3_deleteQuoteWrapper_test
{
    static list<Quote_Wrapper__c> Quote = new list<Quote_Wrapper__c>();
    
 static testmethod void testdata()
 {
     Quote_Wrapper__c QC=new Quote_Wrapper__c();
     //QC.Name='testing';
     QC.CurrencyType__c='USD';
     insert QC;
     
      Test.startTest(); 
      R3_deleteQuoteWrapper R3data=new R3_deleteQuoteWrapper();
              Database.BatchableContext bc;
              Database.executeBatch(R3data);
      Test.stopTest();
 }
}

Let us know if this will help you