You need to sign in to do that
Don't have an account?
MaheemSam
Test class coverage for wrapper inside the class
Hi,
Below is the controller
I was to call the wrapper below from class in test class please suggest me how to call.
public class ForeCastWrap {
public ForeCastWrap(String month, Date StDate){
this.month = month;
commitAmt = 0;
bestCaseAmt = 0;
this.StDate = StDate;
}
public String month {get; set;}
public String type;
public ID commitID {get; set;}
public Decimal commitAmt {get; set;}
public String cComment {get; set;}
public ID bestCaseID {get; set;}
public Decimal bestCaseAmt {get; set;}
public String bcComment {get; set;}
public Date StDate;
}
Thanks
Sudhir
Below is the controller
public with sharing class CForecastSubmitCnt{ public Map<id, Period> periodMap; Date todaydate = system.today(); public List<SelectOption> forecastQtr {get; set;} public String selectedForecastQ{get;set;} public List<Collaborative_Forecast_History__c> revenueHFCs {get; set;} public List<Collaborative_Forecast_History__c> enhanceHFCs {get; set;} //public List<Collaborative_Forecast_History__c> ServiceHFCs {get; set;} public list<ForeCastWrap> revenueFC {get; set;} public list<ForeCastWrap> enhanceFC {get; set;} //public list<ForeCastWrap> ServiceFC {get; set;} List<ForecastingOwnerAdjustment> upsertLst; List<Collaborative_Forecast_History__c> upsertHstLst; //History Map<String, Id> forecastingTypeMap; static string FORECAST_TYPE_REVENUE = 'Opportunity Revenue'; static string FORECAST_TYPE_ENHANCE = 'Enhanced Tech Amount'; //static string FORECAST_TYPE_SPLIT = 'Opportunity Split Revenue'; static string FORECAST_CATEGORY_COMMIT = 'CommitForecast'; static string FORECAST_CATEGORY_BESTCASE = 'BestCaseForecast'; public class ForeCastWrap { public ForeCastWrap(String month, Date StDate){ this.month = month; commitAmt = 0; bestCaseAmt = 0; this.StDate = StDate; } public String month {get; set;} public String type; public ID commitID {get; set;} public Decimal commitAmt {get; set;} public String cComment {get; set;} public ID bestCaseID {get; set;} public Decimal bestCaseAmt {get; set;} public String bcComment {get; set;} public Date StDate; } public CForecastSubmitCnt(){ if( getManagerEmail(UserInfo.getUserId()) == null ) { ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'No manager has been set up to receive this Forecast submission. Please contact the SFDC Team (sfdc@fortinet.com) for assistance')); } else { // Displays 4 quarter periodMap = new Map<id, Period>([ select id, StartDate, Number, type, EndDate, FullyQualifiedLabel from Period where type in ('Quarter', 'Month') and ( startdate = THIS_QUARTER or startdate=NEXT_N_QUARTERS:3) order by startdate]); forecastQtr = new List<SelectOption>(); for(Period p : periodMap.values()) { if(p.type == 'Quarter') { forecastQtr.add(new SelectOption(p.id,'Q'+p.Number+' '+p.StartDate.year() )); if(selectedForecastQ == null) selectedForecastQ = p.id; } } displayForecastTable(); displayForecastHistoryTable(); } } public Map<Integer, ForeCastWrap> populateMap () { Period qtrPeriod = periodMap.get(selectedForecastQ); Map<Integer, ForeCastWrap> fcMap = new Map<Integer, ForeCastWrap>(); for(Period p : periodMap.values()) { if(p.type == 'Month' && p.StartDate >= qtrPeriod.StartDate && p.StartDate <= qtrPeriod.EndDate) { fcMap.put(p.StartDate.month(), new ForeCastWrap(p.FullyQualifiedLabel.mid(0, 3), p.StartDate)); } } system.debug('__map_' + fcMap); return fcMap; } public void displayForecastTable(){ Period sltdQPeriod = periodMap.get(selectedForecastQ); Map<Integer, ForeCastWrap> revenueFCMap = populateMap(); Map<Integer, ForeCastWrap> enhanceFCMap = populateMap(); Map<Integer, ForeCastWrap> ServiceFCMap = populateMap(); list<ForecastingOwnerAdjustment> forecastLst = [select id,forecastingitemcategory,periodid, startdate,owneradjustedamount,owneradjustmentnote, forecastingtype.masterlabel from forecastingowneradjustment where ForecastOwnerId = :UserInfo.getUserId() and startdate >= :sltdQPeriod.StartDate and startdate <= :sltdQPeriod.EndDate ]; for(ForecastingOwnerAdjustment foa : forecastLst) { if( foa.forecastingtype.masterlabel == FORECAST_TYPE_REVENUE) { ForeCastWrap fcWrap = revenueFCMap.get(foa.startdate.month()); if(foa.forecastingitemcategory == FORECAST_CATEGORY_COMMIT){ fcWrap.commitID = foa.id; fcWrap.commitAmt = foa.owneradjustedamount; fcWrap.cComment = foa.owneradjustmentnote; } else if( foa.forecastingitemcategory == FORECAST_CATEGORY_BESTCASE) { fcWrap.bestCaseID = foa.id; fcWrap.bestCaseAmt = foa.owneradjustedamount; fcWrap.bcComment = foa.owneradjustmentnote; } } else if( foa.forecastingtype.masterlabel == FORECAST_TYPE_ENHANCE && foa.forecastingitemcategory == FORECAST_CATEGORY_COMMIT) { ForeCastWrap fcWrap = enhanceFCMap.get(foa.startdate.month()); fcWrap.commitID = foa.id; fcWrap.commitAmt = foa.owneradjustedamount; fcWrap.cComment = foa.owneradjustmentnote; } /* else if( foa.forecastingtype.masterlabel == FORECAST_TYPE_SPLIT && foa.forecastingitemcategory == FORECAST_CATEGORY_COMMIT) { ForeCastWrap fcWrap = ServiceFCMap.get(foa.startdate.month()); fcWrap.commitID = foa.id; fcWrap.commitAmt = foa.owneradjustedamount; fcWrap.cComment = foa.owneradjustmentnote; } */ } revenueFC = revenueFCMap.values(); enhanceFC = enhanceFCMap.values(); //ServiceFC = ServiceFCMap.values(); system.debug(revenueFC); system.debug(enhanceFC); //system.debug(ServiceFC); displayForecastHistoryTable(); } //Display Forecast History Data public void displayForecastHistoryTable(){ Period sltdQPeriod = periodMap.get(selectedForecastQ); revenueHFCs = [select Quater__c, Start_Date__c ,Comment__c, Commit_Amount__c, Best_Case_Amount__c, Forecast_Type__c,Submission_Date__c,createddate from Collaborative_Forecast_History__c where OwnerId = :UserInfo.getUserId() and Forecast_Type__c = :FORECAST_TYPE_REVENUE and Start_Date__c >= :sltdQPeriod.StartDate and Start_Date__c <= :sltdQPeriod.EndDate order by createddate desc ]; enhanceHFCs = [select Quater__c, Start_Date__c ,Comment__c, Commit_Amount__c, Best_Case_Amount__c, Forecast_Type__c,Submission_Date__c,createddate from Collaborative_Forecast_History__c where OwnerId = :UserInfo.getUserId() and Forecast_Type__c = :FORECAST_TYPE_ENHANCE and Start_Date__c >= :sltdQPeriod.StartDate and Start_Date__c <= :sltdQPeriod.EndDate order by createddate desc]; /* ServiceHFCs = [select Quater__c, Start_Date__c ,Comment__c, Commit_Amount__c, Best_Case_Amount__c, Forecast_Type__c,Submission_Date__c,createddate from Collaborative_Forecast_History__c where OwnerId = :UserInfo.getUserId() and Forecast_Type__c = :FORECAST_TYPE_SPLIT and Start_Date__c >= :sltdQPeriod.StartDate and Start_Date__c <= :sltdQPeriod.EndDate order by createddate desc]; */ } public void populateDataList(list<ForeCastWrap> lst, String fcType) { Period qtrPeriod = periodMap.get(selectedForecastQ); String Qlabel = 'Q'+qtrPeriod.Number + ' ' + qtrPeriod.StartDate.year(); for(ForeCastWrap fc : lst) { ForecastingOwnerAdjustment foa = new ForecastingOwnerAdjustment( id = fc.commitID, startdate=fc.stDate, owneradjustedamount = fc.commitAmt, //owneradjustmentnote = fc.cComment, forecastingitemcategory = FORECAST_CATEGORY_COMMIT, forecastingtypeid = getTypeID(fcType), forecastownerid = UserInfo.getUserId() ); upsertLst.add(foa); system.debug('kkk__' + foa); Collaborative_Forecast_History__c foaH = new Collaborative_Forecast_History__c ( Quater__c = Qlabel, Start_Date__c=fc.stDate, Submission_Date__c = System.today(), Commit_Amount__c = fc.commitAmt, Comment__c = fc.cComment, Forecast_Type__c = fcType ); if(fcType == FORECAST_TYPE_REVENUE) { foa = new ForecastingOwnerAdjustment(id = fc.bestCaseID, startdate=fc.stDate, owneradjustedamount = fc.bestCaseAmt, //owneradjustmentnote = fc.bcComment, forecastingitemcategory = FORECAST_CATEGORY_BESTCASE, forecastingtypeid = getTypeID(FORECAST_TYPE_REVENUE), forecastownerid = UserInfo.getUserId() ); upsertLst.add(foa); system.debug('kkk__' + foa); foaH.Best_Case_Amount__c = fc.bestCaseAmt; //foaH.Comment__c = fc.cComment; } upsertHstLst.add(foaH); } } public void submit() { upsertHstLst = new List<Collaborative_Forecast_History__c>(); upsertLst = new List<ForecastingOwnerAdjustment>(); populateDataList(revenueFC, FORECAST_TYPE_REVENUE); populateDataList(enhanceFC, FORECAST_TYPE_ENHANCE); // populateDataList(ServiceFC, FORECAST_TYPE_SPLIT); upsert upsertLst ID; Insert upsertHstLst; sendEmail(); displayForecastTable(); ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Forecast submitted to ' + getManagerEmail(UserInfo.getUserId()) + ' on ' + system.now() )); } public ID getTypeID(String label){ if(forecastingTypeMap == null) { forecastingTypeMap = new Map<String, Id>(); for (ForecastingType ft : [select id, masterlabel from Forecastingtype where isactive=true]) { forecastingTypeMap.put(ft.masterlabel, ft.id); } } return forecastingTypeMap.get(label); } public String getManagerEmail(Id userId) { User curUser; String managerEmail = null; // Get User Parent Id curUser = [select Id,name,UserRole.ForecastUserId,Email,UserRoleId,Userrole.name, UserRole.ParentRoleid from User where id = :UserInfo.getUserId()]; if( curUser.Userrole.name == 'Administrator') managerEmail = curUser.email; // Get Forecast Manager role id if(curUser.UserRole.ParentRoleid <> null) { Userrole managerRole = [select Id,ForecastUserId, ParentRoleid from userrole where id = :curUser.UserRole.ParentRoleid]; user mUser = [select id,name,email from user where id = :managerRole.ForecastUserId]; system.debug('Manager Name : ' + mUser.name); managerEmail = mUser.email; system.debug('Forecast Manager Email :' + managerEmail); } return managerEmail; } public void sendEmail() { Period qtrPeriod = periodMap.get(selectedForecastQ); String Qlabel = 'Q'+qtrPeriod.Number + ' ' + qtrPeriod.StartDate.year(); List<String> toAddresses = new List<String>(); String htmlBody; String MgrMail; // Check Manager Email to add MgrMail = getManagerEmail(UserInfo.getUserId()); if(MgrMail <> null){ toAddresses.add(MgrMail); } //Copy user mail toAddresses.add(UserInfo.getUserEmail()); //toAddresses.add('sudhirn@fortinet.com'); if(toAddresses.size() >0 && toAddresses != null){ Messaging.reserveSingleEmailCapacity(2); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(toAddresses); mail.setSenderDisplayName('Collaborative Forecasting Submission Alert'); mail.setSubject('Collaborative Forecasting Submission Alert :'+userinfo.getname()+' - '+Qlabel); htmlBody= userinfo.getname() + ' has submitted the below forecast for : ' + Qlabel + ' on ' + system.now() + '<br/><br/>'; htmlBody += '<head><style> table, th, td { border: 1px solid black; } </style> </head>'; //************************************Total Revenue Forecast****************************************** htmlBody+='<table class="list" border="1" cellpadding="0" cellspacing="0" width="100%">'; htmlBody+='<thead class=""><tr class=" headerRow">'; htmlBody+='<thead> <th class="dataCell" colspan="5" style="color:black">'+'Total Revenue Forecast'+'</th></thead></tr>'; htmlBody+='<tr> <th class=" headerRow" scope="col" colspan="1" bgcolor="#E5E5E5">Forecast Period</th>'; htmlBody+='<th class=" headerRow" scope="col" colspan="1" bgcolor="#E5E5E5">Commit (Core) Forecast</th>'; htmlBody+='<th class=" headerRow" scope="col" colspan="1" bgcolor="#E5E5E5">Best Case (Stretch) Forecast</th>'; htmlBody+='<th class=" headerRow" scope="col" colspan="1" bgcolor="#E5E5E5">Comment</th>'; htmlBody+='</tr></thead>'; htmlBody+='<tbody>'; for(ForeCastWrap rc: revenueFC){ htmlBody+='<tr class="dataRow even first">'; htmlBody+='<td class="dataCell" colspan="1" style="color:black" width="20%">'+rc.month+'</td>'; htmlBody+='<td class="dataCell" colspan="1" style="color:black" width="20%">'+rc.commitAmt+'</td>'; htmlBody+='<td class="dataCell" colspan="1" style="color:black" width="20%">'+rc.bestCaseAmt+'</td>'; htmlBody+='<td class="dataCell" colspan="1" style="color:black" width="50%">'+rc.cComment+'</td>'; htmlBody+='</tr>'; } htmlBody+='</tbody></table><br/><br/>'; //************************************Enhanced Tech Forecast****************************************** htmlBody+='<table class="list" border="1" cellpadding="0" cellspacing="0" width="100%">'; htmlBody+='<thead class=""><tr class=" headerRow">'; htmlBody+='<thead> <th class="dataCell" colspan="4" style="color:black">'+'Enhanced Tech Forecast'+'</th></thead></tr>'; htmlBody+='<tr> <th class=" headerRow" scope="col" colspan="1" bgcolor="#E5E5E5">Forecast Period</th>'; htmlBody+='<th class=" headerRow" scope="col" colspan="1" bgcolor="#E5E5E5">Commit (Core) Forecast</th>'; htmlBody+='<th class=" headerRow" scope="col" colspan="1" bgcolor="#E5E5E5">Comment</th>'; htmlBody+='</tr></thead>'; htmlBody+='<tbody>'; for(ForeCastWrap ec: enhanceFC){ htmlBody+='<tr class="dataRow even first">'; htmlBody+='<td class="dataCell" colspan="1" style="color:black" width="20%">'+ec.month+'</td>'; htmlBody+='<td class="dataCell" colspan="1" style="color:black" width="20%">'+ec.commitAmt+'</td>'; htmlBody+='<td class="dataCell" colspan="1" style="color:black" width="85%">'+ec.cComment+'</td>'; htmlBody+='</tr>'; } htmlBody+='</tbody></table><br/><br/>'; //************************************Service Forecast****************************************** /* htmlBody+='<table class="list" style="width:100%" border="1" cellpadding="0" cellspacing="0">'; htmlBody+='<thead class=""><tr class=" headerRow">'; htmlBody+='<thead> <th class="dataCell" colspan="3" style="color:black">'+'Enhanced Tech Forecast'+'</th></thead></tr>'; htmlBody+='<tr> <th class=" headerRow" scope="col" colspan="1">Commit (Core) Forecast</th>'; htmlBody+='<th class=" headerRow" scope="col" colspan="1">Comment</th>'; htmlBody+='</tr></thead>'; htmlBody+='<tbody>'; for(ForeCastWrap sc: ServiceFC){ htmlBody+='<tr class="dataRow even first">'; htmlBody+='<td class="dataCell" colspan="1" style="color:black">'+sc.commitAmt+'</td>'; htmlBody+='<td class="dataCell" colspan="1" style="color:black">'+sc.cComment+'</td>'; htmlBody+='</tr>'; } htmlBody+='</tbody></table><br/><br/>'; */ mail.setUseSignature(false); mail.setHtmlBody(htmlBody); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); //ApexPages.Message myMsg2 = new ApexPages.Message(ApexPages.Severity.Info,'Manager email address to send email: '+toAddresses); //ApexPages.addMessage(myMsg2); } } }Below is the test class which is having only 45 code coverage.
@isTest(seealldata=true) private class CForecastSubmitCnt_Test { static testMethod void myTest() { Period P = [Select id,EndDate, Number, PeriodLabel, QuarterLabel, StartDate, Type from Period where startdate = THIS_YEAR and Type = 'Month' limit 1]; ForecastingType F = [select Id,MasterLabel from ForecastingType where isactive = true and MasterLabel = 'Opportunity Revenue' limit 1]; ForecastingOwnerAdjustment foa = new ForecastingOwnerAdjustment( startdate= p.startdate, owneradjustedamount = 100, forecastingitemcategory = 'CommitForecast', forecastingtypeid = F.id, forecastownerid = UserInfo.getUserId() ); insert foa; ForecastingOwnerAdjustment foau = new ForecastingOwnerAdjustment( id = foa.id, owneradjustedamount = 100); update foau; test.StartTest(); PageReference pageRef = Page.EditOppLines; Test.setCurrentPage(pageRef); CForecastSubmitCnt controller = new CForecastSubmitCnt(); controller.selectedForecastQ = p.id; controller.populateMap(); test.StopTest(); } }
I was to call the wrapper below from class in test class please suggest me how to call.
public class ForeCastWrap {
public ForeCastWrap(String month, Date StDate){
this.month = month;
commitAmt = 0;
bestCaseAmt = 0;
this.StDate = StDate;
}
public String month {get; set;}
public String type;
public ID commitID {get; set;}
public Decimal commitAmt {get; set;}
public String cComment {get; set;}
public ID bestCaseID {get; set;}
public Decimal bestCaseAmt {get; set;}
public String bcComment {get; set;}
public Date StDate;
}
Thanks
Sudhir
you have to figure it out why the test method is not getting covered? there could be various reason. Just pointing it out.
1. periodMap.values() doesn't have any value. Put System debug and check if map has values or not
2. Inside for loop check wheather if condition does satisfy!
3. Does qtrPeriod has expected value as your accessing inside for loop if condition?
4. Once if condition satisfies, then only fcMap could have some values. Does your system.debug('__map_' + fcMap); returns values? if not then you have to build data inside the test method to satisfy all condition.
Try to put debug statement, for each condition. Then you will get to know the root cause. check if you have correct set of test data to satisfy your test method.
Thanks,
Sasmita
All Answers
You can call the inner wrapper class in following way inside you test class.
Initialize the object for the inner class
----------------------------------
CForecastSubmitCnt.ForeCastWrap forcastObj = new CForecastSubmitCnt.ForeCastWrap();
assign values to the fields defined inside the class.
--------------------------------------------------------------------------
forcastObj.type= "";
forcastObj.month ="Jan";
Hope, it would be useful.
Thanks,
Sasmita
I tried adding below code
CForecastSubmitCnt.ForeCastWrap forcastObj = new CForecastSubmitCnt.ForeCastWrap();
I am getting this error Error: Compile Error: Constructor not defined: [CForecastSubmitCnt.ForeCastWrap].<Constructor>() at line 38 column 49
Please suggest
Thanks
Sudhir
CForecastSubmitCnt.ForeCastWrap forcastObj = new CForecastSubmitCnt.ForeCastWrap("Jan", "03/05/2017");
you have to pass all the parameters as you have defined in your constructer.
CForecastSubmitCnt.ForeCastWrap forcastObj = new CForecastSubmitCnt.ForeCastWrap();
This is for default constructor.
I have another problem need your suggestio on below error when I run the test class
Class.CForecastSubmitCnt.populateMap: line 75, column 1
Class.CForecastSubmitCnt_Test.myTest: line 45, column 1
Below code is not getting covered
public Map<Integer, ForeCastWrap> populateMap () {
Period qtrPeriod = periodMap.get(selectedForecastQ);
Map<Integer, ForeCastWrap> fcMap = new Map<Integer, ForeCastWrap>();
for(Period p : periodMap.values()) {
if(p.type == 'Month' && p.StartDate >= qtrPeriod.StartDate && p.StartDate <= qtrPeriod.EndDate) {
fcMap.put(p.StartDate.month(), new ForeCastWrap(p.FullyQualifiedLabel.mid(0, 3), p.StartDate));
}
}
system.debug('__map_' + fcMap);
return fcMap;
}
Thanks
Sudhir
you have to figure it out why the test method is not getting covered? there could be various reason. Just pointing it out.
1. periodMap.values() doesn't have any value. Put System debug and check if map has values or not
2. Inside for loop check wheather if condition does satisfy!
3. Does qtrPeriod has expected value as your accessing inside for loop if condition?
4. Once if condition satisfies, then only fcMap could have some values. Does your system.debug('__map_' + fcMap); returns values? if not then you have to build data inside the test method to satisfy all condition.
Try to put debug statement, for each condition. Then you will get to know the root cause. check if you have correct set of test data to satisfy your test method.
Thanks,
Sasmita