+ Start a Discussion
Shubham Sinha 49Shubham Sinha 49 

Write a test class for Custom Controller

Hi, 
How to write a test class for below code
public class AccountSelectClassController{

    

//Our collection of the class/wrapper objects wrapAccount 
    public List<wrapContact> wrapContactList {get; set;}
    public List<contact> selectedcontacts{get;set;}
    public string searchtext1{get;set;}
     
    public void search(){
        if(wrapContactList == null) {
            wrapContactList = new List<wrapContact>();
            for(Contact a: [select Id, name, Phone from contact where lastname like :searchtext1 ]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapContactList.add(new wrapContact(a));
            }
        }
    }
 
    public void processSelected() {
    selectedcontacts = new List<contact>();
 
        for(wrapContact wrapContactObj : wrapContactList) {
            if(wrapContactObj.selected == true) {
                selectedcontacts.add(wrapContactObj.con);
            }
        }
    }
 public void clear() {
        wrapContactList.clear();
    }
 
    // 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 Account and a Boolean value
    public class wrapContact {
        public contact con {get; set;}
        public Boolean selected {get; set;}
 
        //This is the contructor method. When we create a new wrapAccount object we pass a Account that is set to the acc property. We also set the selected value to false
        public wrapContact(contact a) {
            con = a;
            selected = false;
        }
    }
}
Andrew GAndrew G
short version.
Insert a contact.
Build an instance of the controller.
Invoke a method for the controller.

Longer version - not compiled.
@isTest 
public class ControllerTestClass 
{
@Istest
	 static void searchTest() 
	 {
		Contact cont = new Contact ();
		cont.LastName = 'LastName';
//other fields as required
		insert cont;
		
		Test.StartTest(); 
			PageReference pageRef = Page.SomePageName; // Add your VF page Name here
			pageRef.getParameters().put('searchtext1', 'LastName');
			Test.setCurrentPage(pageRef);

			AccountSelectClassController accSelect = new AccountSelectClassController();
			accSelect.search();	
		Test.StopTest();
//some sort of assert 
        System.assertEquals(1, pageRef.wrapContactList.size());
	 }

Rinse repeat for other methods
Regards
Andrew
Shubham Sinha 49Shubham Sinha 49
Thank You Andrew. I covered all the methods except the wrapper class. Could you please help me to cover the wrapper class.
Andrew GAndrew G
Not a lot of experience with wrappers, but i think something like:
 
@Istest
	 static void wrapperTest() 
	 {
		Contact con = new Contact (LastName='Test');
        insert con;
		
		Test.StartTest(); 
AccountSelectClassController.wrapContact   wrapper = new AccountSelectClassController.wrapContact(con);
		Test.StopTest();
//above should do coverage
//for assertion?? perhaps
      System.assertEquals(false, wrapper.selected);
      System.assertEquals(con.Id, wrapper.con.Id);
	 }
HTH
Andrew