+ Start a Discussion
SFDC_LearnerSFDC_Learner 

Test class Code coverage

How can i cover the code:
public with sharing class chkboxcon {
    public String lstwrapper { get; set; }
    public chkboxcon() {
    }
    public chkboxcon(ApexPages.StandardController controller) {
    }
    public boolean[] chk1{get;set;}
    public boolean[] chk2{get;set;}
    public List<boolean> chk1select=new List<boolean>();
    public List<boolean> chk2select=new List<boolean>();
    public void processdata()
    {
        system.debug('**** One****');
        for(Integer i=0;i<lstchk.size();i++)
        {
            system.debug('**** Two ****');
            if(chk1[i]==true)
            {
                system.debug('**These checkboxes are checked****'+i);
            }
        }
    }
    public List<Chkbox__c> lstchk=new List<Chkbox__c>();
    public List<Chkbox__c> getRecs() {
        lstchk=[select Id,name from Chkbox__c];
        return lstchk;
    }
    static testmethod void testm1()
    {
        chkboxcon clsobj=new chkboxcon();
        ApexPages.StandardController constructorvar;
        chkboxcon clsobj1=new chkboxcon(constructorvar);
        clsobj.processdata();
        clsobj.getRecs();
    }
}
How can i cover these lines in my class.
Best Answer chosen by Admin (Salesforce Developers) 
Jake GmerekJake Gmerek

No problems, if you have any specific questions as you go, feel free to ask.

All Answers

My OwnMy Own

Can you please let me know about this object. 

 

Is Chkbox__c is having any boolean value?  I don't see  setting of value for this. If you are able to set the value for this, then the code will automatically covered. 

 

 

            

 

 

Jake GmerekJake Gmerek

Hello,

 

There are Three main steps to writing a good test method:

 

1. Set up the Data - Never assume that any data that you are querying is in the database.  Add test data as the first thing you do.

2. Execute your code, ensuring that you follow all branches.

3. Test your code results for accuracy (i.e. something like system.debugEquals(...))

 

So you could be running into a couple of problems here.  First "[select Id,name from Chkbox__c];" may not be returning any data since your test class does not set any data up.  Then "lstchk.size()" would be zero so the for loop is checked but never executed.  Also, once you get the for loop running, "chk1[i]==true", is always going to be false since it is not set anywhere in your code.  Finally, even when you correct all of the above Istchk.size() will still be zero because it is being set in the getRecs() method and in your test class you are calling it after the processData() method.

 

I hope this gives you some insight into your problems.

SFDC_LearnerSFDC_Learner

Yes Jake,

Thanku so much for telling my mistakes in this code..

I am only in initial stage. Today only i wrote my first test class.

That helps great to write good test methods.

 

 

 

 

Jake GmerekJake Gmerek

No problems, if you have any specific questions as you go, feel free to ask.

This was selected as the best answer