[SalesForce] Constructor Not Defined Error when writing a test class

I read through other Constructor Error posts but I seem to be missing something, I am writing a test class net new for a class that edits an event that was built by another developer. Previously, I had been working on a test class for a class that creates an event.

The edit class uses a standard controller and it's throwing off the code I am trying to reuse from the create test class.

I have this working in a method:

@isTest static void doSaveFail(){

    // Get the new Account Id we created and put it into the vf page.
    Account acc = [Select Id FROM Account];

    // Grab the user 
    User u1 = [Select Id, LastName FROM User WHERE username = 'apextestuser@npd.com'];

    ApexPages.currentPage().getParameters().put('accId',acc.Id);
    SL_CallReport callReport = new SL_CallReport();

    callReport.event.EndDateTime = System.now() - 1;

    callReport.doSave();

}

I tried to edit this method to do the same but with the standard controller I get the error Constructor not defined.

@isTest static void doSaveFail(){

    // Get the new Account Id we vf page.
    Account acc = [Select Id FROM Account];

    // Grab the user
    User u1 = [Select Id, LastName FROM User WHERE username = 'apextestuser@npd.com'];

    ApexPages.StandardController sc = new ApexPages.StandardController();
    SL_CallReportEditController callReport = new SL_CallReportEditController(sc);
    ApexPages.currentPage().getParameters().put('Id',acc.Id);

    callReport.event.EndDateTime = System.now() - 1;

    callReport.doSave();

}

Class:

 /** 
    * @methodName  Constructor
    * @date 5/18/2016
    * @description : Constructor
**/
public SL_CallReportEditController(ApexPages.StandardController stdController) 
{
    // logic
}

Error: Result: [COMPILE FAILED]: (classes/NPD_Test_CallReport_Edit.cls) Constructor not defined: [ApexPages.StandardController].() (Line: 161, Column: 43)

Best Answer

The ApexPages.StandardController class does not have an empty constructor. The only defined constructor accepts one SObject parameter:

public StandardController(SObject controllerSObject)

So in your testMethod you must do:

ApexPages.StandardController controller = new ApexPages.StandardController(acc);
MyExtension extension = new MyExtension(controller);

A note that in the constructor logic (I removed because it seems irrelevant here) you do not need to ever touch ApexPages.currentPage().getParameters(). Just use the ApexPages.StandardController.getId() method:

Id controllerId = stdController.getId();

Your SL_CallReportEditController class does not have a constructor defined that accepts an instance of ApexPages.StandardController as its only parameter. By default, only the empty constructor is defined, and this definition is lost if you define one yourself.

Related Topic