+ Start a Discussion

Test method for Wrapper class

Hi there,


working on a test method for a class that is a controller for a VF page and allows the user to check the box for the records they want and click on a button  to create new records on a diferent object, using the selected Contact records IDs.

Here is the Class:



public class wrapperClassController {
    GW_Volunteers__Volunteer_Shift__c[] stravail = [Select id, GW_Volunteers__Volunteer_Job__c, Shift_weekday__c from GW_Volunteers__Volunteer_Shift__c where id = :System.currentPagereference().getParameters().get('id')];
    public List<cContact> contactList {get; set;}

    public List<cContact> getContacts() {
        if(contactList == null) {
            contactList = new List<cContact>();
            for(Contact c : [Select c.ID, c.GW_Volunteers__Volunteer_Availability__c, c.GW_Volunteers__Volunteer_Status__c, c.Name, c.Volunteers_OK__c FROM Contact c WHERE c.Volunteers_OK__c = True and (c.GW_Volunteers__Volunteer_Availability__c INCLUDES (:stravail[0].Shift_weekday__c)) ]) {
                contactList.add(new cContact(c));
        return contactList;

    public PageReference processSelected() {

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

        for(cContact cCon : getContacts()) {
            if(cCon.selected == true) {

        System.debug('These are the selected Contacts...');
        for(Contact con : selectedContacts) {
            GW_Volunteers__Volunteer_Hours__c newvolu = new GW_Volunteers__Volunteer_Hours__c( GW_Volunteers__Contact__c = Con.id, GW_Volunteers__Volunteer_Job__c = stravail[0].GW_Volunteers__Volunteer_Job__c, GW_Volunteers__Volunteer_Shift__c = stravail[0].id, GW_Volunteers__Start_Date__c = system.today(), GW_Volunteers__Status__c = 'Scheduled', GW_Volunteers__Number_of_Volunteers__c = 1 );
            insert newvolu;
        return null;

    public class cContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}

        public cContact(Contact c) {
            con = c;
            selected = false;



I started to write a test method but got stuck when calling the class to test and don;t know how to make a record selected.:



private  class TestwrapperClassController
   static testMethod void testgetContacts()
      // insert a test account
      Account acc=new Account(Name='Test123');
      Database.saveresult sr = database.insert(acc);
      // insert a test contact
      List<Contact> con=new List<Contact>{new Contact(FirstName='Lester', Lastname='DbigTester', accountID=acc.id, email='123123123@888555.com', Volunteers_OK__c  = True, GW_Volunteers__Volunteer_Availability__c = 'Wednesday morning' ), 
            new Contact(FirstName='Lester2', Lastname='DbigTester2', accountID=acc.id, email='1231@888555.com', Volunteers_OK__c  = True, GW_Volunteers__Volunteer_Availability__c = 'Friday morning')};
      insert con;


      wrapperClassController tl=new wrapperClassController();
      List<Contact> ContactOK=tl.getContacts();

      System.assertEquals(0, ContactOK.size());

 VF Page:


<apex:page controller="wrapperClassController" showHeader="false">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Add Volunteers to shift" action="{!processSelected}" rerender="table" onclick="window.close()"/>
            Please check the Volunteers to be added to the Shift selected and press the "Add Volunteers to Shift" button.
            <!-- In our table we are displaying the cContact records -->
            <apex:pageBlockTable value="{!contacts}" var="c" id="table">
                <apex:column >
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!c.selected}"/>
                <!-- This is how we access the contact values within our cContact container/wrapper -->
                <apex:column value="{!c.con.Name}" />
                <apex:column value="{!c.con.GW_Volunteers__Volunteer_Availability__c}" />
                <apex:column value="{!c.con.GW_Volunteers__Volunteer_Status__c}" />




Any Help is really appreciated as usual. !




Shashikant SharmaShashikant Sharma

To select from wrapper create a public list of wrapper item, then select from test method.

Ankit AroraAnkit Arora

Sashikant is right, another way to do so is create a boolean property which will be true only when we are executing test class. If that property is true then set isSelected property of your wrapper class as true.



Ankit Arora



Guys thanks for the help!


I thought that was the list for the wrapped item, which is a CONTACT. The error I'm getting is:


Compile Error: Illegal assignment from LIST<wrapperClassController.cContact> to LIST<Contact> at line 21 column 7


Line 21 reads:


List<Contact> ContactOK=tl.getContacts();


Thanks again,