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
azurerelishazurerelish 

Need some help to build the test class for controller

Hi Development Community, 

I am trying to build a test class for the controller I built, the controller is just pull some data from the system. However, I have tried numerous ways to build the test but got 0 coverage all the time. Any hint or help? 

Thanks for your help.

Below is my controller,
public with sharing class MDSExtension
{
    public string Object1id {get; set;}
    public string Object2id {get; set;}
    public string Object3id {get; set;}
    public Object1_c SelectedObject1{get;set;}
    public Object2_c SelectedObject2{get;set;}
    public Object3_c SelectedSD {get;set;}
    public List<Object1_c> Object1 { get;set; }
    public List<Object2_c> Object2 { get;set; }
    public List<Object3_c> Object3 { get;set; }
    public MDSExtension(ApexPages.StandardController controller)
    {
        Object1= [
            SELECT Name, RecordType.DeveloperName FROM Object1_c
            WHERE Client__c = :controller.getId() AND RecordTypeID= '0127F000000gkUkQAI'
        ];
        Object2= [
            SELECT Name, RecordType.DeveloperName  FROM Object2_c
            WHERE Client__c = :controller.getId() AND RecordTypeID= '0127F000000gkUVQAY'
        ];
        Object3= [
            SELECT Name, Client__c FROM Object3_c
            WHERE Client__c = :controller.getId() 
        ];
    }
public PageReference save(){    
return null;
 }
}

 
Manas Kanti DattaManas Kanti Datta
Hi Azurerelish,
Can you post the test class that you have written
Thanks

 
Manas Kanti DattaManas Kanti Datta
Hi Azurerelish,

1. if you want to use relatime data in test class use (seealldata=true) but that is not recommed.
2. please check the following example for visualforce page controller test class. i see no reference to the vf page in your test class,


@isTest 
public class TestStandardSetController 
{
 static testMethod void testMethod1() 
 {
 List <Account> lstAccount = new List<Account>();
 
 Account testAccount = new Account();
 testAccount.Name='Test Account' ;
 lstAccount.add(testAccount);
 Account testAccount1 = new Account();
 testAccount1.Name='Test Account11' ;
 lstAccount.add(testAccount1);

 insert  lstAccount;
 
 Test.startTest();
  Test.setCurrentPage(Page.YOUR_PAGE);
  ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(lstAccount);
  stdSetController.setSelected(lstAccount);
  YOUR_Extension ext = new YOUR_Extension(stdSetController);
 Test.stopTest();
 }
}