1

I am still new to Apex testing and am trying to build a test class on the following, but keep running into the error message “Constructor not defined: [MyNewCaseListController].(ApexPages.StandardController)” It is coming from the 16th line in the test class. Any ideas what I am doing wrong?

public with sharing class MyNewCaseListController {
public MyNewCaseListController(ApexPages.StandardSetController  ctrl){
    string filter = ApexPages.currentPage().getParameters().get('filterId');
    ctrl.setPageSize(50);
    
    List<System.SelectOption> listViews = ctrl.getListViewOptions();
    if (filter != null) {
        ctrl.setFilterId(filter);
    }
    else {

        for(System.SelectOption listView : listViews)
        {
            if (listView.getLabel() == 'Recently Viewed Cases') 
                ctrl.setFilterId(listView.getValue());
        }
    }
}

@isTest public without sharing class MyNewCaseListControllerTest {
@isTest public static void TestMyNewCaseListController(){
    
    Case ca = new Case();
    String i = Datetime.now().format('yyyy-MM-dd');
    ca.Subject = 'Test Subject - Field Test ' + i;
    ca.Description = 'Test Description - Field Test ' + i;
    ca.Origin = 'Web Email';
    ca.Type = 'Other';
    insert ca;
    string filterId = ca.Id;            
    Test.startTest();
    ApexPages.currentPage().getParameters().put('filterId',ca.id);
    MyNewCaseListController controller  = new MyNewCaseListController(new ApexPages.StandardController(ca));

    
    
    string filter = ApexPages.currentPage().getParameters().get('filterId');
    controller.setPageSize(50);
    
    List<System.SelectOption> listViews = ctrl.getListViewOptions();

    // Set Filter Id if passed in parameter
    if (filter != null) {
        ctrl.setFilterId(filter);
    }
    else {
        for(System.SelectOption listView : listViews)
        {
            if (listView.getLabel() == 'Recently Viewed Cases') 
                ctrl.setFilterId(listView.getValue());
        }
    }
}
2
  • @sfdcfox thanks for your help with the other question. Any chance you can help with this one? It is related to the other one you helped with and I thought it would be similar but having problems. Commented Dec 9, 2022 at 6:38
  • Adding this comment in each question: You should provide your Visualforce, or at least your requirement so that your controller is designed in the right way. Commented Dec 10, 2022 at 0:05

1 Answer 1

1

Firstly: separate the 2 classes, this makes life easier via deployment and source control etc.

But to the issue at hand: You are using ApexPages.StandardSetController in your Class, but your test provides ApexPages.StandardController

Yes pass the standardSetController a list. Which now I read your code more carefully you do want a list. I am interested in what your requirement is?

But there is another issue: In your test you are passing a Case Id as if it is a ListView FilterId.

You could do something like this in your Test Class;

    ListView LV = [SELECT Id, Name FROM ListView WHERE Name = 'MyListview Filter Name'];
filter

but your test needs to run this page twice, once with a listviewId and once without. so that you cover the if statement and test the 'Recently Viewed Cases Listview.

You do not need 2 test methods, you just need to reset the controller within the test:

List<Case> CaseList = [SELECT id FROM Case]; //what else goes in here???
//First Page Render test no filterId
MyNewCaseListController controller  = new 
MyNewCaseListController(new ApexPages.StandardSetController(CaseList));
//Second Page Render tests FilterId set from query above
ApexPages.currentPage().getParameters().put('filterId',LV.Id);
controller  = new MyNewCaseListController(new ApexPages.StandardSetController(CaseList));
//CODE NOT TESTED BY ME

Caveat and aside:

You need to ensure you maintain your list views properly thought otherwise this test class will fail (I suppose that is the point), problem is listview can be updated directly in prod and hence your test could fail a at a later date when you bring in new code, hence source control is important with these types of code bases.

10
  • @ TheArchitecta thanks. Ug. Can't believe I missed that. However, still getting the error message "Constructor not defined: [ApexPages.StandardSetController].<Constructor>(Case)". I just don't know what I need to pass it - perhaps a list? Commented Dec 9, 2022 at 15:00
  • @ TheArchitecta. That was it. I had to pass it a list of cases and it worked. Now having other issues with the controller not being able to set the page size. Getting "Method does not exist or incorrect signature: void setPageSize(Integer) from the type CommunitiesCaseListController". But I will add a new question for that. Commented Dec 9, 2022 at 15:15
  • @ TheArchitecta. Ok the code now shows no errors, but when I do a system.debug on "controller" after that it says Expression cannot be assigned. Commented Dec 9, 2022 at 16:59
  • Updated my answer with the other issue filterId should not be Case Id Commented Dec 9, 2022 at 22:56
  • Can I ask, what is the requirement, as reviewing your questions and particularly this one, it seems to be replicating out-of-the-box functionality. And I wonder if there is a different way to solve the problem. Or are you trying new things out? Commented Dec 9, 2022 at 23:03

You must log in to answer this question.

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