You need to sign in to do that
Don't have an account?
Jim Shorkey 6
Apex Testing of Standard Set Controller
Hello,
I am neew to Apex coding and am struggling with the tresting. I created a class to filter accounts by record type that returns the results correctly. I have written a test class, but I am receiving an error message that reads "Constructor not defined: [ApexPages.StandardSetController].()". I am not able to figure out or find the solution to fix this error. Below is the code from the test class:
@isTest
public class TestFilterAccountSummary {
static testMethod void myAccountFilterTest() {
Account acc = new Account(Name='Test Account');
insert acc;
Test.StartTest();
ApexPages.StandardSetController sOC = new ApexPages.StandardSetController();
sOC.setSelected(new List<Account>{acc});
FilterAccountSummary fAS = new FilterAccountSummary(sOC);
Test.StopTest();
}
}
The error is in line 9 which I have ubderlined in the code above.
I have also posted the code for the Apex class that I am testing below: (This code is working as expected, but thought it may be helpful)
public class FilterAccountSummary {
public ApexPages.StandardSetController Acc {
get {
if (Acc == null){
Acc = new
ApexPages.StandardSetController(Database.getQueryLocator(
[Select
Id,
Name,
RecordType.Name,
Industry
From Account
Where RecordType.Name In ('Standard BDAP')]));
}
return Acc;
}
set;
}
public List <Account> getAccounts(){
Return (List<Account>) Acc.getRecords();
}
}
Thank you in advance for your help,
Jim
I am neew to Apex coding and am struggling with the tresting. I created a class to filter accounts by record type that returns the results correctly. I have written a test class, but I am receiving an error message that reads "Constructor not defined: [ApexPages.StandardSetController].()". I am not able to figure out or find the solution to fix this error. Below is the code from the test class:
@isTest
public class TestFilterAccountSummary {
static testMethod void myAccountFilterTest() {
Account acc = new Account(Name='Test Account');
insert acc;
Test.StartTest();
ApexPages.StandardSetController sOC = new ApexPages.StandardSetController();
sOC.setSelected(new List<Account>{acc});
FilterAccountSummary fAS = new FilterAccountSummary(sOC);
Test.StopTest();
}
}
The error is in line 9 which I have ubderlined in the code above.
I have also posted the code for the Apex class that I am testing below: (This code is working as expected, but thought it may be helpful)
public class FilterAccountSummary {
public ApexPages.StandardSetController Acc {
get {
if (Acc == null){
Acc = new
ApexPages.StandardSetController(Database.getQueryLocator(
[Select
Id,
Name,
RecordType.Name,
Industry
From Account
Where RecordType.Name In ('Standard BDAP')]));
}
return Acc;
}
set;
}
public List <Account> getAccounts(){
Return (List<Account>) Acc.getRecords();
}
}
Thank you in advance for your help,
Jim
I changed my class to:
public class FilterAccountSummary {
public Account[] accounts {get; private set;}
public FilterAccountSummary() {
accounts = [Select
Id, Name, RecordType.Name, Industry
From Account
Where RecordType.Name In (''Standard BDAP')];
}
}
And the test class to:
@isTest
public class TestFilterAccountSummary {
static testMethod void myAccountFilterTest() {
Account acc = new Account(Name='Test Account');
insert acc;
Test.StartTest();
ApexPages.StandardController sc = new ApexPages.StandardController(acc);
FilterAccountSummary filter = new FilterAccountSummary();
Test.StopTest();
}
}
Passed with 100% code coverage.
Thank you,
Jim
All Answers
Thanks
Anupama
Thank you for your response. I tried the code, but I am receiving another error message "Constructor not defined: [FilterAccountSummary].(ApexPages.StandardSetController)". Below is the code that I used and I have unlined the line of code that the error message is referencing.
@isTest
public class TestFilterAccountSummary {
static testMethod void myAccountFilterTest() {
Account acc = new Account(Name='Test Account');
insert acc;
List<Account> lstAcc = new List<Account>();
lstAcc.add(acc);
Test.StartTest();
ApexPages.StandardSetController sOC = new ApexPages.StandardSetController(lstAcc);
FilterAccountSummary fAS = new FilterAccountSummary(sOC);
Test.StopTest();
}
}
Thank you for your time. I really appreciate your help.
Jim
I changed my class to:
public class FilterAccountSummary {
public Account[] accounts {get; private set;}
public FilterAccountSummary() {
accounts = [Select
Id, Name, RecordType.Name, Industry
From Account
Where RecordType.Name In (''Standard BDAP')];
}
}
And the test class to:
@isTest
public class TestFilterAccountSummary {
static testMethod void myAccountFilterTest() {
Account acc = new Account(Name='Test Account');
insert acc;
Test.StartTest();
ApexPages.StandardController sc = new ApexPages.StandardController(acc);
FilterAccountSummary filter = new FilterAccountSummary();
Test.StopTest();
}
}
Passed with 100% code coverage.
Thank you,
Jim
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();
}