0
@isTest
public class populateOpportunityTest {
    @isTest
    public static void testopportunity()
    {
        List<Opportunity> oppList= new List<Opportunity>();
        for(Integer i=1;i<=5;i++)
        {
            Opportunity opp= new Opportunity();
            opp.Name='TestOpp'+i;
            opp.StageName='Prospecting';
            opp.closeDate= Date.today()+10;
            oppList.add(opp);
        }
        if(oppList.size()>0)
            insert oppList;
        List<Opportunity> oppListUpdate= new List<Opportunity>();
        for(Opportunity opp:[SELECT StageName, CloseDate FROM Opportunity WHERE Name LIKE 'TestOpp%'])
        {
            opp.StageName='Closed Won';
            opp.CloseDate= Date.today()+12;
                oppListUpdate.add(opp);
        }
        PopulateOpportunityHandler.changeStage(oppList);
        if(oppListUpdate.size()>0)
            update oppListUpdate;


        for(Opportunity opp:[SELECT StageName, CloseDate FROM Opportunity WHERE Name LIKE 'TestOpp%'])
        {
            System.assertEquals(Date.newInstance(2018, 12, 9), opp.CloseDate);
        }
    }

}
1
  • 1
    Where are you stuck? If you are testing for closed date = today, then assert with today's date? Why are you checking against Date.newInstance(2018, 12, 9), its better to avoid hard coded values. Dec 9, 2018 at 8:25

1 Answer 1

1

You should avoid hard-coded date values in your test methods since it will be running in future and at that time date could be different.

Have modified your code

@isTest
public class populateOpportunityTest {
  @isTest
  public static void testopportunity() {
     List<Opportunity> oppList= new List<Opportunity>();
     for(Integer i=1;i<=5;i++) {
        Opportunity opp= new Opportunity();
        opp.Name='TestOpp'+i;
        opp.StageName='Prospecting';
        opp.closeDate= Date.today()+10;
        oppList.add(opp);
     }
     if(oppList.size()>0)
        insert oppList;
     List<Opportunity> oppListUpdate= new List<Opportunity>();
     for(Opportunity opp:[SELECT StageName, CloseDate FROM Opportunity WHERE Name LIKE 'TestOpp%']) {
        opp.StageName='Closed Won';
        opp.CloseDate= Date.today()+12;
        oppListUpdate.add(opp);
     }
     PopulateOpportunityHandler.changeStage(oppList);
     if(oppListUpdate.size()>0)
        update oppListUpdate;


     for(Opportunity opp:[SELECT StageName, CloseDate FROM Opportunity WHERE Name LIKE 'TestOpp%']) {
        System.assertEquals(Date.today()+12, opp.CloseDate);
      }
  }

}

You must log in to answer this question.

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