This is my sample class

public class className{

    public className()
        public Boolean showNTC 
                if(reg[0].Status__c == 'In Review')
                    return true;
                else return false;
            }private set;
        public void loadData()

I've used following code to execute and return the boolean value : in VF page

<apex:commandButton rendered="{!showNTC}" value="Refresh" styleClass="btn styled-button1"}">

Everything is working fine. Only issue is i'm unable to write test coverage for above showNTC getter.

Since the above code is executed only by visualforce expression, i'm unable to execute. How do i write test coverage to execute above code ?

Thank you.

Test class so far i've written is something like this

 MyPage= Page.MyPage; //Page which attached to controller
 StdConVR= new ApexPages.standardController(Obj);
System.currentPageReference().getParameters().put('id', obj.id);

StdConObj= new ApexPages.standardController(Obj);
ClassName testObj_Controller= new ClassName  ();
  • Can you post what you have so far of your test class? – Jenny B Feb 20 '16 at 14:44
  • Thank you...I've updated the question with testClass code, i've used sofar – Chathura Feb 20 '16 at 15:01
  • 3
    use ' testObj_Controller.showNT = false' or system.assert( testObj_Controller.showNT);' – Tushar Sharma Feb 20 '16 at 15:34
  • Thanks for your reply. I tried both the ways. testObj_Controller.showNT=false gave a compiled error while second option it gave a run time error (Assertion failed). Any possible reason? – Chathura Feb 20 '16 at 16:12
  • Set the Status__c value and then use assert. – Tushar Sharma Feb 20 '16 at 17:44

It is good practice imo to use private setters as you have done. However, this means you can only directly call the getter (which usually calls the setter anyway). In your case, you actually don't even need a setter, because you are never actually setting your property value anywhere (showNTC=...).

Anyway, your test should be along the lines of:

static testMethod void testMyProperty_true()
    ClassName instance = new ClassName();
    // further manipulation so reg[0].Status__c == 'In Review')
    system.assertEquals(true, instance.showNTC, message);
static testMethod void testMyProperty_false()
    ClassName instance = new ClassName();
    // further manipulation so reg[0].Status__c != 'In Review')
    system.assertEquals(false, instance.showNTC, message);

Also, the property itself can be cleaned up to just:

public Boolean showNTC { get { return reg[0].Status__c == 'In Review'; } }

Some might prefer to make it an actual method when there is only a getter:

public Boolean getShowNTC() { return reg[0].Status__c == 'In Review'; }

which you would test slightly differently:

system.assertEquals(false, instance.getShowNTC());

To test a lazy load on a query, your code might look like:

public List<Attachment> attachments
        if (attachments == null)
            // attachments = [query];
        return attachments;
    private set;

Notice that here you are actually making use of the setter. Often I test hitting the getter multiple times to make sure I only query once:

static testMethod void testAttachments()
    // set up attachments
        Integer startQueries = Limits.getQueries();
        List<Attachments> loadedAttachments;
        for (Integer i = 0; i < Limits.getLimitQueries(); i++)
            loadedAttachments = instance.attachments;
        Integer endQueries = Limits.getQueries();

    system.assertEquals(1, endQueries, message);
    system.assertEquals(expectedSize, loadedAttachments.size(), message);
| improve this answer | |
  • Thank you for your solution, it works fine. It works fine for Boolean type. Is there any way if the getter returns non-boolean values ex: public List<Attachment> attachments { get { if(attachments == null) { //something.. } return attachments; } private set; } Please tell me if there is any way? Thanks a lott.. – Chathura Feb 21 '16 at 6:11
  • @Chathura I added an example, but if you want more detail than what I provided, you might want to ask a separate question. – Adrian Larson Feb 21 '16 at 17:23
  • 1
    @Chathura Any more detail needed here? If this answer solved your issue, please accept it so as to help future members of the community. – Adrian Larson Feb 22 '16 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.