You need to sign in to do that
Don't have an account?
james1986
trouble passing variable from test class to controller
hi, I'm hoping someone can point me in the right direction.
I have a visual force page that passes 2 parameters to an APEX class. It seems to work fine, but before I deploy it need to develop a test class.
I am stuck at 31% code coverage. It looks like my parameters are NOT being passed and so I can't get past line 24 (below). I tried assigning values to "firstselect" and "lastselect" (see lines 95 and 96) but I get the following error:
Error: Compile Error: Method does not exist or incorrect signature: [updatepledges].firstselect(String) at line 95 column 8
I'm hoping someone could have a look at my test class and let me know how I should go about passing these parameters. Thanks in advance
public class updatepledges {
//added an instance varaible for the standard controller
private ApexPages.StandardController controller {get; set;}
// add the instance for the variables being passed by id on the url
private Deposit__c depbatch {get;set;}
// set the id of the record that is created -- ONLY USED BY THE TEST CLASS
public ID newRecordId {get;set;}
public String firstselect {get; set;}
public String lastselect {get; set;}
public String batch_paytype {get; set;}
public updatepledges(ApexPages.StandardController controller)
{
//initialize the stanrdard controller
this.controller = controller;
// load the current record
depbatch = (Deposit__c)controller.getRecord();
}
public PageReference runpledges()
{
// THE NEXT LINE IS #24. I CANT GET ANY COVERAGE BEYOND THIS POINT, I THINK BECAUSE IM NOT PROPERLY PASSING THE PARAMETER
integer firstdate = integer.valueof(firstselect);
integer lastdate = integer.valueof(lastselect);
depbatch = [select PaymentType__c from Deposit__c where Id = :depbatch.Id];
batch_paytype = String.Valueof(depbatch.get(Deposit__c.PaymentType__c));
List<Donation__c> donations = new List<Donation__c>();
Map<ID,Donation__c> mgDonationMap = new Map<ID,Donation__c>();
for (Monthly_Gift__c mg: [SELECT Id, Account__c, Contact__c, PaymentType__c FROM Monthly_Gift__c WHERE PaymentType__c = :batch_paytype AND Day_of_Month__c >= :firstdate AND Day_of_Month__c <= :lastdate])
{
//Create a donation and populate it. then add it to the list
Donation__c d = new Donation__c();
d.Account__c = mg.Account__c;
d.Contact__c = mg.Contact__c;
d.Payment_Type__c = mg.PaymentType__c;
d.Deposit_Batch__c = depbatch.Id;
donations.add(d);
//add the id of the monthly donation and the new dontation to a map to be used for the allocations
mgDonationMap.put(mg.id,d);
}
insert donations;
List<Allocation__c> allocations = new List<Allocation__c>();
for(Monthly_Gift_Allocations__C mga: [SELECT Amount__c, Monthly_Gift__c, Program__c, Stream__c FROM Monthly_Gift_Allocations__c WHERE Monthly_Gift__r.Day_of_Month__c >= :firstdate AND Monthly_Gift__r.Day_of_Month__c <= :lastdate AND Monthly_Gift__r.PaymentType__c = :depbatch.PaymentType__c])
{
Allocation__c a = new Allocation__c();
a.Amount__c = mga.Amount__c;
//This is the line that is important.
//associate the allocation to the correct Donation
//by finding the monthly gift in the map which the monthly gift allocation is tied to
a.Transaction__c = mgDonationMap.get(mga.Monthly_Gift__c).Id;
a.Program__c = mga.Program__c;
a.Stream__c = mga.Stream__c;
allocations.add(a);
}
insert allocations;
return new PageReference('/a0a/o');
}
static testMethod void createbatches()
{
list<Deposit__c> batch = new List<Deposit__c>{};
for(Integer i = 0; i < 5; i++)
{
Deposit__c b = new Deposit__c(DepositDate__c = date.today(), Deposit_Items__c = 2, DepositTotal__c = 77.5, PaymentType__c = 'EFT');
batch.add(b);
}
for(Integer i = 0; i < 5; i++)
{
Deposit__c b = new Deposit__c(DepositDate__c = date.today(), Deposit_Items__c = 2, DepositTotal__c = 77.5, PaymentType__c = 'Canada Helps');
batch.add(b);
}
test.startTest();
insert batch;
test.stopTest();
}
static testMethod void testcontroller()
{
Deposit__c mybatch = [SELECT Name, DepositDate__c, Deposit_Items__c, DepositTotal__c, PaymentType__c FROM Deposit__c LIMIT 1];
PageReference pageRef = Page.pledges;
Test.setCurrentPage(pageref);
updatepledges controller = new updatepledges(new ApexPages.StandardController(mybatch));
String nextPage = controller.runpledges().getUrl();
System.assertEquals('/apex/failure?error=noParam', nextPage);
ApexPages.currentPage().getParameters().put('ID', mybatch.ID);
//added an instance varaible for the standard controller
private ApexPages.StandardController controller {get; set;}
// add the instance for the variables being passed by id on the url
private Deposit__c depbatch {get;set;}
// set the id of the record that is created -- ONLY USED BY THE TEST CLASS
public ID newRecordId {get;set;}
public String firstselect {get; set;}
public String lastselect {get; set;}
public String batch_paytype {get; set;}
public updatepledges(ApexPages.StandardController controller)
{
//initialize the stanrdard controller
this.controller = controller;
// load the current record
depbatch = (Deposit__c)controller.getRecord();
}
public PageReference runpledges()
{
// THE NEXT LINE IS #24. I CANT GET ANY COVERAGE BEYOND THIS POINT, I THINK BECAUSE IM NOT PROPERLY PASSING THE PARAMETER
integer firstdate = integer.valueof(firstselect);
integer lastdate = integer.valueof(lastselect);
depbatch = [select PaymentType__c from Deposit__c where Id = :depbatch.Id];
batch_paytype = String.Valueof(depbatch.get(Deposit__c.PaymentType__c));
List<Donation__c> donations = new List<Donation__c>();
Map<ID,Donation__c> mgDonationMap = new Map<ID,Donation__c>();
for (Monthly_Gift__c mg: [SELECT Id, Account__c, Contact__c, PaymentType__c FROM Monthly_Gift__c WHERE PaymentType__c = :batch_paytype AND Day_of_Month__c >= :firstdate AND Day_of_Month__c <= :lastdate])
{
//Create a donation and populate it. then add it to the list
Donation__c d = new Donation__c();
d.Account__c = mg.Account__c;
d.Contact__c = mg.Contact__c;
d.Payment_Type__c = mg.PaymentType__c;
d.Deposit_Batch__c = depbatch.Id;
donations.add(d);
//add the id of the monthly donation and the new dontation to a map to be used for the allocations
mgDonationMap.put(mg.id,d);
}
insert donations;
List<Allocation__c> allocations = new List<Allocation__c>();
for(Monthly_Gift_Allocations__C mga: [SELECT Amount__c, Monthly_Gift__c, Program__c, Stream__c FROM Monthly_Gift_Allocations__c WHERE Monthly_Gift__r.Day_of_Month__c >= :firstdate AND Monthly_Gift__r.Day_of_Month__c <= :lastdate AND Monthly_Gift__r.PaymentType__c = :depbatch.PaymentType__c])
{
Allocation__c a = new Allocation__c();
a.Amount__c = mga.Amount__c;
//This is the line that is important.
//associate the allocation to the correct Donation
//by finding the monthly gift in the map which the monthly gift allocation is tied to
a.Transaction__c = mgDonationMap.get(mga.Monthly_Gift__c).Id;
a.Program__c = mga.Program__c;
a.Stream__c = mga.Stream__c;
allocations.add(a);
}
insert allocations;
return new PageReference('/a0a/o');
}
static testMethod void createbatches()
{
list<Deposit__c> batch = new List<Deposit__c>{};
for(Integer i = 0; i < 5; i++)
{
Deposit__c b = new Deposit__c(DepositDate__c = date.today(), Deposit_Items__c = 2, DepositTotal__c = 77.5, PaymentType__c = 'EFT');
batch.add(b);
}
for(Integer i = 0; i < 5; i++)
{
Deposit__c b = new Deposit__c(DepositDate__c = date.today(), Deposit_Items__c = 2, DepositTotal__c = 77.5, PaymentType__c = 'Canada Helps');
batch.add(b);
}
test.startTest();
insert batch;
test.stopTest();
}
static testMethod void testcontroller()
{
Deposit__c mybatch = [SELECT Name, DepositDate__c, Deposit_Items__c, DepositTotal__c, PaymentType__c FROM Deposit__c LIMIT 1];
PageReference pageRef = Page.pledges;
Test.setCurrentPage(pageref);
updatepledges controller = new updatepledges(new ApexPages.StandardController(mybatch));
String nextPage = controller.runpledges().getUrl();
System.assertEquals('/apex/failure?error=noParam', nextPage);
ApexPages.currentPage().getParameters().put('ID', mybatch.ID);
// HERE ARE LINES 95 & 96. I HAVE COMMENTED THEM OUT SO I CAN SAVE MY CLASS- BUT AS A RESULT CANNOT PROCEED PAST LINE 24
// controller.firstselect('1');
// controller.lastselect('15');
controller = new updatepledges(new ApexPages.StandardController(mybatch));
nextPage = controller.runpledges().getUrl();
}
}
// controller.firstselect('1');
// controller.lastselect('15');
controller = new updatepledges(new ApexPages.StandardController(mybatch));
nextPage = controller.runpledges().getUrl();
}
}
Hey it should be (in the test class - your commented lines)
controller.firstselect = '1';
controller.lastselect= '15';
All Answers
Hey it should be (in the test class - your commented lines)
controller.firstselect = '1';
controller.lastselect= '15';
100% code coverage. Awesome - thanks Ritesh.
I did have to make one small other change and move those 2 lines up a few lines so that the parameters where assigned when my code was called. Working test class reads like this:
public static testMethod void testcontroller()
{
Deposit__c mybatch = [SELECT Name, DepositDate__c, Deposit_Items__c, DepositTotal__c, PaymentType__c FROM Deposit__c LIMIT 1];
PageReference pageRef = Page.pledges;
Test.setCurrentPage(pageref);
updatepledges controller = new updatepledges(new ApexPages.StandardController(mybatch));
controller.firstselect ='1';
controller.lastselect ='15';
String nextPage = controller.runpledges().getUrl();
System.assertEquals('/apex/failure?error=noParam', nextPage);
ApexPages.currentPage().getParameters().put('ID', mybatch.ID);
controller = new updatepledges(new ApexPages.StandardController(mybatch));
nextPage = controller.runpledges().getUrl();
}
public static JSON2Apex parse(String json) {
return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
}
how can i use this in test class
public with sharing class allPatntsRcrdsCtrlr {
public PageReference Clear() {
searchVal=null;
return null;
}
public list<oAuthRecords> lstopwrpr{get;set;}
public list<oAuthRecords> lstSearchPb { get; set; }
public List<patient__c> patlist{ get; set;}
public String strBody{ get; set; }
public string idv{get;set;}
public string strquery;
public String labeling;
public String searchVal { get; set; }
public String searchFields{get; set;}
public Boolean show{ get; set; }
public Boolean lstrecdstb1{ get; set; }
public Boolean queryflds{ get; set; }
public boolean pdfrendered{get;set;}
public Boolean showmessage { get; set; }
public Boolean wordRendered { get; set; }
public Boolean xlRendered { get; set; }
public static JSON2Apex parse(String json) {
return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
}
public class JSON2Apex {
public String access_token;
}
public allPatntsRcrdsCtrlr()
{
lstSearchPb = new list<oAuthRecords>();
patlist= new list<patient__c>();
lstrecdstb1=true;
queryflds=false;
show = false;
pdfrendered=true;
wordRendered=true;
xlRendered=true;
}
/* public string gettoken(){
string tokenval;
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
req.setEndpoint('https://dev2-dsm.testwd.com/SecurityTokenService/token');
req.setMethod('POST');
String requestBody = 'grant_type=client_credentials&client_id=abc1123&client_secret=xxx';
req.setBody(requestBody);
Http http = new Http();
HTTPResponse res = http.send(req);
String response = res.getBody();
system.debug('The response value is****************TOKEN VALUE**********************'+response);
JSON2Apex obj =parse(response);
system.debug('token recieved'+obj);
return obj.access_token;
}*/
public PageReference patinfo() {
// String token =gettoken();
//'3YrwjTS_bUQyulqlr0DZMDY7iijtwv3F40dRlavu_RqBoActDIkdr3wKQiDtofZYyamuOxsLBy07qhufKVx5TkKN6bSKuWr6UfV9ama5sbSTz_4jo3XGPaNd02gg79YQ6o0pXLxg7Jp5lm1n1FB6iWDDrUHxP2DA4enB97wYIzAXjR1U0zEkk6pB89JCUe0ULYU7rary2raA6CCPpVAgNRPFCo5AsxRwDJjEY1CvG-SRcZUG';
HttpRequest req1 = new HttpRequest();
req1.setHeader('Content-Length', '0');
req1.setHeader('Content-Type', 'application/json');
//req1.setHeader('Authorization', 'Bearer ' + token);
req1.setEndpoint('https://dev1-dsm.testwd.com/CRMPOC/PatientProfileService.svc/GetPatientRecord');
req1.setMethod('POST');
Http http1 = new Http();
HTTPResponse res1 = http1.send(req1);
String response1 = res1.getBody();system.debug('The response value is**************************************'+response1);
lstopwrpr=(list<oAuthRecords>)JSON.deserialize(response1,list<oAuthRecords>.class);
system.debug('***************'+lstopwrpr);
lstSearchPb = lstopwrpr;
/* for(oAuthRecords objPRWrp:lstopwrpr)
{
patient__c objPant = new patient__c();
objPant.BlueStarID__c = objPRWrp.BlueStarID;
objPant.Hub_Code__c= objPRWrp.HubCode;
objPant.Name = objPRWrp.PatientName;
objPant.Patient_First_Name__c= objPRWrp.PatientFirstName;
objPant.Patient_Id__c= objPRWrp.PatientID;
objPant.Patient_Last_Name__c= objPRWrp.PatientLastName;
objPant.ProviderName__c= objPRWrp.ProviderName;
objPant.ExternalID__c= objPRWrp.ExternalID;
objPant.SRType__c= objPRWrp.SRType;
objPant.SampleCode__c= objPRWrp.SampleCode;
objPant.ServiceRequestID__c=objPRWrp.ServiceRequestID;
objPant.UserID__c= objPRWrp.UserID;
patlist.add(objPant);
}
system.debug('patlist-----'+patlist);*/
return null;
}
public PageReference ExpWord()
{
System.currentPagereference().getParameters().put('isWord','true');
pdfrendered=false;
wordRendered=false;
xlRendered=false;
return null;
}
public void PatientData()
{
for(oAuthRecords objPRWrp:lstopwrpr)
{
patient__c objPant = new patient__c();
objPant.BlueStarID__c = objPRWrp.BlueStarID;
system.debug('objPant.BlueStarID__c---------------------------'+objPant.BlueStarID__c);
objPant.Name = objPRWrp.PatientName;
objPant.Patient_First_Name__c= objPRWrp.PatientFirstName;
objPant.Patient_Id__c= objPRWrp.PatientID;
objPant.Patient_Last_Name__c= objPRWrp.PatientLastName;
objPant.ExternalID__c= objPRWrp.ExternalID ;
objPant.ProviderName__c= objPRWrp.ProviderName ;
objPant.SRType__c= objPRWrp.SRType ;
objPant.SampleCode__c= objPRWrp.SampleCode ;
objPant.ServiceRequestID__c= objPRWrp.ServiceRequestID ;
objPant.UserID__c= objPRWrp.UserID ;
patlist.add(objPant);
system.debug('patlist----------------------------'+patlist);
}
}
public PageReference retrieve() {
queryflds=true;
return null;
}
public String getlabeling () {
return labeling ;
}
public void setString(String labeling ) {
this.labeling = labeling ;
}
public String getsearchFields() {
return searchFields;
}
public void setsearchFields(String searchFields ) {
this.searchFields = searchFields ;
}
//Search Button Functionality
public PageReference doSearch() {
lstrecdstb1=false;
show = true;
lstSearchPb=new list<oAuthRecords>();
if(searchVal=='')
{
show=false;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Enter Value'));
}
try{
for(oAuthRecords dop:lstopwrpr)
{
labeling='ProviderName';
if((dop.ProviderName==searchVal) &&(searchFields=='ProviderName'))
{
labeling=searchFields;
system.debug('is it checking'+dop.ProviderName);
system.debug('entered value'+searchVal);
lstSearchPb.add(dop);
}
else if((dop.BlueStarID==searchVal) &&(searchFields=='BlueStarID'))
{
labeling=searchFields;
system.debug('is it checking'+dop.BlueStarID);
system.debug('entered value'+searchVal);
lstSearchPb.add(dop);
}
/*else if((dop.ExternalID.containsIgnoreCase(searchVal)) &&(searchFields=='ExternalID'))
{
labeling=searchFields;
system.debug('is it checking'+dop.ExternalID);
system.debug('entered value'+searchVal);
lstSearchPb.add(dop);
}*/
else if((dop.PatientFirstName==searchVal) &&(searchFields=='PatientFirstName'))
{
labeling=searchFields;
system.debug('is it checking'+dop.PatientFirstName);
system.debug('entered value'+searchVal);
lstSearchPb.add(dop);
}
else if((dop.PatientLastName==searchVal) &&(searchFields=='PatientLastName'))
{
labeling=searchFields;
system.debug('is it checking'+dop.PatientLastName);
system.debug('entered value'+searchVal);
lstSearchPb.add(dop);
}
else if((dop.SRType==searchVal) &&(searchFields=='SRType'))
{
labeling=searchFields;
system.debug('is it checking'+dop.SRType);
system.debug('entered value'+searchVal);
lstSearchPb.add(dop);
}
else if((dop.SampleCode==searchVal) &&(searchFields=='SampleCode'))
{
labeling=searchFields;
system.debug('is it checking'+dop.SampleCode);
system.debug('entered value'+searchVal);
lstSearchPb.add(dop);
system.debug('lst added'+lstSearchPb);
}
// else if((dop.ServiceRequestID==Decimal.valueOf(searchVal) ) &&(searchFields=='ServiceRequestID'))
// {
// format.parse(searchVal)).doubleValue()
//Decimal.valueOf(searchVal) Number number = format.parse("1,234"); double d = number.doubleValue();
/* labeling=searchFields;
system.debug('is it checking'+dop.ServiceRequestID);
system.debug('entered value'+searchVal);
lstSearchPb.add(dop);
}
else if((dop.UserID==Decimal.valueOf(searchVal)) &&(searchFields=='UserID'))
{
labeling=searchFields;
system.debug('is it checking'+dop.UserID);
system.debug('entered value'+searchVal);
lstSearchPb.add(dop);
}*/
else{
show=true;
}
}
}
catch(DmlException ex){
ApexPages.addMessages(ex);
}
system.debug(' out of loop lst added'+lstSearchPb);
return null;
}
public PageReference ExpXL()
{
PageReference exdownload = null;
exdownload = Page.ExcelDownload1;
exdownload.setRedirect(false);
PatientData();
return exdownload ;
}
public PageReference ExpPDF()
{
System.currentPagereference().getParameters().put('isPdf','true');
pdfrendered=false;
return null;
}
// Wrapper class variables declaration
public class oAuthRecords
{
public String BlueStarID { get; set; }
public String HubCode{ get; set; }
public String PatientName { get; set; }
public String PatientFirstName{ get; set; }
public String PatientID{ get; set; }
public string PatientLastName{ get; set; }
public String ProviderName{ get; set; }
public String SRType{ get; set; }
public String SampleCode{ get; set; }
public String ExternalID { get; set; }
public Decimal ServiceRequestID{ get; set; }
public Integer UserID { get; set; }
}
}