function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jacob Elliott 8Jacob Elliott 8 

Creating Test Class For Batch Apex

I have a batch method that searches for all "Matters" with a certain record type. After they are found it loops though this list, assigns a new record type, and creates several "Stages" for the matter. I can't seem to write a Test class that covers more than 4% (cone of shame). Any help would be greatly appreciated!


Batch Class
 
global class MyBatchClass implements Database.Batchable<sObject> {
     global Database.QueryLocator start(Database.BatchableContext bc) {
        // collect the batches of records or objects to be passed to execute
         return Database.getQueryLocator(
            'SELECT ID, RecordTypeId FROM litify_pm__Matter__c Where litify_pm__Matter_Plan__c = \'a0h6A000002nWNrQAM\' AND (RecordTypeId = \'0126A000000NjDyQAK\' OR RecordTypeId = \'0126A000000xP0cQAE\')'
        );
    }
    global void execute(Database.BatchableContext bc, List<litify_pm__Matter__c> scope){
        // process each batch of records
        List<litify_pm__Matter__c> matters = new List<litify_pm__Matter__c>();
        //List<litify_pm__Matter_Stage_Activity__c> stages = new List<litify_pm__Matter_Stage_Activity__c>();
        //List<litify_pm__Matter_Stage_Activity__c> discoveryStages = new List<litify_pm__Matter_Stage_Activity__c>();
        litify_pm__Matter_Stage_Activity__c WorkSettled;
        litify_pm__Matter_Stage_Activity__c settled;
        litify_pm__Matter_Stage_Activity__c discovery;
        
        for (litify_pm__Matter__c matter : scope){
            if (matter.RecordTypeId == '0126A000000NjDyQAK'){
                matter.RecordTypeId = '0123s000000ywXyAAI';
                matters.add(matter);
            }
            else{
                matter.RecordTypeId = '0123s000000ywXzAAI';
                matters.add(matter);
            }
                
                
              
              litify_pm__Matter_Stage_Activity__c LitPrep = New litify_pm__Matter_Stage_Activity__c();
              LitPrep.Name = 'Litigation Preparation';
                LitPrep.litify_pm__Matter__c = matter.Id;
                LitPrep.litify_pm__Order__c = 8;
                LitPrep.litify_pm__Original_Matter_Stage__c = 'a0k6A00000MU022QAD';
                 LitPrep.litify_pm__Stage_Status__c = 'Idle';
              //stages.add(LitPrep);
              upsert(LitPrep);  
            
              litify_pm__Matter_Stage_Activity__c Filed = New litify_pm__Matter_Stage_Activity__c();
              Filed.Name = 'Filed';
                Filed.litify_pm__Matter__c = matter.Id;
                Filed.litify_pm__Order__c = 9;
                Filed.litify_pm__Original_Matter_Stage__c = 'a0k6A00000MU02CQAT';
                 Filed.litify_pm__Stage_Status__c = 'Idle';
              //stages.add(Filed);
              upsert(Filed);
            
              litify_pm__Matter_Stage_Activity__c Served = New litify_pm__Matter_Stage_Activity__c();
              Served.Name = 'Served';
                Served.litify_pm__Matter__c = matter.Id;
                Served.litify_pm__Order__c = 10;
                Served.litify_pm__Original_Matter_Stage__c = 'a0k6A00000MU02HQAT';
                 Served.litify_pm__Stage_Status__c = 'Idle';
              //stages.add(Served);
              upsert(Served);
              
              litify_pm__Matter_Stage_Activity__c WrittenDiscovery = New litify_pm__Matter_Stage_Activity__c();
              WrittenDiscovery.Name = 'Written Discovery';
                WrittenDiscovery.litify_pm__Matter__c = matter.Id;
                WrittenDiscovery.litify_pm__Order__c = 11;
                WrittenDiscovery.litify_pm__Original_Matter_Stage__c = 'a0k6A00000MU02HQAT';
                 WrittenDiscovery.litify_pm__Stage_Status__c = 'Idle';
              //stages.add(WrittenDiscovery);
              upsert(WrittenDiscovery);
            
              litify_pm__Matter_Stage_Activity__c DOP = New litify_pm__Matter_Stage_Activity__c();
              DOP.Name = 'Deposition of Parties';
                DOP.litify_pm__Matter__c = matter.Id;
                DOP.litify_pm__Order__c = 12;
                DOP.litify_pm__Original_Matter_Stage__c = 'a0k6A00000MU02MQAT';
                 DOP.litify_pm__Stage_Status__c = 'Idle';
              //stages.add(DOP);
              upsert(DOP);
              
              litify_pm__Matter_Stage_Activity__c ExpertDep = New litify_pm__Matter_Stage_Activity__c();
              ExpertDep.Name = 'Expert Deposition';
                ExpertDep.litify_pm__Matter__c = matter.Id;
                ExpertDep.litify_pm__Order__c = 13;
                ExpertDep.litify_pm__Original_Matter_Stage__c = 'a0k6A00000MU02MQAT';
                 ExpertDep.litify_pm__Stage_Status__c = 'Idle';
              //stages.add(ExpertDep);
              upsert(ExpertDep);
            
              litify_pm__Matter_Stage_Activity__c Mediation = New litify_pm__Matter_Stage_Activity__c();
              Mediation.Name = 'Mediation';
                Mediation.litify_pm__Matter__c = matter.Id;
                Mediation.litify_pm__Order__c = 14;
                Mediation.litify_pm__Original_Matter_Stage__c = 'a0k6A00000MU02MQAT';
                 Mediation.litify_pm__Stage_Status__c = 'Idle';
              //stages.add(Mediation);
              upsert(Mediation);
              
              litify_pm__Matter_Stage_Activity__c Trial = New litify_pm__Matter_Stage_Activity__c();
              Trial.Name = 'Trial';
                Trial.litify_pm__Matter__c = matter.Id;
                Trial.litify_pm__Order__c = 15;
                Trial.litify_pm__Original_Matter_Stage__c = 'a0k6A00000MU02RQAT';
                 Trial.litify_pm__Stage_Status__c = 'Idle';
              //stages.add(Trial);
              upsert(Trial);
            
              litify_pm__Matter_Stage_Activity__c TrialPrep = New litify_pm__Matter_Stage_Activity__c();
              TrialPrep.Name = 'Trail Preparation';
                TrialPrep.litify_pm__Matter__c = matter.Id;
                TrialPrep.litify_pm__Order__c = 16;
                TrialPrep.litify_pm__Original_Matter_Stage__c = 'a0k6A00000MU02RQAT';
                 TrialPrep.litify_pm__Stage_Status__c = 'Idle';
              //stages.add(TrialPrep);
              upsert(TrialPrep);
            
            
            
                WorkSettled = [SELECT ID FROM litify_pm__Matter_Stage_Activity__c 
                           Where Name = 'Working to Settle' AND litify_pm__Matter__c = :matter.Id];
              WorkSettled.litify_pm__Order__c = 17;
              //stages.add(settled);
              update(WorkSettled);
                
              settled = [SELECT ID FROM litify_pm__Matter_Stage_Activity__c 
                           Where Name = 'Settled' AND litify_pm__Matter__c = :matter.Id];
              settled.litify_pm__Order__c = 18;
              //stages.add(settled);
              update(settled);
            
              discovery = [SELECT ID FROM litify_pm__Matter_Stage_Activity__c 
                           Where Name = 'Discovery' AND litify_pm__Matter__c = :matter.Id];
              //discoveryStages.add(discovery);
              delete(discovery);
        }
        update matters;
        //upsert stages;
        //delete discoveryStages;
    }    
    global void finish(Database.BatchableContext bc){
        // execute any post-processing operations
    }    
}

Test class (please don't laugh)
 
@isTest
private class MyBatchClassTest {
   static testMethod void BatchProcessAccount_TestMethod (){
         Test.StartTest();
        List<litify_pm__Matter__c> matter = [SELECT ID, RecordTypeId FROM litify_pm__Matter__c Where litify_pm__Matter_Plan__c = 'a0h6A000002nWNrQAM' AND (RecordTypeId = '0126A000000NjDyQAK' OR RecordTypeId = '0126A000000xP0cQAE')];
     
         litify_pm__Matter__c mat = new litify_pm__Matter__c(litify_pm__Display_Name__c = 'Darth', Case_Red_Flags__c='flag',
               litify_pm__Client__c ='0016A00000ITd3QQAT');
                insert mat;
         
         litify_pm__Matter_Stage_Activity__c stage = new litify_pm__Matter_Stage_Activity__c(Name = 'Settled', litify_pm__Order__c  =1,
                  litify_pm__Matter__c= mat.Id, litify_pm__Stage_Status__c = 'Idle');
                insert stage;
         
         List<litify_pm__Matter_Stage_Activity__c> stages = new List<litify_pm__Matter_Stage_Activity__c>();
         
         for (litify_pm__Matter__c i : matter){
             litify_pm__Matter_Stage_Activity__c stage1 = new litify_pm__Matter_Stage_Activity__c(Name = 'Settled', litify_pm__Order__c  =1,
                  litify_pm__Matter__c= i.Id, litify_pm__Stage_Status__c = 'Idle');
             stages.add(stage1);
         }
        insert(stages);
   
   MyBatchClass objBatch = new MyBatchClass();
   ID batchprocessid = Database.executeBatch(objBatch);
   Test.StopTest();
  }
    
}

​​​​​​​
Best Answer chosen by Jacob Elliott 8
Andrew GAndrew G
Hi there

Lacking coffee, but here is a possible solution:  (note - not compiled, so may not be correct syntax)
 
@isTest (SeeAllData=true)   //using SeeAllData tag is not best practice - we should really create all the test data - however the batch class file has explicit IDs (not the preferred practice)
private class MyBatchClassTest {
@IsTest
   static void BatchProcessAccount_TestMethod (){

	litify_pm__Client__c client = New litify_pm__Client__c ( Name='TestClient'  ); //add whatever fields are required to create a client record
	insert client;
//this is why we have the see all data tag on
	litify_pm__Matter_Plan__c testMatterPlan = [SELECT Id FROM litify_pm__Matter_Plan__c  WHERE Id = 'a0h6A000002nWNrQAM' LIMIT 1];
	System.assert('a0h6A000002nWNrQAM',testMatterPlan.Id);  //sanity check - do we have the matter plan - issue is that we are grabbing an existing record from the system based on Id, and the ID may or may not exist 
//Recordtypes are psuedo meta data, so the see all data tag is not required as we see them anyway - 
//Preferred option for getting the record type
	Map <String,Schema.RecordTypeInfo> recordTypesMatterPlan = litify_pm__Matter__c.sObjectType.getDescribe().getRecordTypeInfosByName();
	Id RecTypeId_1 = recordTypesMatterPlan.get('RecordTypeNameOne').getRecordTypeId();
	Id RecTypeId_2 = recordTypesMatterPlan.get('RecordTypeNameTwo').getRecordTypeId();

	System.assert('0126A000000NjDyQAK', RecTypeId_1.Id);  //Sanity Check 
	System.assert('0126A000000xP0cQAE', RecTypeId_2.Id);  //Sanity Check 

	litify_pm__Matter__c matter1 = new litify_pm__Matter__c(litify_pm__Display_Name__c = 'Test1', Case_Red_Flags__c='flag',litify_pm__Client__c = Client.Id, RecordTypeId = RecTypeId_1.Id);
	litify_pm__Matter__c matter2 = new litify_pm__Matter__c(litify_pm__Display_Name__c = 'Test2', Case_Red_Flags__c='flag',litify_pm__Client__c = Client.Id, RecordTypeId = RecTypeId_2.Id);
	List< litify_pm__Matter__c > matters = new List<litify_pm__Matter__c >();
	matters.add(matter1);
	matters.add(matter2);
	insert matters;
         
        litify_pm__Matter_Stage_Activity__c stage = new litify_pm__Matter_Stage_Activity__c(Name = 'Settled', litify_pm__Order__c  =1,
                  litify_pm__Matter__c= mat.Id, litify_pm__Stage_Status__c = 'Idle');
                insert stage;
         
        List<litify_pm__Matter_Stage_Activity__c> stages = new List<litify_pm__Matter_Stage_Activity__c>();
         
        for (litify_pm__Matter__c i : matter){
             litify_pm__Matter_Stage_Activity__c stage1 = new litify_pm__Matter_Stage_Activity__c(Name = 'Settled', litify_pm__Order__c  =1,
                  litify_pm__Matter__c= i.Id, litify_pm__Stage_Status__c = 'Idle');
             stages.add(stage1);
         }
        insert(stages);

         Test.StartTest();
   
	   MyBatchClass objBatch = new MyBatchClass();
	   ID batchprocessid = Database.executeBatch(objBatch);
	   Test.StopTest();
//run some sort of assertion here - perhaps
	litify_pm__Matter__c resultMatter1 = [SELECT Id, RecordTypeId FROM litify_pm__Matter__c  WHERE Id = matter1.Id];
	System.assertEquals('0123s000000ywXyAAI',resultMatter1.RecordTypeId);


  }
    
}

 

All Answers

Andrew GAndrew G
Hi there

Lacking coffee, but here is a possible solution:  (note - not compiled, so may not be correct syntax)
 
@isTest (SeeAllData=true)   //using SeeAllData tag is not best practice - we should really create all the test data - however the batch class file has explicit IDs (not the preferred practice)
private class MyBatchClassTest {
@IsTest
   static void BatchProcessAccount_TestMethod (){

	litify_pm__Client__c client = New litify_pm__Client__c ( Name='TestClient'  ); //add whatever fields are required to create a client record
	insert client;
//this is why we have the see all data tag on
	litify_pm__Matter_Plan__c testMatterPlan = [SELECT Id FROM litify_pm__Matter_Plan__c  WHERE Id = 'a0h6A000002nWNrQAM' LIMIT 1];
	System.assert('a0h6A000002nWNrQAM',testMatterPlan.Id);  //sanity check - do we have the matter plan - issue is that we are grabbing an existing record from the system based on Id, and the ID may or may not exist 
//Recordtypes are psuedo meta data, so the see all data tag is not required as we see them anyway - 
//Preferred option for getting the record type
	Map <String,Schema.RecordTypeInfo> recordTypesMatterPlan = litify_pm__Matter__c.sObjectType.getDescribe().getRecordTypeInfosByName();
	Id RecTypeId_1 = recordTypesMatterPlan.get('RecordTypeNameOne').getRecordTypeId();
	Id RecTypeId_2 = recordTypesMatterPlan.get('RecordTypeNameTwo').getRecordTypeId();

	System.assert('0126A000000NjDyQAK', RecTypeId_1.Id);  //Sanity Check 
	System.assert('0126A000000xP0cQAE', RecTypeId_2.Id);  //Sanity Check 

	litify_pm__Matter__c matter1 = new litify_pm__Matter__c(litify_pm__Display_Name__c = 'Test1', Case_Red_Flags__c='flag',litify_pm__Client__c = Client.Id, RecordTypeId = RecTypeId_1.Id);
	litify_pm__Matter__c matter2 = new litify_pm__Matter__c(litify_pm__Display_Name__c = 'Test2', Case_Red_Flags__c='flag',litify_pm__Client__c = Client.Id, RecordTypeId = RecTypeId_2.Id);
	List< litify_pm__Matter__c > matters = new List<litify_pm__Matter__c >();
	matters.add(matter1);
	matters.add(matter2);
	insert matters;
         
        litify_pm__Matter_Stage_Activity__c stage = new litify_pm__Matter_Stage_Activity__c(Name = 'Settled', litify_pm__Order__c  =1,
                  litify_pm__Matter__c= mat.Id, litify_pm__Stage_Status__c = 'Idle');
                insert stage;
         
        List<litify_pm__Matter_Stage_Activity__c> stages = new List<litify_pm__Matter_Stage_Activity__c>();
         
        for (litify_pm__Matter__c i : matter){
             litify_pm__Matter_Stage_Activity__c stage1 = new litify_pm__Matter_Stage_Activity__c(Name = 'Settled', litify_pm__Order__c  =1,
                  litify_pm__Matter__c= i.Id, litify_pm__Stage_Status__c = 'Idle');
             stages.add(stage1);
         }
        insert(stages);

         Test.StartTest();
   
	   MyBatchClass objBatch = new MyBatchClass();
	   ID batchprocessid = Database.executeBatch(objBatch);
	   Test.StopTest();
//run some sort of assertion here - perhaps
	litify_pm__Matter__c resultMatter1 = [SELECT Id, RecordTypeId FROM litify_pm__Matter__c  WHERE Id = matter1.Id];
	System.assertEquals('0123s000000ywXyAAI',resultMatter1.RecordTypeId);


  }
    
}

 
This was selected as the best answer
Jacob Elliott 8Jacob Elliott 8
Thanks, Andrew, not only for the code but explaining why certain things were necessary and best practices.