function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pamela NguyenPamela Nguyen 

How To Write A Test Method To Access(Cover) {get} Variable From Controller

Controller Code:
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);
        }
   }

Test Class (so far I've researched and written)
 
@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();
}

The code coverage is now 30%. Please help me with writing methods to cover public String xlsHeader & public String filterNameData.

The Controller was written by another Developer that is no longer working in my company. I'm taking his task now. 

As I've researched, Get is a Read-only variable. How do we call/invoke/access it in Test Class?

Thank you so much.
PrabhaPrabha
You are not using that variable anywhere in the class, why would that be covered?
Pamela NguyenPamela Nguyen
Hi @Prabha,

Thank you so much for your comment.

If it is not covered, how can I reach 75% of code coverage?

Thank you.
Mary FieldsMary Fields

Hello, 

A test method to access a variable in a controller can be written in a testing framework such as JUnit (for Java) or pytest (for Python), depending on the programming language and framework you are using. Here's an example of a JUnit test method in Java:
@Test
public void testGetVariable() {
    // Create an instance of the controller
    MyController controller = new MyController();

    // Set the value of the variable you want to test
    controller.setVariable("testValue");

    // Call the get method to retrieve the value of the variable
    String result = controller.getVariable();

    // Assert that the value returned by the get method is the expected value
    assertEquals("testValue", result);
}
In this example, we first create an instance of the MyController class. Then, we set the value of the variable we want to test using the setVariable method. Next, we call the getVariable method to retrieve the value of the variable and store it in the result variable. Finally, we use the assertEquals method to check that the value of result is equal to the expected value of "testValue".
 

This is just a simple example, and the specifics of your test method may vary depending on the specifics of your controller and the variables it contains.

Thanks,

https://www.myhealthatvanderbilt.us/

Shri RajShri Raj
@isTest
private class TestExportTemplateController {
    @isTest
    static void testXlsHeader() {
        ExportTemplateController controller = new ExportTemplateController(new ApexPages.StandardSetController(new List<Contact>()));
        String expected = '<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?>';
        String actual = controller.xlsHeader;
        System.assertEquals(expected, actual);
    }
    
    @isTest
    static void testFilterNameData() {
        List<Contact> contacts = new List<Contact>();
        Contact c = new Contact();
        c.FirstName = 'John';
        c.LastName = 'Doe';
        contacts.add(c);
        ApexPages.StandardSetController sc = new ApexPages.StandardSetController(contacts);
        sc.setFilterId('someFilterId');
        ExportTemplateController controller = new ExportTemplateController(sc);
        String expected = 'someFilterId';
        String actual = controller.filterNameData;
        System.assertEquals(expected, actual);
    }
}