You need to sign in to do that
Don't have an account?
William Keam
Need help writing test class
Hello everyone,
I have an apex class that is used to calculate the Net Promoter Score based on surveys returned back into a custom object in salesforce, the code works and calculates the correct values but I am unsure on how to get started writing test code for the class. The code is as follows:
I have an apex class that is used to calculate the Net Promoter Score based on surveys returned back into a custom object in salesforce, the code works and calculates the correct values but I am unsure on how to get started writing test code for the class. The code is as follows:
public class NPScalc{ public NPScalc(ChartController controller) { } public double all{public get; public set;} public double notlikely{public get; public set;} public double neutral{public get; public set;} public double extremelylikely{public get; public set;} public double promoters {public get; public set;} public double detractors {public get; public set;} public double NPS {public get; public set;} public NPScalc(ApexPages.StandardController controller) { List <GetFeedback__c> allnotlikely=[Select CreatedDate, NPS__c from GetFeedback__c where (NPS__c=0 or NPS__c=1 or NPS__c=2 or NPS__c=3 or NPS__c=4 or NPS__c=6 or NPS__c=6) LIMIT 10000]; double d=0.0; integer notlikely=0; if(allnotlikely.size()>0){ for(GetFeedback__c c: allnotlikely) { d+=c.NPS__c; notlikely++; } } List <GetFeedback__c> allneutral=[Select CreatedDate, NPS__c from GetFeedback__c where (NPS__c=7 or NPS__c=8) LIMIT 10000]; double e=0.0; integer neutral=0; if(allneutral.size()>0){ for(GetFeedback__c c: allneutral) { e+=c.NPS__c; neutral++; } } List <GetFeedback__c> allextremelylikely=[Select CreatedDate, NPS__c from GetFeedback__c where (NPS__c=9 or NPS__c=10) LIMIT 10000]; double f=0.0; integer extremelylikely=0; if(allextremelylikely.size()>0){ for(GetFeedback__c c: allextremelylikely) { f+=c.NPS__c; extremelylikely++; } } //Calculate NPS making sure not to divide by zero if(extremelylikely==0&¬likely==0){ promoters=0; detractors=0; all=0; NPS=0; } else if(extremelylikely==0&¬likely>0){ extremelylikely=0; all=extremelylikely+neutral+notlikely; promoters=0; detractors=math.FLOOR((notlikely/all)*100); NPS=math.FLOOR(promoters-detractors); } else if(extremelylikely>0&¬likely==0){ notlikely=0; all=extremelylikely+neutral+notlikely; promoters=math.FLOOR((extremelylikely/all)*100); detractors=0; NPS=math.FLOOR(promoters-detractors); } else if(extremelylikely>0&¬likely>0){ all=extremelylikely+neutral+notlikely; promoters=math.FLOOR((extremelylikely/all)*100); detractors=math.FLOOR((notlikely/all)*100); NPS=math.FLOOR(promoters-detractors); } } }Any pointers, advice or anything would be greatly appreciated!
I was able to modify an existing test class and I now have 75% Coverage, 12 of the last lines are not covered, I have commented at the end of each of the lines
Here is the test class so far:
Here is the apex class:
As you have "if.. else" statements in the code, I will suggest you to cover your "If" and "Else" statements by writing both positive and negative tests which will cover bothe the parts of conditional "If..Else" statements.
You can refer to this post:
https://developer.salesforce.com/forums/ForumsMain?id=906F000000091SnIAI
Thanks,
Pratik
P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.