-1

How can I get increased coverage for this code? I get only 75% so far.

Not able to cover the delete statement in the main class.

Any help is greatly appreciated.

Main Class:

public void execute(SchedulableContext schedulevariable)
    {

        //Variable Declaration
        List<Case> CaseList = new List<Case>();

        try{

           CaseList = [SELECT Id,Privacy_Issue__c,Status,SystemModstamp FROM Case 
                        WHERE Status ='Closed' AND Privacy_Issue__c = True AND 
                        SystemModstamp < :system.now().addDays(-63)];

            if(CaseList.size()>0){
                Database.delete(CaseList ,false);
            }
        }
        Catch(Exception ex){
            system.debug('Exception Message::'+ex.getMessage());
        }
    }  
}

Test Class:

@isTest(seealldata=false)
public class DeleteCasesBatch_Test
{   

 static testmethod void executeTest()
    {
        List<case> caseList = new List<Case>();
        //Runas System Admin
        TestUtility.getRunAsUser();

        System.runAs(TestUtility.runAsUser){

            //Case record Insertion
            Case c1 = TestUtility.createCase(); 
            c1.Status='Assigned';
            c1.Privacy_Issue__c= true;
            Database.insert(c1);

            c1.Status = 'Closed';
            Database.update(c1);

            DeleteCasesBatch ads = new DeleteCasesBatch();
            ads.execute(null);

            Test.stopTest();

            system.assertNotEquals(c1, null ,'Case Record is Null');

            Database.delete(c1);

        }
    }
}
2

Use Test.setCreatedDate() method for the case created, to set the date Days which are -63 from Today's Date in test class. This will help you to meet the last condition written in query from execute method & thus your test class will provide you coverage for delete statement from execute method.

| improve this answer | |

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