function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Som_11Som_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();        
    }    
}
Best Answer chosen by Som_11
AnudeepAnudeep (Salesforce Developers) 
Hi Somesh, 

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

AnudeepAnudeep (Salesforce Developers) 
Hi Somesh - Issue is at this line - 
CreditMeterGraphic  testCreditMeterGraphic = new CreditMeterGraphic();

Your code in test class should be
 
Account dealerAccnt = TestDataFactory.createDealerAccount();

ApexPages.StandardController sc = new ApexPages.StandardController(dealerAccnt); 
CreditMeterGraphic cM = new CreditMeterGraphic(sc);

Let me know if this helps

Thanks, 
Anudeep
Som_11Som_11
Hi Anudeep, Thanks for response. I kept as u said. Still same issue I came.User-added image
Santosh Kumar 348Santosh Kumar 348
Try Below Code, replace you code in Test.StartTest and Test.StopTest.

Replace "yourPageName" on Line 2 of below snippet with the name of your page and it will work.
 
Test.StartTest(); 
       
PageReference testPage = Page.yourPageName; 
Test.setCurrentPage(testPage);
testPage.getParameters().put('Id', String.valueOf(dealerAccnt.Id));
       
ApexPages.StandardController sc = new ApexPages.StandardController(dealerAccnt);     
CreditMeterGraphic ext = new CreditMeterGraphic(sc);         
        
System.debug(ext.getData()); 
       
Test.StopTest();

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
AnudeepAnudeep (Salesforce Developers) 
Hi Somesh, 

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
This was selected as the best answer
Som_11Som_11
Yepp, Thanks Anudeep, It helped..