You need to sign in to do that
Don't have an account?
Unable to get code coverage for an Apex Class
I have created Apex Class #1 which filters out a particular data value, and created a test for that class. I am able to get 95% code coverage.
I then created Apex Class #2 which includes the data value exclused from Class #1. I then created a test for Class #2, but I get 0% code coverage.
Here is Apex Class #2:
/*Controller class for the PropInactiveUnitRelatedList visualforce page used inline on Property layouts
to show only inactive Units related to the Property
*/
public class InactiveUnitList{
public Integer noOfRecords{get; set;}
public Integer size{get;set;}
public boolean showHeaderSideBar{get;set;}
public String redirectUrl {get;set;}
ApexPages.StandardController controller;
private boolean fullscreen;
public InactiveUnitList(ApexPages.StandardController controller) {
this.controller = controller;
//check if page needs to be opened in full screen mode
String value = ApexPages.currentPage().getParameters().get('fullscreen');
if(String.isNotBlank(value) && value == 'true'){
fullscreen = true;
showHeaderSideBar = true;
}else{
fullscreen = false;
showHeaderSideBar = false;
}
}
public ApexPages.StandardSetController units{
get{
if(units == null){
if(fullscreen) size = 100;
if(size==null) size=5;
units = new ApexPages.StandardSetController(Database.getQueryLocator(
[Select Name, Unit_Number__c, RSF__c, Parent_Tenant__r.name, Tenant__r.name, Unit_Status_Code__c, Current_Lease__r.name, RecordType.name
from Unit__c
where Property__c = :controller.getId()
AND Unit_Status_Code__c = 'N']));
units.setPageSize(size);
noOfRecords = units.getResultSize();
}
return units;
}
set;
}
public Boolean hasNext {
get {
return units.getHasNext();
}
set;
}
public Boolean hasPrevious {
get {
return units.getHasPrevious();
}
set;
}
public Integer pageNumber {
get {
return units.getPageNumber();
}
set;
}
public void first() {
units.first();
}
public void last() {
units.last();
}
public void previous() {
units.previous();
}
public void next() {
units.next();
}
public PageReference refreshPageSize() {
units.setPageSize(size);
return null;
}
public void showAll(){
redirectUrl = '/apex/PropUnitRelatedList?id='+controller.getId()+'&fullscreen=true';
}
public void saveUnits(){
try {
units.save();
} catch (Exception e) {
ApexPages.addMessages(e);
}
}
}
And here is the test for Class #2:
@isTest
public class InactiveUnitListTest
{
static testMethod void testInactiveUnitListPage()
{
Property__c objProperty = new Property__c(Name = 'Test Prop');
insert objProperty;
/* Assuming Name is not an auto number here. Please populate all the required field value pairs.*/
List<Unit__c> lstUnit = new List<Unit__c>{ new Unit__c( BU_Unit__c = 'Test Unit1', Property__c = objProperty.Id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit2', Property__c = objProperty.Id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit3', Property__c = objProperty.id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit4', Property__c = objProperty.id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit5', Property__c = objProperty.id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit6', Property__c = objProperty.id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit7', Property__c = objProperty.id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit8', Property__c = objProperty.id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit9', Property__c = objProperty.id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit10', Property__c = objProperty.id,
Unit_Status_Code__c = 'N')
};
insert lstUnit;
// replace YOURPAGENAME with actual page name
PageReference pageRef = Page.PropUnitRelatedList;
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('fullscreen','true');
ApexPages.StandardController sc = new ApexPages.StandardController(objProperty);
// now pass it to the extension
UnitList stdController = new UnitList(sc);
ApexPages.StandardSetController setCon = stdController.units;
// verify if 10 rows returned
system.assertEquals(0, stdController.noOfRecords);
Boolean blnHasNext = stdController.hasNext;
Boolean blnHasPrevious = stdController.hasPrevious;
system.assertEquals(1, stdController.pageNumber);
stdController.next();
//system.assertEquals(2, stdController.pageNumber);
stdController.last();
//system.assertEquals(2, stdController.pageNumber);
stdController.previous();
//system.assertEquals(2, stdController.pageNumber);
stdController.first();
//system.assertEquals(1, stdController.pageNumber);
stdController.refreshPageSize();
stdController.showAll();
stdController.saveUnits();
// turn off fullscreen
ApexPages.currentPage().getParameters().put('fullscreen','false');
sc = new ApexPages.StandardController(objProperty);
// now pass it to the extension
stdController = new UnitList(sc);
}
}
Class #1 and Class #2 are so similar; only 1 line of code is different. I must be missing something obvious; please advise.
I then created Apex Class #2 which includes the data value exclused from Class #1. I then created a test for Class #2, but I get 0% code coverage.
Here is Apex Class #2:
/*Controller class for the PropInactiveUnitRelatedList visualforce page used inline on Property layouts
to show only inactive Units related to the Property
*/
public class InactiveUnitList{
public Integer noOfRecords{get; set;}
public Integer size{get;set;}
public boolean showHeaderSideBar{get;set;}
public String redirectUrl {get;set;}
ApexPages.StandardController controller;
private boolean fullscreen;
public InactiveUnitList(ApexPages.StandardController controller) {
this.controller = controller;
//check if page needs to be opened in full screen mode
String value = ApexPages.currentPage().getParameters().get('fullscreen');
if(String.isNotBlank(value) && value == 'true'){
fullscreen = true;
showHeaderSideBar = true;
}else{
fullscreen = false;
showHeaderSideBar = false;
}
}
public ApexPages.StandardSetController units{
get{
if(units == null){
if(fullscreen) size = 100;
if(size==null) size=5;
units = new ApexPages.StandardSetController(Database.getQueryLocator(
[Select Name, Unit_Number__c, RSF__c, Parent_Tenant__r.name, Tenant__r.name, Unit_Status_Code__c, Current_Lease__r.name, RecordType.name
from Unit__c
where Property__c = :controller.getId()
AND Unit_Status_Code__c = 'N']));
units.setPageSize(size);
noOfRecords = units.getResultSize();
}
return units;
}
set;
}
public Boolean hasNext {
get {
return units.getHasNext();
}
set;
}
public Boolean hasPrevious {
get {
return units.getHasPrevious();
}
set;
}
public Integer pageNumber {
get {
return units.getPageNumber();
}
set;
}
public void first() {
units.first();
}
public void last() {
units.last();
}
public void previous() {
units.previous();
}
public void next() {
units.next();
}
public PageReference refreshPageSize() {
units.setPageSize(size);
return null;
}
public void showAll(){
redirectUrl = '/apex/PropUnitRelatedList?id='+controller.getId()+'&fullscreen=true';
}
public void saveUnits(){
try {
units.save();
} catch (Exception e) {
ApexPages.addMessages(e);
}
}
}
And here is the test for Class #2:
@isTest
public class InactiveUnitListTest
{
static testMethod void testInactiveUnitListPage()
{
Property__c objProperty = new Property__c(Name = 'Test Prop');
insert objProperty;
/* Assuming Name is not an auto number here. Please populate all the required field value pairs.*/
List<Unit__c> lstUnit = new List<Unit__c>{ new Unit__c( BU_Unit__c = 'Test Unit1', Property__c = objProperty.Id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit2', Property__c = objProperty.Id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit3', Property__c = objProperty.id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit4', Property__c = objProperty.id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit5', Property__c = objProperty.id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit6', Property__c = objProperty.id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit7', Property__c = objProperty.id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit8', Property__c = objProperty.id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit9', Property__c = objProperty.id,
Unit_Status_Code__c = 'N'),
new Unit__c( BU_Unit__c = 'Test Unit10', Property__c = objProperty.id,
Unit_Status_Code__c = 'N')
};
insert lstUnit;
// replace YOURPAGENAME with actual page name
PageReference pageRef = Page.PropUnitRelatedList;
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('fullscreen','true');
ApexPages.StandardController sc = new ApexPages.StandardController(objProperty);
// now pass it to the extension
UnitList stdController = new UnitList(sc);
ApexPages.StandardSetController setCon = stdController.units;
// verify if 10 rows returned
system.assertEquals(0, stdController.noOfRecords);
Boolean blnHasNext = stdController.hasNext;
Boolean blnHasPrevious = stdController.hasPrevious;
system.assertEquals(1, stdController.pageNumber);
stdController.next();
//system.assertEquals(2, stdController.pageNumber);
stdController.last();
//system.assertEquals(2, stdController.pageNumber);
stdController.previous();
//system.assertEquals(2, stdController.pageNumber);
stdController.first();
//system.assertEquals(1, stdController.pageNumber);
stdController.refreshPageSize();
stdController.showAll();
stdController.saveUnits();
// turn off fullscreen
ApexPages.currentPage().getParameters().put('fullscreen','false');
sc = new ApexPages.StandardController(objProperty);
// now pass it to the extension
stdController = new UnitList(sc);
}
}
Class #1 and Class #2 are so similar; only 1 line of code is different. I must be missing something obvious; please advise.