I am coding an Apex Test Class for an Apex Class. I am have trouble understanding why some lines of code are being covered while others are not. I also don't understand why the if statement in my method is never being fired/covered. Any help is greatly appreciated!

The Apex Class

public class apexClass {
        public ApexPages.StandardController std;
        public CustomObject__c customobject;
        public List<RS__c> rentSched;
        public List<wrapper> rSW {get; set;} // covered
        public Integer rowNum {get;set;} // NOT covered
        public Integer rowIndex {get;set;} // NOT covered
        public String rId {get;set;} // NOT covered
        public String txnStatus {get;set;} // NOT covered
        public Id customobjectId {get;set;}    // covered
        public String customobjectName {get;set;} // NOT covered
        public String spaceName {get;set;} // NOT covered
        public Date rcd {get;set;}  // NOT covered
        public Date expDate {get;set;} // NOT covered
        public String breakPoint {get; set;} // NOT covered
        public Decimal X1stExpRent {get; set;} // NOT covered
        public Decimal sqFtRpt {get; set;} // NOT covered
        public Decimal minRentAchAnn {get; set;} // NOT covered
        public Integer totRentSched {get; set;} // NOT covered
        public List<String> sFRM;
        public List<String> freeRentMonths {get; set;} // covered
        public Integer totalMonths; 
        public Boolean noOptions {get; set;} // covered

        public void method () {
            Integer i = 0;
            foo1 = new List<wrapper>();
            foo2 = new List<String>();
            foo3 = ApexPages.currentPage().getParameters().get('customobjectid');     
            if (customobjectId != null) {

                // do stuff 
                // NONE OF THIS IS COVERED
                // WHY IS customobjectId NULL?


The Test Class

public class apexClass_test {

    @isTest(SeeAllData=true) static void test_method_one() {

        apexClass calc = new apexClass ();




There are very, very few excuses for deploying a new unit test with seeAllData=true, and I don't see any of them here. Your unit tests should always create their own data.

WHY IS customobjectId NULL?

Because your test code does not populate it. One of the hardest things about shifting into writing unit tests is realizing that that's no real magic here. You have to do the tricky work of building the environment your code relies upon to execute.

Calling a method in test context is just calling the method. It doesn't execute every line in the method if the data - the context - does not result in the logic evaluating that way.

In this case, you need to populate the parameter with ...getParameters().put() so your controller can read it and thereby execute the body of your if statement. See for example this answer.

public Integer rowNum {get;set;} // NOT covered

Property declarations with getter and setter methods are considered executable, meaning they are available for code coverage. This applies even if the getter and setter are synthesized as they are here.

You handle this the same way you handle other code coverage: make sure it executes in test context, whether indirectly by executing a code path that itself calls the getter and/or setter, or by invoking them directly from your unit test.


I also don't understand why the if statement in my method is never being fired/covered

Because the if block gets executed only if customobjectId != null and that the value of this variable is always null as currently written. It seems you want to retrieve the value from query parameter into foo3 and utilize that in your if block. However, even if you had used that, your test class never sets the query parameter.

You will need to set the query parameter customobjectid in your test class and then utilize its value in your controller. Refer to the documentation for more details on testing controllers/extensions.

This is how you set values for query parameters when testing controllers/extensions.

PageReference pageRef = Page.success;
apexClass controller = new apexClass();
// Add parameters to page
ApexPages.currentPage().getParameters().put('customobjectid', '<value>');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.