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
Mohmad SohelMohmad Sohel 

Test class for Custom Controller

Hi, please help on writing test class for below controller:
public class DataTableExampleController {
    public List<Contact> contactList{
        get{
            if(contactList ==Null){
                contactList = [select Account.Name, FirstName, LastName, Phone from Contact limit 10000];
            }
            return contactList;
        }
        set;
            }
        }
     Test class:
@isTest
public class Test_DataTableExampleController {
    static testMethod void testSave(){
           Test.setCurrentPage(Page.DataTablePage);
        Account acc = new Account(Name = 'Test');
        insert acc;
        Contact con = new Contact(LastName = 'Test', AccountId = acc.Id );
        insert con;
        
        DataTableExampleController dt = new DataTableExampleController ();
 //   dt.contactList = 'test';
   //     System.assertEquals(contactList, 'test');
    }
}
Thank You.
Best Answer chosen by Mohmad Sohel
Anupama SamantroyAnupama Samantroy
Hi, 
Please assert the size of the contact list.
@isTest
public class Test_DataTableExampleController {
    static testMethod void testSave(){
           Test.setCurrentPage(Page.DataTablePage);
        Account acc = new Account(Name = 'Test');
        insert acc;
        Contact con = new Contact(LastName = 'Test', AccountId = acc.Id );
        insert con;
        
        DataTableExampleController dt = new DataTableExampleController ();
        System.assertEquals(dt.contactList.size(), 1);
    }
}

All Answers

Anupama SamantroyAnupama Samantroy
Hi, 
Please assert the size of the contact list.
@isTest
public class Test_DataTableExampleController {
    static testMethod void testSave(){
           Test.setCurrentPage(Page.DataTablePage);
        Account acc = new Account(Name = 'Test');
        insert acc;
        Contact con = new Contact(LastName = 'Test', AccountId = acc.Id );
        insert con;
        
        DataTableExampleController dt = new DataTableExampleController ();
        System.assertEquals(dt.contactList.size(), 1);
    }
}
This was selected as the best answer
Mohmad SohelMohmad Sohel
Thank You for quick reply Anupama. It's giving 100% coverage.