2

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 = '[email protected]'];

    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 = '[email protected]'];

    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)

6
  • Do you actually have two separate classes? In your first example it's SL_CallReport, in the second it's SL_CallReportEditController.
    – Adrian Larson
    Jul 18, 2016 at 19:37
  • Yes, one for create one for edit. I'm trying to paste the class it does have a controller but it looks weird and unformulated for some reason. Jul 18, 2016 at 19:39
  • Always, always, always include the verbatim error message if you have one. Makes it much easier to assess what's going on. :)
    – Adrian Larson
    Jul 18, 2016 at 19:58
  • 1
    N.B. testmethods should mock the user rather than relying on a hardcoded username as you have here.
    – cropredy
    Jul 18, 2016 at 23:01
  • @cropredy I created a user in the testsetup, Then to reference that user in the methods I couldn't figure out a way to get to that specific user, is there a better way? Jul 20, 2016 at 14:48

1 Answer 1

5

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.

2
  • can you explain or better, link to an explanation on why getId is better than ApexPages.currentPage().getParameters()? I see it used in a lot of places in my org and I wonder if I should start to change it over in those places or not. Jul 20, 2016 at 14:55
  • @Dan Sure, my reasoning is just that being more programmatic is cleaner. Also, you're doing more work than you need to in your test. The id is already accessible without ever touching the parameters. I only mess with them (the parameters) when I have to. It's not a blocker issue if you don't do it, more something you can tighten up if you want your code to be a bit leaner.
    – Adrian Larson
    Jul 20, 2016 at 14:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .