function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri 

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
User-added image
pbattissonpbattisson
Your test class should contain multiple methods in which you test all the possible paths of your code. You are having this line missing because you have a single test method which only tests one route of the code. You also have no assertions which means the tests are not very useful (how do you know it is working?)

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. 
Anoop yadavAnoop yadav
Hi,

Try the below code.
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);
      cw.selected = true;
      ad.getContacts();
      ad.processSelected();

 
  }
}

Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

@Anoop Yadav

Hi, we've already tried that but no use. :(

Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

@pbattisson

Hi, will you please give me an example that covered selectedoption (checkbox).

pbattissonpbattisson
You could try something like this:

public class wrapperClassController {

   
    public List<cContact> contactList {get; set;}

    public wrapperClassController(){
        getContacts();
    }

   
    private void 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));
            }
        }
    }


    public PageReference processSelected() {

               
        List<Contact> selectedContacts = new List<Contact>();

        for(cContact cCon: contactList {
            if(cCon.selected == true) {
                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;
        }
    }
}

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:

@isTest
private class wrapperClassController_Test
{
  private static testmethod void addUser_Test()
  {
    Contact c =new Contact();
    c.LastName='RRRR';
    insert c;

    wrapperClassController ad = new wrapperClassController();
    ad.contactList[0].selected = true;
    ad.processSelected();
  }
}

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..