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
TechSteveTechSteve 

Conroller class development and testing code

I am trying to write my first controller class and am finding the concept tricky. I want a controller to show a list contacts from the contact standard object and filter what is shown by a boolean check form a check box. I think I have it with:

public with sharing class Assessmentcontroller {

    Public List<cContact> clientList {get; set;}

    Public List<cContact> getContacts(){
        if(clientList == null){
            clientList = new List<cContact>();
             for(Contact c : [Select name, Client_DOB__c, Contact_Ref__c From Contact where Is_client__c = True])
                clientList.add(new cContact(c));
        }
        return clientList;
    }
    public pageReference clientList(){
        List<Contact> clientList = new List<Contact>();
        for(cContact cCon : getContacts()){
            clientList.add(cCon.con);
        }
   
    system.debug('These are the clients...');
    for(Contact con : clientList){
        system.debug(con);
    }
    return null;
    }
   
    public class cContact{
        public Contact con {get; set;}
   
        public cContact(Contact c){
            con = c;
        }
    }
   
   
}

Then trying to write the test methods to cover it .

ps. is there anyone in north machester who could give me some time to cover some basics?

Thanks

Chamil MadusankaChamil Madusanka

Hi,

 

Try this,

 

@isTest
private class TestAssessmentcontroller
{
	public static testmethod void testClientList()
	{
		Contact con1 = new Contact();
		Contact con2 = new Contact();
		
		con1.LastName = 'testName1';
		con1.Client_DOB__c = DateTime.newInstance(1985, 8, 6);;
		con1.Contact_Ref__c = 'your values here';
		con1.Is_client__c = true;
		insert con1;
		
		con2.LastName = 'testName2';
		con2.Client_DOB__c = DateTime.newInstance(1985, 4, 3);;
		con2.Contact_Ref__c = 'your values here';
		con2.Is_client__c = true;
		insert con2;
		
		Assessmentcontroller AC = new Assessmentcontroller();
		Assessmentcontroller.cContact ACC = new Assessmentcontroller.cContact();
		AC.getContacts();
		AC.clientList();
		
		System.assert(con1.LastName == 'testName');
	}
}

 NOTE : Please set values for con1.Contact_Ref__c and con2.Contact_Ref__c in test class(use correct data type.  I assume that it is  Text)

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Chamil's Blog

TechSteveTechSteve

HI chamil, Sorry took a while to reply. Found development in the sandbox doesn't need the testing so been busy developing. Unfortunately this means all my code has change. The assessment controller is stand alone with no object just a page with direction and display controls. I know I am a pain but could someone help please? with comments please as I am very new. Also how do you make it change the code coverage for the actual class it is testing?

Here's my final code:

public class Assessmentcontroller {

    public String conid { get; private set; }   
    public String notclient { get; private set; }   
    public boolean Notcid { get; private set; }   
    public boolean Nocid { get; private set; }   
    public boolean Gotcid { get; private set; }
     
    public Assessmentcontroller(){
    if(ApexPages.currentPage().getParameters().get('cid')!= null){     
        conid = '?cid='+ApexPages.currentPage().getParameters().get('cid'); //assign client record number to conid
        notclient = ApexPages.currentPage().getParameters().get('cc');
            if (notclient == 'false'){
                Notcid = True;
            }
        Nocid = False;
        Gotcid = True;
        }else{
        Nocid = True;
        Gotcid = False;
        }
    }

    public String Id  { get; private set; }
   
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                Id id = ApexPages.currentPage().getParameters().get('cid');
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(               
                      [select name, Is_Client__c,Client_DOB__c, Contact_Ref__c, DNR_Authorisation_Uploaded__c from Contact where Id = :id]));
            }
            return setCon;
        }
        set;
    }
    // Initialize setCon and return a list of records     
    public List<Contact> getContacts() {
         return (List<Contact>) setCon.getRecords();
    }
}