You need to sign in to do that
Don't have an account?
Pamela Nguyen
How To Write A Test Method To Access(Cover) {get} Variable From Controller
Controller Code:
Test Class (so far I've researched and written)
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.
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.
Thank you so much for your comment.
If it is not covered, how can I reach 75% of code coverage?
Thank you.
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/