0

I've been searching for hours to write a test class for the Controller.

Scenario: All Visualforce Page will use this controller to export a CSV/Excel template from List View. This Controller has written by another Dev that stopped working and I'm quite new to it.

Here Is The Code For Controller:

public with sharing class ExportTemplateController {
    
    public String xlsHeader{
        get{
            String strHeader = '';
            strHeader += '<?xml version="1.0"?>';
            strHeader += '<?mso-application progid="Excel.Sheet"?>';
            return strHeader;
        }
    }
    
    public String filterNameData{
        get{
            String selectedFilterId = this.controller.getFilterId();
            return selectedFilterId;
        }
    }
    
    ApexPages.StandardSetController controller;    
    public ExportCSVController(ApexPages.StandardSetController controller) {
        
        this.controller = controller;   
        if (controller.getResultSize() < 2000 ) {
            controller.setPageSize(controller.getResultSize());
        } else {
            controller.setPageSize(2000);
        }
   }

My understanding of the code: It is written so that any Standard Controller (Account, Lead, Opportunity) can use it. The code also retrieved a filter name/ filter Id of List View. After it has the filter Id, it will check the size (how many records that List View has).

My test class:

@isTest private class ExportTemplateControllerTest {

@isTest
private static void ExportTemplateTest() {

    //Try insert a list of accounts to use Account Controller

    List<Account> testAccs = new List<Account>();
    for (Integer i = 0; i < 200; i++) {
        Account a = new Account(Name='TestAccount' + i);
        testAccs.add(a);
    }
    insert testAccs;
    
    Test.startTest();
    PageReference pageRef = Page.OpenAccountPage;
    Test.setCurrentPage(pageRef);
    ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(testAccs);
    stdSetController.setSelected(testAccs);
    ExportCSVController ext = new ExportCSVController(stdSetController);
    System.assertEquals(200, 200);
    Test.stopTest();
}

}

I read a very good explanation Apex Test - Instantiate new Controller but I still don't know how to figure out how to access those 2 public strings xlsHeader & filterNameData

enter image description here

Thank you guys so much,

Pam.

2
  • your ExportCSVController takes as an arg ApexPages.StandardSetController which is different from ApexPages.StandardController being used in the testmethod Commented Feb 6, 2023 at 20:29
  • Hi @cropredy thank you so much! I did some research and now I can set the StandardSetController Commented Feb 7, 2023 at 4:40

1 Answer 1

0

The lines not being covered are properties referenced by the VF page

public String xlsHeader{
    get{
        String strHeader = '';
        strHeader += '<?xml version="1.0"?>';
        strHeader += '<?mso-application progid="Excel.Sheet"?>';
        return strHeader;
    }
}

public String filterNameData{
    get{
        String selectedFilterId = this.controller.getFilterId();
        return selectedFilterId;
    }
}

as such, you need to simulate access to them as if the VF page were requesting their values.

after this line in your testmethod:

stdSetController.setSelected(testAccs);
stdSetController.setFilterId('someFilterId');  // init the filterId 
ExportCSVController ext = new ExportCSVController(stdSetController);

you will do

Assert.areEqual('<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?>',ext.xlsHeader,
               's/s header sb as expected'); 

and

Assert.areEqual('someFilterId',ext.filterSomeData,'stdSetController filterId sb returned');

These lines simulate the VF page fetching the variable's getters

Lastly, this line in your testmethod:

 System.assertEquals(200, 200);

is a tautology and adds no value, it should be

 Assert.areEqual(200,ext.getPageSize(),
                'pg size sb # of records in set controller when fewer than 2000 recs');
  

Note best practice of always using a string message to explain the assert.

P.S.

If this is all the controller does, you dont need to insert the Accounts. The set Controller accepts a list of Sobjects

List<Account> testAccs = new List<Account>();
for (Integer i = 0; i < 200; i++) {
    Account a = new Account(Name='TestAccount' + i);
    testAccs.add(a);
}
// insert testAccs; - not required unless your controller is querying or DML'ing these

This makes running the test faster and you can thus easily test the code path for the result size of > 2000 without having to insert 2000 accounts and potentially running into Limits issues

1
  • Thank you soo much! Your explanation is very easy to understand. I learned a lot from you. @cropredy Commented Feb 7, 2023 at 17:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.