+ Start a Discussion
SzymonSzymon 

Hel with Test Class

Hi, I'm trying to write a test class for the below:
global class EmptyDeletedRecords implements Database.Batchable<sObject>, Schedulable{   
    global EmptyDeletedRecords(){}

    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator([Select id from Deleted_record__c where CreatedDate <LAST_N_DAYS:5]);
    } 

    //Execute method for the Schedulable interface
    global void execute(SchedulableContext sc){   
        //execute the batch
        EmptyDeletedRecords deleteCS = new EmptyDeletedRecords();
        ID batchprocessid = Database.executeBatch(deleteCS);
    }

    //Execute method for the batchable interface
    global void execute(Database.BatchableContext BC, list<sObject> scope){     
        delete scope;   
        DataBase.emptyRecycleBin(scope); 
    }

    global void finish(Database.BatchableContext BC){}
}
But am not sure how to test the scope deletion:
@isTest
private class EmptyDeletedRecords_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    Deleted_record__c deleted_record_Obj = new Deleted_record__c(Deleted_ID__c = '5001p00002O54P2AAJ', External_ID__c = 'null', Object_Type_From_Apex__c = 'Case_with_custom_object');
    Insert deleted_record_Obj; 
    test.stopTest();
  }
    
    static testMethod void testBatch(){
    SchedulableContext sc = null;
    EmptyDeletedRecords tsc = new EmptyDeletedRecords();
    tsc.execute(sc);
  }
}
Any advice how to test that? Thanks!

 
Raj VakatiRaj Vakati
Use this code
 
@isTest
private class EmptyDeletedRecords_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    List<Deleted_record__c> recore = new list<Deleted_record__c>();
	for(Integer i = 0 ;i<10;i++){
	Deleted_record__c deleted_record_Obj = new Deleted_record__c(
	Deleted_ID__c = '5001p00002O54P2AAJ', External_ID__c = 'null', 
	Object_Type_From_Apex__c = 'Case_with_custom_object');
	recore.add(deleted_record_Obj);
	}
	Insert deleted_record_Obj; 
    
	EmptyDeletedRecords  obj = new EmptyDeletedRecords ();
          DataBase.executeBatch(obj);	
	test.stopTest();
  }
    
}

 
SzymonSzymon
Thanks Raj but that doesn't work unofortunately: Compile Error: Variable does not exist: deleted_record_Obj at line 13 column 12
Raj VakatiRaj Vakati
Sorry.. use this code
@isTest
private class EmptyDeletedRecords_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    List<Deleted_record__c> recore = new list<Deleted_record__c>();
	for(Integer i = 0 ;i<10;i++){
	Deleted_record__c deleted_record_Obj = new Deleted_record__c(
	Deleted_ID__c = '5001p00002O54P2AAJ', External_ID__c = 'null', 
	Object_Type_From_Apex__c = 'Case_with_custom_object');
	recore.add(deleted_record_Obj);
	}
	Insert recore; 
    
	EmptyDeletedRecords  obj = new EmptyDeletedRecords ();
          DataBase.executeBatch(obj , 200);	
	test.stopTest();
  }
    
}

 
SzymonSzymon
Tried that too before but this won't pass any tests and the code coverage reverts to 0%.
Raj VakatiRaj Vakati
what is the error message ??
Raj VakatiRaj Vakati
@isTest
private class EmptyDeletedRecords_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    List<Deleted_record__c> listRec = new list<Deleted_record__c>();
    for(Integer i = 0 ;i<10;i++){
    Deleted_record__c deleted_record_Obj = new Deleted_record__c(
    Deleted_ID__c = '5001p00002O54P2AAJ', External_ID__c = '21376761237'+i, 
    Object_Type_From_Apex__c = 'Case_with_custom_object');
    listRec.add(deleted_record_Obj);
    }
    Insert listRec; 
    
    EmptyDeletedRecords  obj = new EmptyDeletedRecords ();
    DataBase.executeBatch(obj , 10);    
    test.stopTest();
  }
    
}
Raj VakatiRaj Vakati
Updated version 
 
@isTest
private class EmptyDeletedRecords_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    List<Deleted_record__c> listRec = new list<Deleted_record__c>();
	for(Integer i = 0 ;i<10;i++){
	Deleted_record__c deleted_record_Obj = new Deleted_record__c(
	Deleted_ID__c = '5001p00002O54P2AAJ', External_ID__c = '21376761237'+i, 
	Object_Type_From_Apex__c = 'Case_with_custom_object');
	listRec.add(deleted_record_Obj);
	}
	Insert listRec; 
    
	EmptyDeletedRecords  obj = new EmptyDeletedRecords ();
    DataBase.executeBatch(obj , 10);	
	
	 String sch = '0 0 23 * * ?';
    system.schedule('Test check', sch, obj);
		
	
	test.stopTest();
  }
    
}

 
v varaprasadv varaprasad
Hi ,

Please check once below snippet code: 

I think records are not retrieving please check once records are displaying in debug logs or not.
 
@isTest
private class EmptyDeletedRecords_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    List<Deleted_record__c> recore = new list<Deleted_record__c>();
	for(Integer i = 0 ;i<10;i++){
	Deleted_record__c deleted_record_Obj = new Deleted_record__c(
	Deleted_ID__c = '5001p00002O54P2AAJ', External_ID__c = 'null', 
	Object_Type_From_Apex__c = 'Case_with_custom_object');
	recore.add(deleted_record_Obj);
	}
	Insert recore; 
    
	EmptyDeletedRecords  obj = new EmptyDeletedRecords ();
          DataBase.executeBatch(obj , 200);	
	test.stopTest();
  }
  
  public static testmethod void testBatch(){
     List<Deleted_record__c> recore = [Select id from Deleted_record__c where CreatedDate <LAST_N_DAYS:5];
	 system.debug('==recore=='+recore);
	 test.starttest();
	    SchedulableContext sc = null;
       EmptyDeletedRecords tsc = new EmptyDeletedRecords();
       tsc.execute(sc);
	 test.stoptest();
  
  
  }
    
}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 
SzymonSzymon
Thanks Varaprasad but this class will still only cover 70% of my code. Same as the original class I posted.

Raj, there's no error only 0% code coverage on the class.
Thanks