1

I have written a apex class with test class both are running but my code coverage is only 66%.Please help to solve the problem.

Apex Class-

    global class Deleteoppotest implements Schedulable{

    global void execute(SchedulableContext SC) {
        deleteoppo();
    }

    public static void deleteoppo() {

        for(List<opportunity> objoppo : [SELECT Id FROM opportunity WHERE CreatedDate <= :(Date.Today() - 200) LIMIT 10])
            {
                delete objoppo;
            }    
    }
}

Test Class

    @isTest
private class Deleteoppotestcls {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test



        Opportunity op = new Opportunity();
        op.Name = 'test1';
        op.StageName = 'Qualified';
        op.CloseDate = Date.Today();
        op.Description ='testingoppotestcls';
        op.Business_Type__c = 'Domestic';


        insert op;


        Deleteoppotest.deleteoppo();

    }
}
1
  • NEVER, .... you never put a database operation (insert/update/delete/upsert) in a loop.
    – user853710
    Nov 8, 2018 at 18:02

2 Answers 2

3

Few things to be noticed here.

  1. Your DML should be outside like this and also no need to use for loop

simple DML on list

public static void deleteoppo() {
      delete  [SELECT Id FROM opportunity 
               WHERE CreatedDate <= :(Date.Today() - 200) 
               LIMIT 10];
}
  1. Second, create records in test class with old created date

    Opportunity op = new Opportunity();
    op.Name = 'test1';
    op.StageName = 'Qualified';
    op.CloseDate = Date.Today();
    op.Description ='testingoppotestcls';
    op.Business_Type__c = 'Domestic';
    insert op
    Datetime dt200before = Datetime.now().addDays(-201);
    Test.setCreatedDate(op.Id, dt200before );
    

This will set the created date 201 before date

  1. Since it is a scheduler class you can schedule the class

      Test.startTest();
      Deleteoppotest objDelOpp = new Deleteoppotest();
      System.schedule('delete opp - Monday 5AM', '0 00 05 ? * 2', objDelOpp );
      Test.stopTest();
    

This will cover the all the code

  1. And Lastly Don't forget to add asserts to check if data is deleted or not
1

Here is test class that should give 100% test coverage for a schedulable class.

@isTest(SeeAllData=false)
private class Deleteoppotestcls {

    @TestSetup
    public static void createTestData() {
        Opportunity op = new Opportunity();
        op.Name = 'test1';
        op.StageName = 'Qualified';
        op.CloseDate = Date.Today();
        op.Description ='testingoppotestcls';
        op.Business_Type__c = 'Domestic';
        insert op;
    }

    @IsTest
    static void shouldScheduleDeletion() {
        DateTime currTime = DateTime.now();
        Integer min = currTime.minute();
        Integer hour = currTime.hour();
        String sch;

        if(min <= 58)
            sch = '0 '+ (min + 1) + ' ' + hour + ' * * ?';
        else          
            sch = '0 0 '+ (hour + 1) + ' * * ?';

        Test.startTest();
        String jobId = system.schedule('Test Deletion ' + currTime, sch, new Deleteoppotest());        
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE Id = :jobId];
        System.assertEquals(sch, ct.CronExpression);
        Test.stopTest();
    }
}

Hope this helps.

4
  • It won't though. The query only returns records that have a created date over 200 days ago. The test record that you create will not be returned in the query, and the delete objoppo; line of code will still not be covered.
    – martin
    Aug 29, 2016 at 8:07
  • Thanks for info. However, I tested that class in dev org and its 100% covered.
    – jagmohan
    Aug 29, 2016 at 11:43
  • I stand corrected. I had assumed the for loop was operating over single Opportunities, but it is actually operating over lists of Opportunities. Your answer does in fact give 100% code coverage.
    – martin
    Aug 29, 2016 at 12:21
  • The code has worked
    – akash garg
    Aug 30, 2016 at 3:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .