+ 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;
        try {
            insert aud;
        } catch(Exception 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

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.