You need to sign in to do that
Don't have an account?
Alexandru Moise
apex test class coverage issue
Hello,
I have an issue with an Apex Test Class and I would really need your help. I have a Apex Trigger and an Apex Class and I have tried to create a test class but I can't get it over 70% code coverage.
The class is:
public with sharing class OpportunitySchedulingHandler {
//Update LineItemSchedule dates for all scheduling dates
public static void ScheduleDateUpdate(String oppid, Integer DayDiff)
{
List<OpportunityLineItem> idval = [SELECT id FROM OpportunityLineItem WHERE OpportunityId=:oppid];
List<OpportunityLineItemSchedule> datelist = new List<OpportunityLineItemSchedule>();
for (Integer i = 0; i < idval.size(); i++)
{
datelist = [SELECT ScheduleDate FROM OpportunityLineItemSchedule WHERE OpportunityLineItemId =:idval[i].id];
Date firstDate = datelist[0].ScheduleDate.addDays(DayDiff);
datelist[0].ScheduleDate = firstDate;
Integer day = firstDate.day();
Integer month = firstDate.month();
Integer year = firstDate.year();
for (Integer k = 1; k < datelist.size(); k++)
{
Integer nYear = year;
Integer nMonth = month + k;
Integer nDay = day;
if (nMonth > 12) {
nMonth = nMonth - 12;
nYear = nYear + 1;
}
Set<Integer> longMonths = new Set<Integer> {1,3,5,7,8,10,12};
if (nDay == 31 && ! longMonths.contains(nMonth)) {
nDay = 30;
}
if (nDay > 28 && nMonth == 2) {
nDay = 28;
}
Date mydate = date.newInstance(nYear,nMonth,nDay);
datelist[k].ScheduleDate = mydate;
}
if(!datelist.isEmpty())
{
update datelist;
}
}
}
//Update ServiceDate with closeDate
public static void ScheduleServiceDateUpdate(String oppid)
{
List<Opportunity> oliList = [SELECT Id, Name, CloseDate, (SELECT Id, ServiceDate, OpportunityId from OpportunityLineItems) from Opportunity where Id =:oppid];
List<OpportunityLineItem> oliUpdateList = new List<OpportunityLineItem>();
for(Opportunity x : oliList)
{
for(OpportunityLineItem oli : x.OpportunityLineItems)
{
oli.ServiceDate = x.CloseDate;
oliUpdateList.add(oli);
}
}
if(!oliUpdateList.isEmpty())
{
update oliUpdateList;
}
}
}
The trigger is:
trigger OpportunityReScheduling on Opportunity (after update, before update, after insert)
{
for (Opportunity o: Trigger.new)
{
if (Trigger.isBefore)
{
Opportunity prevOpportunity = Trigger.oldMap.get(o.ID);
if (o.CloseDate != prevOpportunity.CloseDate)
{
Integer DayDiff = prevOpportunity.CloseDate.daysBetween(o.CloseDate);
OpportunitySchedulingHandler.ScheduleDateUpdate(o.id, DayDiff);
}
}
if (Trigger.isAfter || Trigger.isInsert)
{
OpportunitySchedulingHandler.ScheduleServiceDateUpdate(o.id);
}
}
}
and the Test class is:
trigger OpportunityReScheduling on Opportunity (after update, before update, after insert)
{
for (Opportunity o: Trigger.new)
{
if (Trigger.isBefore)
{
Opportunity prevOpportunity = Trigger.oldMap.get(o.ID);
if (o.CloseDate != prevOpportunity.CloseDate)
{
Integer DayDiff = prevOpportunity.CloseDate.daysBetween(o.CloseDate);
OpportunitySchedulingHandler.ScheduleDateUpdate(o.id, DayDiff);
}
}
if (Trigger.isAfter || Trigger.isInsert)
{
OpportunitySchedulingHandler.ScheduleServiceDateUpdate(o.id);
}
}
}
Can somebody help me figure this out? I still getting my way arround Apex and I would really appreciate any assistance
Thank you very much
Alex
I have an issue with an Apex Test Class and I would really need your help. I have a Apex Trigger and an Apex Class and I have tried to create a test class but I can't get it over 70% code coverage.
The class is:
public with sharing class OpportunitySchedulingHandler {
//Update LineItemSchedule dates for all scheduling dates
public static void ScheduleDateUpdate(String oppid, Integer DayDiff)
{
List<OpportunityLineItem> idval = [SELECT id FROM OpportunityLineItem WHERE OpportunityId=:oppid];
List<OpportunityLineItemSchedule> datelist = new List<OpportunityLineItemSchedule>();
for (Integer i = 0; i < idval.size(); i++)
{
datelist = [SELECT ScheduleDate FROM OpportunityLineItemSchedule WHERE OpportunityLineItemId =:idval[i].id];
Date firstDate = datelist[0].ScheduleDate.addDays(DayDiff);
datelist[0].ScheduleDate = firstDate;
Integer day = firstDate.day();
Integer month = firstDate.month();
Integer year = firstDate.year();
for (Integer k = 1; k < datelist.size(); k++)
{
Integer nYear = year;
Integer nMonth = month + k;
Integer nDay = day;
if (nMonth > 12) {
nMonth = nMonth - 12;
nYear = nYear + 1;
}
Set<Integer> longMonths = new Set<Integer> {1,3,5,7,8,10,12};
if (nDay == 31 && ! longMonths.contains(nMonth)) {
nDay = 30;
}
if (nDay > 28 && nMonth == 2) {
nDay = 28;
}
Date mydate = date.newInstance(nYear,nMonth,nDay);
datelist[k].ScheduleDate = mydate;
}
if(!datelist.isEmpty())
{
update datelist;
}
}
}
//Update ServiceDate with closeDate
public static void ScheduleServiceDateUpdate(String oppid)
{
List<Opportunity> oliList = [SELECT Id, Name, CloseDate, (SELECT Id, ServiceDate, OpportunityId from OpportunityLineItems) from Opportunity where Id =:oppid];
List<OpportunityLineItem> oliUpdateList = new List<OpportunityLineItem>();
for(Opportunity x : oliList)
{
for(OpportunityLineItem oli : x.OpportunityLineItems)
{
oli.ServiceDate = x.CloseDate;
oliUpdateList.add(oli);
}
}
if(!oliUpdateList.isEmpty())
{
update oliUpdateList;
}
}
}
The trigger is:
trigger OpportunityReScheduling on Opportunity (after update, before update, after insert)
{
for (Opportunity o: Trigger.new)
{
if (Trigger.isBefore)
{
Opportunity prevOpportunity = Trigger.oldMap.get(o.ID);
if (o.CloseDate != prevOpportunity.CloseDate)
{
Integer DayDiff = prevOpportunity.CloseDate.daysBetween(o.CloseDate);
OpportunitySchedulingHandler.ScheduleDateUpdate(o.id, DayDiff);
}
}
if (Trigger.isAfter || Trigger.isInsert)
{
OpportunitySchedulingHandler.ScheduleServiceDateUpdate(o.id);
}
}
}
and the Test class is:
trigger OpportunityReScheduling on Opportunity (after update, before update, after insert)
{
for (Opportunity o: Trigger.new)
{
if (Trigger.isBefore)
{
Opportunity prevOpportunity = Trigger.oldMap.get(o.ID);
if (o.CloseDate != prevOpportunity.CloseDate)
{
Integer DayDiff = prevOpportunity.CloseDate.daysBetween(o.CloseDate);
OpportunitySchedulingHandler.ScheduleDateUpdate(o.id, DayDiff);
}
}
if (Trigger.isAfter || Trigger.isInsert)
{
OpportunitySchedulingHandler.ScheduleServiceDateUpdate(o.id);
}
}
}
Can somebody help me figure this out? I still getting my way arround Apex and I would really appreciate any assistance
Thank you very much
Alex
Try following options one by one:
1. Check if there is any exception/error in debug log after update opp statment.
2. Add 'Update Opp' command w/o chaning Close date. Just after 'insert opli' statement.
3. Try invoking method directly from test class at the end of test class using: OpportunitySchedulingHandler.ScheduleServiceDateUpdate(opp.id);
-Thanks,
TK
All Answers
Can you share your test class? You copies Trigger code twice. Also, if possible, share the lines of main class covered by the test class from developer console. It will help to understand which part is already being covered.
-Thanks,
TK
Sorry about that, my test class is :
@isTest
private class OpportunityReSchedulingTest {
static testMethod void myUnitTest() {
// TO DO: implement unit test
Test.starttest();
Account acc = new Account();
acc.Name='NewAcc';
insert acc;
Opportunity opp = new Opportunity();
opp.Name='NewOpp';
opp.AccountId=acc.Id;
opp.StageName='Prospecting';
opp.CloseDate=Date.today().addDays(10);
insert opp;
Product2 Prod = new Product2();
Prod.Name='NewProd';
Prod.IsActive=True;
insert Prod;
PricebookEntry pbe = new PricebookEntry();
pbe.Product2Id=Prod.Id;
pbe.IsActive=True;
pbe.UnitPrice=70;
pbe.Pricebook2Id = Test.getStandardPricebookId();
pbe.UseStandardPrice=false;
insert pbe;
OpportunityLineItem opli = new OpportunityLineItem();
opli.UnitPrice = 57;
opli.Quantity = 12;
opli.OpportunityId=opp.Id;
opli.PricebookEntryId=pbe.id;
insert opli;
opp.CloseDate = opp.CloseDate.addDays(20);
update opp;
Test.stoptest();
}
}
The part of the code which is not covered is:
datelist = [SELECT ScheduleDate FROM OpportunityLineItemSchedule WHERE OpportunityLineItemId =:idval[i].id];
Date firstDate = datelist[0].ScheduleDate.addDays(DayDiff);
datelist[0].ScheduleDate = firstDate;
Integer day = firstDate.day();
Integer month = firstDate.month();
Integer year = firstDate.year();
for (Integer k = 1; k < datelist.size(); k++)
{
Integer nYear = year;
Integer nMonth = month + k;
Integer nDay = day;
if (nMonth > 12) {
nMonth = nMonth - 12;
nYear = nYear + 1;
}
Set<Integer> longMonths = new Set<Integer> {1,3,5,7,8,10,12};
if (nDay == 31 && ! longMonths.contains(nMonth)) {
nDay = 30;
}
if (nDay > 28 && nMonth == 2) {
nDay = 28;
}
Date mydate = date.newInstance(nYear,nMonth,nDay);
datelist[k].ScheduleDate = mydate;
}
if(!datelist.isEmpty())
{
update datelist;
}
}
Thank you very muhch for you suppport
Alex
Try following options one by one:
1. Check if there is any exception/error in debug log after update opp statment.
2. Add 'Update Opp' command w/o chaning Close date. Just after 'insert opli' statement.
3. Try invoking method directly from test class at the end of test class using: OpportunitySchedulingHandler.ScheduleServiceDateUpdate(opp.id);
-Thanks,
TK