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
Suman BonthalaSuman Bonthala 

how to write test for custom controller


public class controllerActionSupport{

    public string accid {get; set;}
    public List<Contact> SelectedAcc{get;set;}
    
    public list<selectoption> getaccountnames() {
        
        list<selectoption> accoptions = new list<selectoption>();
        accoptions.add(new selectoption('None', '--Select Account--'));
        for (account acc : [select id, name from account]){
            accoptions.add(new selectoption(acc.id, acc.name));
        }  
        return accoptions;
    } 
    
    public void  getDetails() {
        
        SelectedAcc = new List<Contact>();
        SelectedAcc = [SELECT id, FirstName, Email FROM Contact WHERE AccountId=:accid ];
        
    }
}
Dushyant SonwarDushyant Sonwar
Hi Suman,

Below will cover your class.
@isTest
public class controllerActionSupportTest{
	public static void unitTest(){
		Account accObj = new Account(name = 'Test Account ');
		insert accObj;
		
		Contact conObj = new Contact(firstName = 'TestFname' , lastname ='TestLname');
		conObj.accountId = accObj.Id;
		insert conObj;
		
		controllerActionSupport controllerObj = new controllerActionSupport();
		controllerObj.getaccountnames();
		controllerObj.accid = accObj.id;
		controllerObj.getDetails();
	}
}

I would suggest you to go through these links to understand more about the testclasses.

http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
http://www.sfdcpoint.com/salesforce/test-class-with-example-salesforce/
 
Dushyant SonwarDushyant Sonwar
Your aim should not be covering the class, it should be testing the positive and negative testcases of your controller.
Hope you would go through the above mentioned links , to learn about the testclasses :)
Deepali KulshresthaDeepali Kulshrestha
Hi Suman,

We make some changes in your custom-controller and write the test class for the controller and it covers 100% test coverage in my ORG.
Try the following class and test-class it may be helpful for you:
        
Custom Controller:
public class controllerActionSupport{
    public static string accid {get; set;}
    public static List<Contact> SelectedAcc{get;set;}
    public static list<selectoption> getaccountnames() {
        list<selectoption> accoptions = new list<selectoption>();
        accoptions.add(new selectoption('None', '--Select Account--'));
        for (account acc : [select id, name from account]){
            accoptions.add(new selectoption(acc.id, acc.name));
        }  
        return accoptions;
    } 
    public static void  getDetails() {
        SelectedAcc = new List<Contact>();
        SelectedAcc = [SELECT id, FirstName, Email FROM Contact WHERE AccountId=:accid ];
    }
}
Test-Class:
@IsTest
public class controllerActionSupport_Test {
    @IsTest
    public static void controllerMethod(){
        Account acc=new Account();
        acc.Name='TestAccount';
        insert acc;
        Contact con=new Contact();
        con.AccountId=acc.Id;
        con.LastName='TestContact';
        con.FirstName='FirstName';
        con.Email='test@gmail.com';
        insert con;
        controllerActionSupport.getaccountnames();
        controllerActionSupport.getDetails();
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Ajay K DubediAjay K Dubedi
Hi Suman,
Try this test class:
@IsTest
public class controllerActionSupport_Test {
    @IsTest
    public static void controllerMethod() {
        Account acc = new Account();
        acc.Name = 'Test';
        insert acc;
        System.assertNotEquals(acc.Id, null,'Assertion Failed : Account Not Inserted');
        
        Contact con=new Contact();
        con.AccountId = acc.Id;
        con.LastName ='Test';
        con.FirstName = 'Test';
        con.Email = 'test@gmail.com';
        insert con;
        System.assertNotEquals(con.Id, null,'Assertion Failed : Contact Not Inserted');
        
        test.startTest();
        controllerActionSupport cobj = new controllerActionSupport();
        list<selectoption> listSelectOption = cobj.getaccountnames();
        System.assertNotEquals(listSelectOption.size(), 0,'List null');
        cobj.getDetails();
        test.stopTest();
    }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi