+ Start a Discussion
Geoffrey NixGeoffrey Nix 

Trying to run a test class to test a method that uses batchable apex to retrieve Opportunities with less than 30 days til close and give a 10% discount to those Opportunities. My code is below and I'm getting errors

@isTest
private class NotifyClosingOppsTest {
    @testSetup
    static void setup(){
        Opportunity [] opps = new List <Opportunity>();
        
        for(Integer i=0; i<50; i++) {
           opps.add(new Opportunity(Name ='Tester ' +i,
                     CloseDate = date.newInstance(2020, 1, 1), Stage = 'Needs Analysis'));
        }
        insert opps;
        
        static testmethod void test() {
            Test.startTest();
            NotifyClosingOpps oppTester = new NotifyClosingOpps();
            Id BatchId = Database.executeBatch(oppTester);
            Test.stopTest();
            
            System.assert(Opportunity.CloseDate < 30);
            }
        }
}
Greg HGreg H
You need a dynamic CloseDate value. Use something  like CloseDate = Date.Today() to populate with a close date of today or CloseDate = Date.Today().addDays(3) for a close date three days from now.
-greg