@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 Answer
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);
}
}
}
Date.newInstance(2018, 12, 9)
, its better to avoid hard coded values.