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.
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 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.
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.
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.
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();
If you have a controller with simple constructor like this :
Then you can write this test class :
But if you have used standard controller like this :
So you can test it like this :
Let me know if this doesn't resolves your problem.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
All Answers
just create an object fro that class..in the test class
If you have a controller with simple constructor like this :
Then you can write this test class :
But if you have used standard controller like this :
So you can test it like this :
Let me know if this doesn't resolves your problem.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
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.
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
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.
Above is the Controller for which I need help to define Test Class.
Thanks in Advance:-)
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();