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
Ramana VRamana V 

Test class for batch apex which copies data from one object to another

Hi All,
I have written a batch apex to copy approval object information to another custome object. Below is my code and I am facing issues with test class.
global class approvalcopy implements Database.Batchable<sObject>{

    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'Select id,Approver_Comments__c,Approv__Approval_Status__c,(Select Id,CreatedDate,Approval__Actual_Approver__r.UserName,Approval__Initial_Submitter__r.username,Approval__Approver_Comments__c,Approval__SubprocessSequence__c FROM Approv__ApprovalRequests__r) From Proposal__Proposal__c where Approv__Approval_Status__c = \'Approved\' ';
        return Database.getQueryLocator(query);
    }     
    global void execute(Database.BatchableContext BC, List<Proposal__Proposal__c> setaudList) {
        list<Audit_Log__c> aud = new List<Audit_Log__c>();
        for(Proposal__Proposal__c s : setaudList) {        
            Audit_Log__c a = new Audit_Log__c();
            a.Action__c = 'Approval Information';
            a.Description__c = s.Approv__ApprovalRequests__r[0].Approval__Approver_Comments__c;
            a.Date__c = s.Approv__ApprovalRequests__r[0].CreatedDate;
            a.User__c = s.Approv__ApprovalRequests__r[0].Approval__Initial_Submitter__r.username;
            a.Delegate_User__c = s.Approv__ApprovalRequests__r[0].Approval__Actual_Approver__r.UserName;
            aud.add(a);
        }
        try {
            insert aud;
            
        } catch(Exception e) {
            System.debug(e);
        }
        
    }   
    
    global void finish(Database.BatchableContext BC) {
        
    }
}

I have written a test class by creating test data for proposal object, approval object and called batch class inside Test.starttest and stop. But still it is not covering code coverage. Please help me with any suggestions.

Thanks in Advance

Regards,
Ramana
AbhinavAbhinav (Salesforce Developers) 
Hi Ramana,

If you are facing any specific issue with your test class you can share that so that community can help you better.

Please check below link which covers most scenarios for test class.

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

Thanks!