0

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.

Pavel Slepiankou
  • 3,516
  • 2
  • 25
  • 30
Chathura
  • 155
  • 6
  • One optimisation that you can use is simply change if-else to `return reg[0].Status__c == 'Review';`, it will help decrease lines of code. – jagmohan Feb 20 '16 at 08:21
  • I can not go to this public Boolean showNtc method by a test class. – Chathura Feb 20 '16 at 08:34

1 Answers1

1

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);
}
Pavel Slepiankou
  • 3,516
  • 2
  • 25
  • 30