0

I have created test class for batch apex but it only covers 36% of code. How do I increase the coverage? Below is the test class that I have created.

@isTest
public class CloseOpportunityBatchTest {  
    @isTest
    public static void testCloseOpportunity(){
        List<Opportunity> listofopp = new List<Opportunity>();
        for(Integer i = 0; i < 200; i++) {
            Opportunity opp = new Opportunity();
            opp.StageName = 'stageName';
            opp.Closed__c = 'closingReason';
            opp.Action_Date__c = 2019-01-01;
        }
        insert listofopp;

        Test.startTest();
        CloseOpportunityBatch obj = new CloseOpportunityBatch();
        Database.executeBatch( obj );
        Test.stopTest();
    }
}

below is the Batch Apex Class to be tested:-

public class CloseOpportunityBatch implements Database.Batchable<SObject> {

    public void execute(SchedulableContext scon) {
        Database.executeBatch(new CloseOpportunityBatch());
    }
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT Id, StageName, Closed__c, Action_Date__c' +
            ' FROM Opportunity' +
            ' WHERE StageName = \'Imported\'' +
            ' AND Action_Date__c < LAST_N_DAYS:60'
        );
    } 
    public void execute(Database.BatchableContext bc, List<Opportunity> records) {
        for( Opportunity record : records) {
            record.StageName = 'System Closed';
            record.Closed__c = 'Expired';
        }
        update records;
    }
    public void finish(Database.BatchableContext bc) {
    }
}

2 Answers 2

2

There are two points here.

  1. You are taking schedulable instance in execute method. But you have not implemented Schedulable interface. I am assuming you are trying to implement both interfaces, batchable and schedulable in same class. Your main class should implement schedulable.

    public class AutoCloseOpportunityBatch implements Database.Batchable,Schedulable

  2. You need to check if there are any missing required fields for opportunity object in your org, other wise the insert fails and no test coverage is increased

Try below code. In my org required fields were name and close date. So added them in testclass as follows.

@isTest
public class AutoCloseOpportunityBatchTest {  
    @isTest
    public static void testAutoCloseOpportunity(){
        List<Opportunity> listofopp = new List<Opportunity>();
        for(Integer i = 0; i < 200; i++) {
    Opportunity opp = new Opportunity();
            opp.Name='test';
            opp.CloseDate=Date.newInstance(2019, 1, 1);
    opp.StageName = 'Imported';
    opp.Closing_Reason__c = 'closingReason';
    opp.Action_By_Date__c = Date.newInstance(2019, 1, 1);
    listofopp.add(opp);
        }
        insert listofopp;

        Test.startTest();
        AutoCloseOpportunityBatch obj = new AutoCloseOpportunityBatch();
        Database.executeBatch( obj );
        String sch = '0 0 23 * * ?';
        system.schedule('Test status Check', sch, obj  );
        Test.stopTest();
    }
}

I was able to achieve 100% coverage by doing these points. enter image description here

2

The filters need to match, so:

opp.StageName = 'stageName';

Should be:

opp.StageName = 'Imported';

And:

opp.Action_By_Date__c = 2019-01-01;

Should be a proper date (in Apex, you wrote the mathematical calculation 2019-1-1, the result being 2017).

You use Date.newInstance(year, month, day) for this:

opp.Action_By_Date__c = Date.newInstance(2019, 1, 1);

You also forgot to add the new record to the list of records to insert:

listofopp.add(opp);

The final code should look like:

for(Integer i = 0; i < 200; i++) {
    Opportunity opp = new Opportunity();
    opp.StageName = 'Imported';
    opp.Closing_Reason__c = 'closingReason';
    opp.Action_By_Date__c = Date.newInstance(2019, 1, 1);
    listofopp.add(opp);
}
1
  • Using the above code does not cover the execute method and still there is no impact on code coverage and it is skipping execute method.
    – sfdx007
    Dec 7, 2021 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.