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
Andrei KuznetsovAndrei Kuznetsov 

How to create unit tests for view list page with selection boxes?

Hi,

 

I've created Page that will display all Contacts for the Account. Beside each Conact there is a check box. User will check some Contacts and click on Update button. This action will copy Account's address to all selected contacts' addressess. Here is my controller. Now, how do I test it so I have 100% coverage?

 

=============================================================================================

public class TD_ContactsAddressController
{
    List<contactwrapper> contactList = new List<contactwrapper>();
    List<Contact> selectedContacts = new List<Contact>();
    // Sorting
    List<Contact> sortContacts = new List<Contact>();
    public String sortField {get; set;}
    public String previousSortField {get; set;}
   
    //Constructor
    public TD_ContactsAddressController(ApexPages.StandardController controller){
 
    }
       
    public List<contactwrapper> getContacts()
    {
        contactList.clear();
        for(Contact c : [SELECT Name, Title, Email, Phone, Employed_By__c, TD_Use_Account_Address__c from Contact where AccountId = :Apexpages.Currentpage().getparameters().get('Id') ORDER BY Name ASC])
        {           
            sortContacts.add(c);
            System.debug('sortContacts: ' + sortContacts);
            contactList.add(new contactwrapper(c)); 
            System.debug('contactList: ' + contactList);         
        }
       
        if (contactList != null && !contactList.IsEmpty())
        {    return contactList;     }
        {    return null;            }
    }
   
    public PageReference getSelected()
    {
        selectedContacts.clear();
        for(contactwrapper conwrapper : contactList)
        if(conwrapper.selected == true)
        selectedContacts.add(conwrapper.con);
        return null;
    }
   
    public List<Contact> GetSelectedContacts()
    {
        if(selectedContacts.size()>0)
        return selectedContacts;
        else
        return null;
    }   
   
    public class contactwrapper
    {
        public Contact con{get; set;}
        public Boolean selected {get; set;}
        public String ContactURL {get;set;}
        public contactwrapper(Contact c)
        {
            con = c;
           ;
            ContactURL = '/' + c.Id;
        }
    }
   
    //Sorting.
    public void doSort(){
        String order = 'asc';
       
        /*This checks to see if the same header was click two times in a row, if so it switches the order.*/
        if(previousSortField == sortField){
            order = 'desc';
            previousSortField = null;
        }else{
            previousSortField = sortField;
        }
      
        //To sort the table we simply need to use this one line, nice!
        superSort.sortList(sortContacts, sortField, order);
    }   
   
    // Update Contacts
    public PageReference doUpdateAddress(){
        for (Contact c : selectedContacts)
        {
            c.TD_Use_Account_Address__c = true;
        }
       
        try {
          update selectedContacts;         
          }
        catch (DmlException e) {
          ApexPages.addMessages(e);
          }
        return new PageReference('/'+ ApexPages.currentPage().getParameters().get('id'));       
    }
 }

=============================================================================================

 

Thank you!

Best Answer chosen by Andrei Kuznetsov
crop1645crop1645

andrei --

 

This is well  documented in the VF Developers Guide under the heading 'Testing Custom Controllers and Controller Extensions".  The key is that you write testmethods to execute each of your public methods one-by-one.

 

To do this, you'll need to get a handle to the TD_ContactsAddressController (that is, create an instance of it -- just as if VF called its constructor).

 

Then, you set any of your public setters to capture simulated user input.  You may need to create test SObjects first so you have something to set.

 

Then you execute each public method and test taht results are as expected (right page is returned, expected errors occur, database is updated correctly, public properties are updated as expected.

 

You may need more than one testmethod for clarity.  The testmethods can be in the same class as your TD_ContactsAddressController or can be in a separate 'test' class such as Test_TD_ContactsAddressController.