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
Abhilash DaslalAbhilash Daslal 

Test class for the following controller class

Hi Guys,
Please give the test class for the following test class

public class ClsAccountController{
public contact con{get; set;}
public List<Account> AccountList{get; set;}
public Account acc{get;set;}
public id conID{get; set;}

public ClsAccountController(){
}

public List<Account> getConttacts(){
conID=ApexPages.currentPage().getParameters().get('id');
AccountList=new List<Account>();
AccountList=[Select Id,Name,Phone from Account where Contact=:conID];

return AccountList;
          }

}

Thanks in advance,
Abhilash
Best Answer chosen by Abhilash Daslal
Raj VakatiRaj Vakati
@isTest
private class ClsAccountController_Test {
    private static testmethod void testStdCntrl(){
        Account testAccount = new Account(Name='Test Company Name123');
        insert testAccount;
        Contact con  = new Contact();
        con.LastName='Demo' ; 
        con.Email='demo@demo.com';
        insert con;
        
        ClsAccountController cls = new ClsAccountController();
        cls.getConttacts();
        
        
    }
    
    
}
 
public class ClsAccountController{
    public contact con{get; set;}
    public List<Account> AccountList{get; set;}
    public Account acc{get;set;}
    public id conID{get; set;}
    
    public ClsAccountController(){
    }
    
    public List<Account> getConttacts(){
        conID=ApexPages.currentPage().getParameters().get('id');
        AccountList=new List<Account>();
        AccountList=[Select Id,Name,Phone from Account where Id=:conID];
        return AccountList;
    }
    
}

 

All Answers

Raj VakatiRaj Vakati
@isTest
private class ClsAccountController_Test {
    private static testmethod void testStdCntrl(){
        Account testAccount = new Account(Name='Test Company Name123');
        insert testAccount;
        Contact con  = new Contact();
        con.LastName='Demo' ; 
        con.Email='demo@demo.com';
        insert con;
        
        ClsAccountController cls = new ClsAccountController();
        cls.getConttacts();
        
        
    }
    
    
}
 
public class ClsAccountController{
    public contact con{get; set;}
    public List<Account> AccountList{get; set;}
    public Account acc{get;set;}
    public id conID{get; set;}
    
    public ClsAccountController(){
    }
    
    public List<Account> getConttacts(){
        conID=ApexPages.currentPage().getParameters().get('id');
        AccountList=new List<Account>();
        AccountList=[Select Id,Name,Phone from Account where Id=:conID];
        return AccountList;
    }
    
}

 
This was selected as the best answer
Abhilash DaslalAbhilash Daslal
Hi Raj V,
Thanks for your reply.I was wondering if custom objects were used instead of Standard objects.SUppose my custom objetcs are myCustomObject1__c and myCustomObject2__c instead of Account and contact.How will my code change

How can can i write cls.getConttacts();

Will it be cls.getmyCustomObject1__c???

Thanks,
Abhilash
Raj VakatiRaj Vakati
Method name you can keep it any meaningful name 
 
public class ClsAccountController{
    public myCustomObject2__c  con{get; set;}
    public List<myCustomObject1__c > AccountList{get; set;}
    public myCustomObject1__c  acc{get;set;}
    public id conID{get; set;}
    
    public ClsAccountController(){
    }
    
    public List<Account> getConttacts(){
        conID=ApexPages.currentPage().getParameters().get('id');
        AccountList=new List<myCustomObject1__c >();
        AccountList=[Select Id,Name,Phone from myCustomObject1__c  where Id=:conID];
        return AccountList;
    }
    
}


 
Abhilash DaslalAbhilash Daslal
Hi Raj V,
I meant

ClsAccountController cls = new ClsAccountController(); cls.getConttacts();
cls.getConttacts();

In the above,I want to know if cls.getmyObject__c is possible if i had used custom object??