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
eswarsfeswarsf 

Writing test method for a constructor

Hi,

If we have constructor in a class,Kindly let me know how can we write code to cover that constructor in test method?

 

Thanks ,

Eswar.

Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

If you have a controller with simple constructor like this :

 

public class MyClass
{
	public MyClass()
	{
		//Do Something
	}
}

 Then you can write this test class :

 

@isTest
private class TestMyClass{
    @isTest
    private static void testClass()
    {
	MyClass controller = new MyClass() ;
    }
}

 

But if you have used standard controller like this :

 

public class MyAccountClass
{
	//Class Constructor        
        public MyAccountClass(ApexPages.StandardController controller)
	{
		//Do Something
	}
}

 

So you can test it like this :

 

@isTest
private class TestMyClass{
    @isTest
    private static void testClass()
    {
	//Standard controller of Account is just an example
	//Create a new instance of Account
	Account acc = new Account(Name = 'TestAccountName');

	//Insert the object virtually
	insert acc;

	//Create a new instance of standard controller
	ApexPages.StandardController sc = new ApexPages.standardController(acc);

	MyAccountClass controller = new MyAccountClass(sc);
    }
}

 

Let me know if this doesn't resolves your problem.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

All Answers

kiranmutturukiranmutturu

just create an object fro that class..in the test class

Ankit AroraAnkit Arora

If you have a controller with simple constructor like this :

 

public class MyClass
{
	public MyClass()
	{
		//Do Something
	}
}

 Then you can write this test class :

 

@isTest
private class TestMyClass{
    @isTest
    private static void testClass()
    {
	MyClass controller = new MyClass() ;
    }
}

 

But if you have used standard controller like this :

 

public class MyAccountClass
{
	//Class Constructor        
        public MyAccountClass(ApexPages.StandardController controller)
	{
		//Do Something
	}
}

 

So you can test it like this :

 

@isTest
private class TestMyClass{
    @isTest
    private static void testClass()
    {
	//Standard controller of Account is just an example
	//Create a new instance of Account
	Account acc = new Account(Name = 'TestAccountName');

	//Insert the object virtually
	insert acc;

	//Create a new instance of standard controller
	ApexPages.StandardController sc = new ApexPages.standardController(acc);

	MyAccountClass controller = new MyAccountClass(sc);
    }
}

 

Let me know if this doesn't resolves your problem.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

This was selected as the best answer
eswarsfeswarsf

Hi Ankit,

Thanks for your reply.Your answer resolved my problem.

I have seen you specified 'Private' keyword for test class and test method.

Kindly let me know what is the significance of 'Private' keyword here,also what will happen if I mention 'Public' keyword for test class and test method.

 

Thanks,

Eswar.

Ankit AroraAnkit Arora

Classes defined with the isTest annotation do not count against your organization limit of 2 MB for all Apex code. Classes annotated with isTest must be declared as private. They cannot be interfaces or enums either.

 

So if your problem is resolved then please mark it as solution, so others may take the same benefit.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Jancy MaryJancy Mary
Hi Ankit,

Can you help me on this, I wrote a controller to show up on Dashboard and called it on VF page. It works like champ, but I need your help in writing a Test Class for the same. 
public class plcmtLineChartController {  
    
    public static String setActiveAcc { get; set; }
    public static integer count=0;
    
        public List<Data> getData() {    
        return plcmtLineChartController.allPlcmt();
        }
              
        @RemoteAction
        public static List<Data> getRemoteData() {
        return plcmtLineChartController.allPlcmt();
        }
     
        public static List<Data> allPlcmt(){       
        List<Data> data = new List<Data>();
        
        AggregateResult[] resultSet = [SELECT COUNT(Id) cid, rnm__Employer__c pid from Placement__c WHERE Start_Date__c= LAST_N_DAYS:180 AND (Employer__r.OwnerId= 'passed-userId' OR Account_Manager__c= 'passed-userId' OR Employer__r.BDM__c= 'passed-userId') GROUP BY Employer__c];
                        
            for(AggregateResult  ar: resultSet )
            {                                          
                count+=(integer)ar.get('cid');                      
                system.debug(count);                       
                data.add(new data(5, count));
            }
                                          
        return data;      
        }
    
        // Wrapper class
    public class Data{
    public Integer Target { get; set; }             
    public Integer childCount { get; set; }
                                
        public Data(Integer Target, Integer childCount) {                           
              this.Target = Target;                     
              this.childCount = count;            
        }
    }
}


Above is the Controller for which I need help to define Test Class.


Thanks in Advance:-)

Saket Ranjan 3Saket Ranjan 3
if apex class is  ::

public class Accountsearch {
    public Accountsearch() {
       }
searchbox1(String){
//your code
}
}

then in the test class ::

    Test.startTest();
        Accountsearch controller =new Accountsearch(); // this line will cover the constructor part of apex  while testing to give you 100% result
       Accountsearch.searchbox1('XYZ');       
        //other part of testing code
    Test.startTest();
Navnath Mohite 2Navnath Mohite 2
@jancy mary do you got ans for same... searching solution which you searched before