Reputation: 155
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.
Upvotes: 0
Views: 123
Reputation: 3585
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);
}
Upvotes: 1