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
ericmonteericmonte 

Wrapper Class Test

Hey all,

Can anyone help me write a unit test for this particular wrapper class?
https://developer.salesforce.com/page/Wrapper_Class

I'm more concerned on how to write the unite test for the following method:


public PageReference processSelected() {

                //We create a new list of Contacts that we be populated only with Contacts if they are selected
  List<Contact> selectedContacts = new List<Contact>();

  //We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
  for(cContact cCon: getContacts()) {
   if(cCon.selected == true) {
    selectedContacts.add(cCon.con);
   }
  }

  // Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
  System.debug('These are the selected Contacts...');
  for(Contact con: selectedContacts) {
   system.debug(con);
  }
  contactList=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
  return null;
}


// This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
public class cContact {
  public Contact con {get; set;}
  public Boolean selected {get; set;}

  //This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
  public cContact(Contact c) {
   con = c;
   selected = false;
  }
}

Any advice on how to approach this would be appreciated.

Thanks
Shyam BhundiaShyam Bhundia
In order to test this you need to create contacts and call the processSelected method which in turn should call getContacts.  

What's in the getContacts method?
KaranrajKaranraj
Hi Ericmonte - you have to initiate your wrapper class by the below method

[YourClassName.Wrapperclass]
processSelected.cContact wrapContact = new processSelected.cContact(con);


KaranrajKaranraj
Try the below test class code for sample, this will cover 95%

public class thecontrollerTests {

    private static testMethod void testMyController() {
        PageReference pageRef = Page.wrapContact;
        Test.setCurrentPage(pageRef);
        
        Contact c = new contact();
        c.LastName = 'testing';
        insert c;
        
        wrapperClassController controllerclass = new wrapperClassController ();     
        wrapperClassController.cContact wrapCon = new wrapperClassController.cContact(c);
        controllerclass.processSelected();
        
    }
}