You need to sign in to do that
Don't have an account?
Som_11
How to write test class for VF Page Controller
Hi all, I getting Constructor not defined: [CreditMeterGraphic].<Constructor>() error for below controller class. Can anyone help on this.
Controller class is :
public with sharing class CreditMeterGraphic {
public String currentRecordId {get;set;}
public Account acc{get;set;}
public CreditMeterGraphic(ApexPages.StandardController controller){
currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
if (Schema.sObjectType.Account.fields.Credit_Limit__c.isAccessible()) {
acc = [Select Credit_Limit__c From Account Where Id =: currentRecordId];
}
}
public List<gaugeData> getData(){
double npsscore = 0;
Account creditusage = [Select Id, Name, Credit_Usage__c, Credit_Limit__c From Account Where Id =: currentRecordId];
Account creditlimit = [Select Id, Name, Credit_Limit__c From Account LIMIT 1];
List<gaugeData> data = new List<gaugeData>();
data.add(new gaugeData('Credit', creditusage.Credit_Usage__c));
data.add(new gaugeData('Credit', creditusage.Credit_Limit__c));
return data;
}
public class GaugeData{
public String name{get; set;}
public decimal credit{get; set;}
public gaugeData(String name, decimal npsScore){
this.name = name;
this.credit = npsScore;
}
}
}
Test Class is :
@isTest
public class TestCreditMeterGraphic {
@isTest Public static void creditMeterGraphicMethod(){
//Getting Dealer Account
Account dealerAccnt = TestDataFactory.createDealerAccount();
Test.StartTest();
ApexPages.currentPage().getParameters().put('id', String.valueOf(dealerAccnt.Id));
CreditMeterGraphic testCreditMeterGraphic = new CreditMeterGraphic();
testCreditMeterGraphic.getData();
Test.StopTest();
}
}
Controller class is :
public with sharing class CreditMeterGraphic {
public String currentRecordId {get;set;}
public Account acc{get;set;}
public CreditMeterGraphic(ApexPages.StandardController controller){
currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
if (Schema.sObjectType.Account.fields.Credit_Limit__c.isAccessible()) {
acc = [Select Credit_Limit__c From Account Where Id =: currentRecordId];
}
}
public List<gaugeData> getData(){
double npsscore = 0;
Account creditusage = [Select Id, Name, Credit_Usage__c, Credit_Limit__c From Account Where Id =: currentRecordId];
Account creditlimit = [Select Id, Name, Credit_Limit__c From Account LIMIT 1];
List<gaugeData> data = new List<gaugeData>();
data.add(new gaugeData('Credit', creditusage.Credit_Usage__c));
data.add(new gaugeData('Credit', creditusage.Credit_Limit__c));
return data;
}
public class GaugeData{
public String name{get; set;}
public decimal credit{get; set;}
public gaugeData(String name, decimal npsScore){
this.name = name;
this.credit = npsScore;
}
}
}
Test Class is :
@isTest
public class TestCreditMeterGraphic {
@isTest Public static void creditMeterGraphicMethod(){
//Getting Dealer Account
Account dealerAccnt = TestDataFactory.createDealerAccount();
Test.StartTest();
ApexPages.currentPage().getParameters().put('id', String.valueOf(dealerAccnt.Id));
CreditMeterGraphic testCreditMeterGraphic = new CreditMeterGraphic();
testCreditMeterGraphic.getData();
Test.StopTest();
}
}
In the screenshot, you are passing the id to the controller (dealerAcnt.Id). Just pass the instance variable(dealAcnt)
See the following examples
https://salesforce.stackexchange.com/questions/131920/constructor-not-defined-error-when-writing-a-test-class
https://developer.salesforce.com/forums/?id=906F00000008xsOIAQ
Let me know if it helps
Anudeep
All Answers
Your code in test class should be
Let me know if this helps
Thanks,
Anudeep
Replace "yourPageName" on Line 2 of below snippet with the name of your page and it will work.
As this is an extension class and you are trying to refer the default constructor which is not present in your class.
For extenstion class you need to use "ApexPages.StandardController sc = new ApexPages.StandardController(dealerAccnt);" and then pass this instance to constructor of your class "CreditMeterGraphic ext = new CreditMeterGraphic(sc);"
If it helped then can you please mark it as the best answer so that it can be used by others in the future.
Regards,
Santosh Kumar
In the screenshot, you are passing the id to the controller (dealerAcnt.Id). Just pass the instance variable(dealAcnt)
See the following examples
https://salesforce.stackexchange.com/questions/131920/constructor-not-defined-error-when-writing-a-test-class
https://developer.salesforce.com/forums/?id=906F00000008xsOIAQ
Let me know if it helps
Anudeep