How to write test class for codes which is used by VF pages as expression language

136 views Asked by At

I need to write a test coverage code for a getter, setter methods in a controller class

public Boolean showNtc {
    get {
        if (reg[0].Status__c == 'Review') {
            return true;
        } else {
            return false;
        }
    }
    private set;
}

in a VisualForce page code is like below

<apex:outputPanel id="step2" rendered="{!showNtc}"

Everything is working fine, expect i'm unable to execute above code by a test class. I tried several ways, but i failed.

1

There are 1 answers

0
Pavel Slepiankou On

In order to cover this code with test you have to emulate at least 2 states:

  • reg[0].Status__c == 'Revire'
  • reg[0].Status__c != 'Revire'

Also I recommend to consider the case when reg has no records because this might cause NPE.

So in your tests you need something like that

@isTest
static void test1() {
    ObjectWhichIsOnRegList__c obj = new ObjectWhichIsOnRegList__c();
    obj.Status__c = 'Review';
    insert obj;

    ControllerClassName ctrl = new ControllerClassName();
    System.assert(ctrl.showNtc);
}


@isTest
static void test1() {
    ObjectWhichIsOnRegList__c obj = new ObjectWhichIsOnRegList__c();
    obj.Status__c = 'Any other Status, but not Review';
    insert obj;

    ControllerClassName ctrl = new ControllerClassName();
    System.assert( !ctrl.showNtc);
}