1

I am trying to instantiate a new controller. But every syntax I have tried shows an error.

Here is my code:

Class:

public  class MyNewCaseController {
    
    public string filterId {get; set;}
    
    public MyNewCaseController(ApexPages.StandardController  ctrl){
        string filter = ApexPages.currentPage().getParameters().get('filterId');
        if (filter != null)
            filterId = filter;
    }
}

Test:

@isTest
public without sharing class MyNewCaseListControllerTest {
    
    @isTest
    public static void testMyNewCaseController (){
        // Instantiate a new controller with all parameters in the page
        MyNewCaseListController controller = new MyNewCaseListController.MyNewCaseListController(ApexPages.StandardController ctrl);
 
    // Do something
    }
}

I get the error “Invalid type: MyNewCaseListController.MyNewCaseListController”.

If I try just

MyNewCaseListController controller = new MyNewCaseListController();

I get Constructor not defined: [MyNewCaseListController].()

If I try

MyNewCaseListController.MyNewCaseListController();

I get “Invalid type: MyNewCaseListController.MyNewCaseListController”

If I try

MyNewCaseListController controller = new MyNewCaseListController.MyNewCaseListController(ApexPages.ctrl);

I get “Invalid type: MyNewCaseListController.MyNewCaseListController”

What am I doing wrong?

6
  • Adding this comment in each question: You should provide your Visualforce, or at least your requirement. – In this question your controller is called "MyNewCaseListController", this is where the confusion starts. Dec 10, 2022 at 0:10
  • @ TheArchitecta The problem is I don't know what the page is. I was just given three classes to write test code for. I wasn't sure how connected they were. Dec 10, 2022 at 0:26
  • started Private discussion, lets go there. Dec 10, 2022 at 0:27
  • Dude I must be really dense I don't see anything for a private discussion on Stack Exchange. Dec 10, 2022 at 0:48

1 Answer 1

1

The (datatype name, datatype name, ...) syntax is for defining a function's parameter list. To call it, you simply pass in the parameters. As a simple example:

class Counter {
  Integer value;
  Counter(Integer initialVal) {
    value = initialVal;
  }
  void increment() {
    value++;
  }
}

You would construct it as:

Integer initialValue = 5;
Counter theCounter = new Counter(initialValue);

If you replace Integer in our example with an ApexPages.StandardController, you can see it works almost the exact same way:

class Controller {
  ApexPages.StandardController value;
  Controller(ApexPages.StandardController initialVal) {
    value = initialVal;
  }
  ApexPages.StandardController redirect() {
    value.setRedirect(true);
    return value;
  }
}

And we'd call it in very much the same manner:

Account record = new Account();
ApexPages.StandardController standardController = new ApexPages.StandardController(record);
Controller extensionController = new Controller(standardController);

As you can see, we don't call the constructor directly; the new operator implies that the constructor will be called, and the specific constructor that will be called will depend on the order, number, and data types of the parameters provided.

Note that all classes get an "implicit" zero-argument constructor by default, unless we define our own. For example:

class A {
}
A value = new A(); // Okay!

class B {
  B(Integer value) {
  }
}
B value1 = new B(5); // Okay!
B value2 = new B(); // No zero-arg constructor available!

class C {
  C() {
  }
  C(Integer value) {
  }
}
C value1 = new C(5); // Okay!
C value2 = new C(); // Okay!
C value3 = new C(5, 10); // No such constructor.
C value4 = new C('Hello World'); // no such constructor.

As such, you always need to pay attention to the data types and number of parameters.

4
  • Thanks @ sfdcfox I guess where I am confused is what I need to pass to the ApexStandardController. The filterId being passed in the class is when a user clicks on a case. I have been able to get the test class to insert a case (case = ca) and pass that case to MyNewCaseController controller = new MyNewCaseController(new ApexPages.StandardController(ca)); The problem is is seems to be passing a null value. Dec 9, 2022 at 2:37
  • But if try passing ca.Id or creating a variable of string filterId = ca.Id; it doesn't work. I tried using ApexPages.currentPage().getParameters().put('filterId',ca.id); but that didn't work either. Dec 9, 2022 at 2:55
  • @JonathanCrow the latter should work, but you must do it before constructing your controller. Also, you may need Test.setCurrentPage(Page.vfPageName);
    – sfdcfox
    Dec 9, 2022 at 3:16
  • thank you so much. I added the ApexPages.currentPage().getParameters().put('filterId',ca.id); before calling the controller and it worked. Dec 9, 2022 at 3:25

You must log in to answer this question.

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