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
EMHDevEMHDev 

Calling setter from test method

When using the get; set; type syntax for a method in my controller:

public Id AccSel {
        get;
        set {
            AccSel = value;
            selectedAcc = accMap.get(AccSel);           
        }
    }

How do I call the method directly from my unit test (to get code coverage)?  The documentation examples all use the other syntax where you have methods like getAccSel and setAccSel, which you can of course call from the test method.  In order to cover this code, do I have to rewrite it with separate getAccSel and SetAccSel methods instead of using automatic properties?

If it is possible, how do I pass the parameter of "value"?

From the VF page I call it using assignTo:
<apex:commandButton value="Refresh List" action="{!resetConts}" rerender="ContactLst, ContDetailpg">
    <apex:param name="AccSel" value="{!selectedAcc.Id}" assignTo="{!AccSel}"/>
</apex:commandButton>

Using
ApexPages.currentPage().getParameters().put('AccSel', ids);
does not call the setter.

Any ideas?

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

When using "automatic" properties, simply setting the value on the object should exercise this.

 

E.g.

 

myController.AccSel=myId;

All Answers

bob_buzzardbob_buzzard

When using "automatic" properties, simply setting the value on the object should exercise this.

 

E.g.

 

myController.AccSel=myId;

This was selected as the best answer
EMHDevEMHDev

Thanks, Bob.  Works a treat of course.  I should have worked that one out.  Sometimes we make things more complicated than they are!

 

Regards,
Erica