+ Start a Discussion
Saad Ahmad 27Saad Ahmad 27 

Issue with code coverage

I wrote this code but I'm only getting 60% coverage. I'm unable to figure out how to get to 100% coverage. Any help will be much appreciated!
 
global class InvoiceLineItemCleanUp implements Database.Batchable<SObject>, Database.Stateful {
	
    Date CutoffDate = Olympus__c.getOrgDefaults().Last_Modified_Date__c;
    
    global InvoiceLineItemCleanUp() {
    }
    
    global Database.QueryLocator start(Database.BatchableContext bc) {
        String searchRec = 'SELECT Id FROM Order_Line_Item__c WHERE CreatedDate > :CutoffDate AND LastModifiedDate <> LAST_N_DAYS:30';
        return Database.getQueryLocator(searchRec);
    }

    global void execute(Database.BatchableContext bc, List<SObject> batch) {
        List<Order_Line_Item__c> b = (List<Order_Line_Item__c>)batch;
        
        Database.delete(b);
        if(!b.isEmpty()) { Database.emptyRecycleBin(b); }
    }
    
    global void finish(Database.BatchableContext bc) {
    }
}



Here is my test code 
 
@isTest
private class InvoiceLineItemCleanUp_test {    
    static testMethod void test() { 
        Order_Line_Item__c invoiceLine = new Order_Line_Item__c(Quantity__c = 1);
        insert invoiceLine;
               
        Datetime yesterday = Datetime.now().addDays(-31);
        Test.setCreatedDate(invoiceLine.Id, yesterday);
               
        Test.startTest();
        Database.executeBatch(new InvoiceLineItemCleanUp());
        Test.stopTest();
    }
}


​​​​​​​
Raj VakatiRaj Vakati
Try this
 
@isTest
private class InvoiceLineItemCleanUp_test {    
    static testMethod void test() { 
	
	insert new Olympus__c(SetupOwnerId=UserInfo.getOrganizationId(), Last_Modified_Date__c=System.today().addDays(-39);

	
	
        Order_Line_Item__c invoiceLine = new Order_Line_Item__c(Quantity__c = 1);
        insert invoiceLine;
               
        Datetime yesterday = Datetime.now().addDays(-31);
        Test.setCreatedDate(invoiceLine.Id, yesterday);
               
        Test.startTest();
        Database.executeBatch(new InvoiceLineItemCleanUp());
        Test.stopTest();
    }
}

 
Saad Ahmad 27Saad Ahmad 27
Thanks, Raj. I tried this but the coverage is still stuck at 60%. These are the lines with the issue: User-added image