You need to sign in to do that
Don't have an account?
Test class for wrapper class
Hi,
I am trying write test class for a wrapper class, but my concern is I couldn't cover one line. I am posting wrapper class and test class please help me its very urgent :( .........
Class :-
public class wrapperClassController {
public List<cContact> contactList {get; set;}
public List<cContact> getContacts() {
if(contactList == null) {
contactList = new List<cContact>();
for(Contact c: [select Id, Name, Email, Phone from Contact limit 10]) {
contactList.add(new cContact(c));
}
}
return contactList;
}
public PageReference processSelected() {
List<Contact> selectedContacts = new List<Contact>();
for(cContact cCon: getContacts()) {
if(cCon.selected == true) {
//********************------------------------- The following is not covered plese find attachment-------------------------------****************************************
selectedContacts.add(cCon.con);
}
}
System.debug('These are the selected Contacts...');
for(Contact con: selectedContacts) {
system.debug(con);
}
contactList=null;
return null;
}
public class cContact {
public Contact con {get; set;}
public Boolean selected {get; set;}
public cContact(Contact c) {
con = c;
selected = false;
}
}
}
Test class :-
@isTest(seeAllData=true)
private class wrapperClassController_Test
{
private static testmethod void addUser_Test()
{
Contact c =new Contact();
c.LastName='RRRR';
insert c;
ApexPages.StandardController sc =new ApexPages.StandardController(c);
wrapperClassController ad=new wrapperClassController();
wrapperClassController.cContact cw=new wrapperClassController.cContact(c);
ad.getContacts();
ad.processSelected();
}
}
The following line is uncovered in the class
Looking through your code however - that line of code does nothing. Your contact wrapper will never have the selected variable set to true as you are initializing it to false on instantiation and then using it straight away. You need to refactor your code to ensure that flag can be set (I would imagine you have a use case requiring that) so that you can then test that line. Or remove that line altogether.
Try the below code.
@Anoop Yadav
Hi, we've already tried that but no use. :(
@pbattisson
Hi, will you please give me an example that covered selectedoption (checkbox).
Here you set the contactList variable when the class is instantiated via the constructor. Your getContacts method is updated to be private and return void as it is just setting a variable within the class. If you update your test to be:
Then it should all be covered. Note as well that on your page you need to bind to the cContact.selected variable for the checkbox (which I believe you are already doing). Then simply add your processing code in place of your debug statement..