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
Thank you guys so much,
Pam.
ExportCSVController
takes as an argApexPages.StandardSetController
which is different fromApexPages.StandardController
being used in the testmethod