You need to sign in to do that
Don't have an account?
SFDC_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.
No problems, if you have any specific questions as you go, feel free to ask.
All Answers
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.
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.
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.
No problems, if you have any specific questions as you go, feel free to ask.