Wednesday, December 14, 2022

Write A Test Class For Getter and Setter Methods in Salesforce Apex

Please use the below code snippet in you case you have getter and setter methods in your apex class and want to cover up the same lines in test class.

Main Class:

Public class SreeTestCase
{
 public List<SelectOption> caseStatusList 
 {
  get {
	List<SelectOption> options = new List<SelectOption>();
	for( Schema.PicklistEntry f : Case.Status.getDescribe().getPicklistValues()) 
        {
	  options.add(new SelectOption(f.getValue(), f.getLabel()));
	}
	return options;
  }
		set;
 }
}

Test Class:

@isTest 
private class SreeTestCaseTestClass 
{
    static testMethod void testStatusList() 
	{
		SreeTestCase st = new SreeTestCase();
		List<SelectOption> options = new List<SelectOption>();
		options = st.caseStatusList;
	}
}


Please comment or write us if you have any queries/requirements.

No comments:

Post a Comment

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC)

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC) Introduction: Salesforce Lightning Web ...