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
Navya sree 4Navya sree 4 

Code Coverage Help?

Hi 
Help me with test class


public with sharing class CancelPendingApprovalsOfLostDeals implements Database.Batchable<sObject> {

    public Database.QueryLocator start(Database.BatchableContext context) {
        return Database.getQueryLocator([SELECT Id, StageName FROM Opportunity WHERE StageName='Closed Lost']);
    }

    public void execute(Database.BatchableContext context, List<Opportunity> opportunities) {
        Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>();
        for (Opportunity opp: opportunities) {
            oppMap.put(opp.Id, opp);
        }

        OpportunityManagement.CancelApprovalsIfClosed(oppMap);
    }

    public void finish(Database.BatchableContext context){
                
    }
}

Thanks
bretondevbretondev
Here is a good link that explains how to test-cover Batchable classes :
https://trailhead.salesforce.com/en/modules/asynchronous_apex/units/async_apex_batch

So for you the test class will look like something like this :
 
@isTest
private class CancelPendingApprovalsOfLostDealsTest {
    @testSetup 
    static void setup() {
        List<Opportunity> opps = new List<Opportunity>();

        // insert 10 opps
        for (Integer i=0;i<10;i++) {
            opps.add(new Opportunity(name='Opp '+i, StageName='Closed Lost'));
        }
        insert opps;

    }
    static testmethod void test() {        
        Test.startTest();
        CancelPendingApprovalsOfLostDeals  cpaold = new CancelPendingApprovalsOfLostDeals ();
        Id batchId = Database.executeBatch(cpaold);
        Test.stopTest();
        // after the testing stops, assert records were updated properly
        System.assertEquals(...);
    }
    
}

For the System.assertEquals() line, I cannot help you because you did not share your code of your OpportunityManagement.CancelApprovalsIfClosed(oppMap) method.