+ Start a Discussion
chubsub2chubsub2 

Apex Test Coverage for Custom Controller on Visualforce page.

I'm only getting 14% coverage on the custom controller.  The controller basically lists fields that contain formula values onto a Visualforce page.  If a check box is checked on a record, the field will display on the Visualforce page.

 

Below is the error message which highlights the code that isn't covered in red, but I'm not sure how to go about writing test code to cover them. 

 

Thanks in advance to anyone that can help,

 

Here's a link to the image:

http://www.flickr.com

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sravusravu

You need not create a record becuase you are not using any constructor in your class.

Try the following code to increase your code coverage percentage:

 

@isTest
private class TestIngList {
    static testMethod void testIngList(){
ApexPages.StandardSetController con = new ApexPages.StandardSetController(Database.getQueryLocator([select name from CustomObject__c WHERE custom_check_box_field__c = true]));
        IngList ing = new IngList();
        ing.getList();
    }
}

 

Hope this will resolve your issue.

All Answers

sravusravu

Can you post the controller and the testmethod. I do not see any error content in you previous post.

chubsub2chubsub2

I highlighted the lines of code that were highlighted in red after I ran the test.

 

 

Controller:

public class IngList {
  // ApexPages.StandardSetController must be instantiated  
    
  // for standard list controllers  
    
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(

                      [select name, from CustomObject__c WHERE custom_check_box_field__c = true]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records  
    
    public List<CustomObject__c> getList() {
         return (List<CustomObject__c>) setCon.getRecords();

    }
}

 

 

 

 

Test Class:

@isTest

private class TestIngList {

static testMethod void testIngList(){
    ApexPages.StandardSetController setCon;
    PageReference pageRef = Page.CustomPage;

Custom_Object__c cusobj = new Custom_Object__c();
cusobj.custom_check_box_field__c = true;
insert cusobj;

List<CustomObject__c> cus = new List<Custom_Object__c>{};

System.assertNotEquals(null, cusobj.Id);
    Test.setCurrentPage(pageRef);
    IngList controller = new IngList();

}
}

sravusravu

You need not create a record becuase you are not using any constructor in your class.

Try the following code to increase your code coverage percentage:

 

@isTest
private class TestIngList {
    static testMethod void testIngList(){
ApexPages.StandardSetController con = new ApexPages.StandardSetController(Database.getQueryLocator([select name from CustomObject__c WHERE custom_check_box_field__c = true]));
        IngList ing = new IngList();
        ing.getList();
    }
}

 

Hope this will resolve your issue.

This was selected as the best answer
chubsub2chubsub2

Perfect, Thank you! 100% coverage.