+ Start a Discussion
Navya sree 4Navya sree 4 

Code Coverage Help?

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);


    public void finish(Database.BatchableContext context){

Here is a good link that explains how to test-cover Batchable classes :

So for you the test class will look like something like this :
private class CancelPendingApprovalsOfLostDealsTest {
    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() {        
        CancelPendingApprovalsOfLostDeals  cpaold = new CancelPendingApprovalsOfLostDeals ();
        Id batchId = Database.executeBatch(cpaold);
        // after the testing stops, assert records were updated properly

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