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
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Test Class for Bath Apex - Please Help!!!

Hi All-
     I need your help in writing a test class for the following batch apex class. This batch creates new child records on being processed. I saw multiple posts where batches update the exisiting record and understood how to writes a test class for the same. But since my batch is creating completely new records I am having difficulty in writing its test class. I have written something but that does not cover the entire execute methos , it covers everything else. Please help me.

Batch Class: 

global class CreateNewSummaryFormRecords implements Database.Batchable<Sobject>, Database.Stateful {

    global Database.QueryLocator start(Database.BatchableContext bc) {
        //first, you gather all the records you want to create a new record with
        String query = 'Select Id,Risk_Assessment_Active__c,Project_Manager__c,Chair_Reviewer__c from Project_Risk_Review__c where Risk_Assessment_Active__c = true and Review_Meeting_Date_Tentative__c = False and Number_Of_Days_For_Next_Review__c=2';
        //below checks to see if the batch is being called from a test class. If the record count
        //returned by the query above is over 200, the test will fail
        if(Test.isRunningTest())
            query += ' limit 200';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc, List<Project_Risk_Review__c> originalrecords){
        //now we want to loop through the records from the query above and create the new records
        //and store them in a list
        List<Project_Form__c> newrecords = new List<Project_Form__c>();
        
        for(Project_Risk_Review__c yo1:originalrecords){ // this is the loop
            Project_Form__c yo2 = new Project_Form__c(); //this sets up a new record to be created
            yo2.Project_Risk_Assessment__c= yo1.Id;
            yo2.RecordTypeId=Label.PRAS_Project_Summary_Form_Record_Type;
            yo2.Project_Manager__c=yo1.Project_Manager__c;
            yo2.Chair_Reviewer__c=yo1.Chair_Reviewer__c;
            //add any other fields on the new record that you want to set
            newrecords.add(yo2); 
            //add to list to insert into SFDC later
        }

        //this checks to see if there are any records to insert. If there are, then it will 
        //create them
        if(!newrecords.isEmpty())
            database.insert(newrecords,false); 
        //newrecords is the list of records you put in during the for loop. The "false" is saying,
        //if one records fails to insert, still insert the rest (All or Nothing)
    }
    
    global void finish(Database.BatchableContext bc) {}
}

Sample Test Class:


@isTest
public class CreateNewSummaryFormRecordsTest
{
 
  static testMethod void testMethod1(){
        Account testAccount = TestUtil.generateTestAccount();
        insert testAccount;

        Opportunity testOpp = new Opportunity();
        testOpp.Name = 'Opportunity For Unit Test';
        testOpp.AccountId = testAccount.Id;
        testOpp.StageName = 'Qualification';
        testOpp.CloseDate = Date.Today().AddDays(365);
        testOpp.Org__c= '1010101';
        testOpp.Project_Director__c = UserInfo.getUserId();
        testOpp.RAQ_Human_Subjects__c='Yes';
        testOpp.Amount=1000000;
        testOpp.Estimated_Subcontractor_Amount__c=1000000;
        insert testOpp;
        
       
        List<User> u1= [SELECT Id,IsActive FROM User WHERE isActive=True and ProfileId=:Label.PRAS_RTI_BusinessDevelopment_Profile_Id Limit 4];
        List<User> u2= [SELECT Id,IsActive FROM User WHERE isActive=False and ProfileId=:Label.PRAS_RTI_BusinessDevelopment_Profile_Id Limit 1];
        
        Costpoint_Project__c testCostpointProject = new Costpoint_Project__c();
        testCostpointProject.Name = '0281300.003';
        testCostpointProject.Project_Name__c = '0281300.003';
        testCostpointProject.Project_Number__c = '0281300.003';
        testCostpointProject.Owning_Reorg_Level_6__c='1.2.1.30.03.03';
        testCostpointProject.Contract_Type__c='GRANT';
        testCostpointProject.Contract_Value__c=100000000;
        testCostpointProject.Business_Unit__c='SSES';
        testCostpointProject.Unit__c='EHS';
        testCostpointProject.Division__c='Environment';
        testCostpointProject.Review_Required__c=False;
        testCostpointProject.Project_Manager__c=u1[0].Id;
        insert testCostpointProject;
        
       
        //Fetch related Project Risk Review form.
        Project_Risk_Review__c ProRiskRev=[Select id,DVP__c,Project_Manager__c,Chair_Reviewer__c,Delegate__c 
                                           from Project_Risk_Review__c where Costpoint_Project__c=:testCostpointProject.Id Limit 1];
                                            ProRiskRev.Chair_Reviewer__c=u1[0].Id;
        ProRiskRev.DVP__c=u1[1].Id;
        ProRiskRev.Project_Manager__c=u1[2].Id;
        ProRiskRev.Delegate__c=u1[3].Id;
        Update ProRiskRev;
        ProRiskRev.Chair_Reviewer__c=u1[3].Id;
        ProRiskRev.DVP__c=u1[2].Id;
        ProRiskRev.Project_Manager__c=u1[1].Id;
        ProRiskRev.Delegate__c=u1[0].Id;
        Update ProRiskRev;
        
        Project_Form__c ProForm=new Project_Form__c();
        ProForm.Project_Risk_Assessment__c=ProRiskRev.Id;
        ProForm.RecordTypeId=Label.PRAS_Project_Summary_Form_Record_Type;
        ProForm.Project_Manager__c=ProRiskRev.Project_Manager__c;
        ProForm.Chair_Reviewer__c=ProRiskRev.Chair_Reviewer__c;
        insert ProForm;
        
        
        Test.startTest();
        CreateNewSummaryFormRecords obj = new CreateNewSummaryFormRecords();
        DataBase.executeBatch(obj);
        Test.stopTest();

 
         
 }

}
 
sasmitasasmita
Hi Shrey,
I am not sure which part of your class/method is not getting covered for test coverage.
I could suggest few finding.
1. If your using SOQL inside test class, better use annotation @seeAllDataTrue. So your test class will access org data to satisfy your method. Though this is not the best solution. you should create your own data to be used inside test method. Even you should create a user inside the test method rather than querying u1/u2.
2. your batch class execute method looks for Project_Risk_Review__c> originalrecords. Does your test method have list of Project_Risk_Review__c before you call DataBase.executeBatch(obj);?

Hope this would help you!.

Thanks,
Sasmita