-1

I have the following class and text class. The test class covers the class at 90% but doesn't seem to do anything or test anything. Any idea 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());
        }
    }
}

}

Test class:

@isTest public without sharing class MyNewCaseListControllerTest {
@isTest public static void TestMyNewCaseListController(){
    
    List<Case> Cases = new List<Case>();
    for(integer i=0; i<20; i++){
        Case ca = new Case();
        ca.Subject = 'Test Subject - Field Test ' + i;
        ca.Description = 'Test Description - Field Test ' + i;
        ca.Origin = 'Web Email';
        ca.Type = 'Other';
    }
       
    Test.startTest();
    insert Cases;
    MyNewCaseListController controller  = new MyNewCaseListController(new ApexPages.StandardSetController(Cases));
string filter = ApexPages.currentPage().getParameters().get('filterId');

}

3
  • you want to assert the value of ctrl.getFilterId()
    – cropredy
    Dec 9, 2022 at 17:42
  • @ cropredy thanks. Still very new at this. So would that look like? I have tried a number of system.assertEquals statements, but whenever I try to reference ctrl.getFilterId() I get variable does not exist. I have tried: CommunitiesCaseListController.ctrl.getFilterId() ctrl.getFilterId() system.Assert(CommunitiesCaseListController.CommunitiesCaseListController(filterId)); But nothing has worked. Dec 9, 2022 at 17:54
  • Hi, I think you may have gotten the down votes because you need a little more info. if you can improve your question by adding a little amount of info, maybe tell us which lines are not covered. I think the answers will sort that out anyway, and your question provides code, so I will upvote for now because experienced people will see the issue anyway. Dec 9, 2022 at 23:30

2 Answers 2

1

First, you need to set the page parameters before the constructor is instantiated

Second, you need to determine the expected filterIds so you can use in asserts

// Given some mock cases
insert cases;

// Given a mock controller so we can get expected filterIds

ID expectedCoercedFilterId;
ID expectedRecentlyViewedCasesFilterId;
MyNewCaseListController controller  = new MyNewCaseListController(
                                         new ApexPages.StandardSetController(cases));
for (Integer i = 0; i < controller.getListViewOptions().size(); i++ ) {
  System.SelectOption listView : controller.getListViewOptions()[i]) {
  if (listView.getLabel() == 'Recently Viewed Cases') ) {
     expectedRecentlyViewedCasesFilterId = listView.getValue();
  }
  if (i == 0) {
     expectedCoercedFilterId = listView.getValue();
  }
}


Test.startTest();
// Given page Param points at an existing listView filter
ApexPages.currentPage().getParameters().put('filterId',expectedCoercedFilterId);
// when set Controller instantiated
controller  = new MyNewCaseListController(
                                         new ApexPages.StandardSetController(cases));
// verify that passed parameter is used to define the set Controller's filter
System.assertEquals(expectedCoercedFilterId,controller.getFilterId(),'filterId if passed sb used');

// Given page Param is empty
ApexPages.currentPage().getParameters().put('filterId',null);
// when set Controller instantiated
controller  = new MyNewCaseListController(
                                         new ApexPages.StandardSetController(cases));
// verify that setController's filterId is Recently Viewed Cases
System.assertEquals(expectedRecentlyViewedCasesFilterId,controller.getFilterId(),'filterId if null sb set');
5
  • What, not using the new Assert.areEqual?! ๐Ÿ˜‰
    – Phil W
    Dec 9, 2022 at 18:50
  • the fingers type faster than the mind ๐Ÿ˜‰; I'm slowly moving to the new Assert class
    – cropredy
    Dec 9, 2022 at 18:55
  • @ cropredy again thanks. It looks like there might have been a typo in the line System.SelectOption listView : controller.getListViewOptions()[i] )[ But I can't figure out the syntax. Dec 9, 2022 at 19:34
  • @ cropredy I am also still getting an error message on the line "for (Integer i = 0; i < (Case)controller.getListViewOptions().size(); i++ ) {" Method does not exist or incorrect signature: void getListViewOptions() from the type MyNewCaseListController Dec 9, 2022 at 21:04
  • oops --edited answer
    – cropredy
    Dec 9, 2022 at 21:33
1
  1. Because your Page Controller has an If Statement regarding a listview filter ID, this means you need to render the page without an Id, and with an ID. Now doing this may raise new questions/issues...

  2. The important thing to note with test methods is that you should test your page scenario's, and you should not concern yourself with coverage (you should but if you test all scenarios then the coverage will be there). Always aim for 100%, but it is not always achievable.

Some Code shows the 2 paths through your test scenarios:

List caseList = [SELECT id FROM Case]; //what else goes in here???

ListView LV = [SELECT Id, Name FROM ListView WHERE sobjectType = 'Case' AND Name = 'MyListview Filter Name'];

//or even beter
LV = [SELECT Id, Name FROM ListView WHERE sobjectType = 'Case' AND Name != 'Recently Viewed Cases'];
//First Page Render should 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(ca));

//CODE NOT TESTED BY ME

I am interested in your requirement for the recently viewed case list or the specific list view filter as a parameter.

You must log in to answer this question.

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