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
Connor Small 5Connor Small 5 

Help on Custom Controller Test Class

I have a fairly simple controller that queries a record and references values from the record in order to render other VF pages.

How can I write a test class for this Controller?
public class MyController {
    
 public Account account  { get; set; }
        
        public MyController() {
        account = [SELECT Id, Name, Billing_Tier__c, Primary_Contact_Full_Name__C,Secondary_Contact_Full_Name__c,One_Line_Address__c FROM Account
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

	}
}

 
Best Answer chosen by Connor Small 5
Maharajan CMaharajan C
Hi Connor,

Try the below test class:
 
@istest
public class MyControllerTest {
    static testmethod void testMyController(){
        
        Account acc = new Account();
        acc.Name = 'Test Account';
		One_Line_Address__c  = 'Test Address';
		//Add if there is any other required fields to create the Account Record. 
		//If there is any Lookup fields then first insert  those records and then refer value in Account record.
        insert acc;
        
        Test.StartTest();
        ApexPages.currentPage().getParameters().put('id', String.valueOf(acc.Id));
        MyController mc = new MyController();
        Test.StopTest();
    }
}

Thanks,
Maharajan.C