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
Bryan TelfordBryan Telford 

Test code coverage for {get set}

I have the following Apex class and test class that is only getting 15/21 lines covered. The part that is not covered is in bold below. Any ideas how I can get the setter covered in this Apex?

public class changeControlExtension {
    public Infrastructure_Change_Control__c cc {get;set;}
     
    public changeControlExtension(ApexPages.StandardController stdCtrl) {
        cc = (Infrastructure_Change_Control__c)stdCtrl.getRecord();
    }
     
    //get the multi-select pick list values
    public List<SelectOption> getMSPicklist {
        get {
            List<SelectOption> options = new List<SelectOption>();
            for(Schema.PicklistEntry obj : Infrastructure_Change_Control__c.Department_Approvals__c.getDescribe().getPicklistValues()) {
                options.add(new SelectOption(obj.getValue(), obj.getLabel()));
            }
            return options;
        }  
        set;
    }
     
    //get and set the multi-select pick list as checkboxes
    public String[] MSItems {
        get {
            List<String> selected = new List<String>();
            List<SelectOption> options = this.getMSPicklist;
            for(SelectOption obj : options) {
                if (this.cc.Department_Approvals__c !=null && this.cc.Department_Approvals__c.contains(obj.getValue()))
                    selected.add(obj.getValue());
            }
            return selected;
        }
        set {
            String selectedCheckBox = '';
            for(String s : value) {
                if (selectedCheckBox == '')
                    selectedCheckBox += s;
                else selectedCheckBox += ';' + s;
            }
            cc.Department_Approvals__c = selectedCheckBox;

        }
    }
}

Test Class
@isTest
public class changeControlExtension_Test {

    private static testmethod void changeControl() {
     
     Case cas = new Case(Subject = 'Testcase');
     insert cas;
     
     Infrastructure_Change_Control__c cc = new Infrastructure_Change_Control__c(Name = 'Test Change', Related_Case__c = cas.id);
     insert cc;
             
     ApexPages.StandardController controller = new ApexPages.StandardController(cc);
     changeControlExtension extension = new changeControlExtension(controller);
     
     Test.startTest();
     List<SelectOption> options = extension.getMSPicklist;
     cc.Department_Approvals__c = options.get(0).getValue();
     String[] items = extension.MSItems;
     Test.stopTest();
     
     System.assertNotEquals(options.get(0).getValue(), null);
     System.assertNotEquals(items, null);
        
    }

}
Best Answer chosen by Bryan Telford
MKRMKR
Hi,

At the very minimum you should at some point set the extension.MSItems in order to cover them. The line before Test.stopTest() is important for code coverage.
Test.startTest();
List<SelectOption> options = extension.getMSPicklist;
cc.Department_Approvals__c = options.get(0).getValue();
String[] items = extension.MSItems;
extension.MSItems = items;
Test.stopTest();
Overall, the above solution does not verify that the setter actually works. For that, you should do assertion according to your business requirements.

Regards,
Mkr
 

All Answers

MKRMKR
Hi,

At the very minimum you should at some point set the extension.MSItems in order to cover them. The line before Test.stopTest() is important for code coverage.
Test.startTest();
List<SelectOption> options = extension.getMSPicklist;
cc.Department_Approvals__c = options.get(0).getValue();
String[] items = extension.MSItems;
extension.MSItems = items;
Test.stopTest();
Overall, the above solution does not verify that the setter actually works. For that, you should do assertion according to your business requirements.

Regards,
Mkr
 
This was selected as the best answer
Bryan TelfordBryan Telford
Thanks! That was a big help. And yes, I got my assertions in there as well.