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
Jim Shorkey 6Jim 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
Best Answer chosen by Jim Shorkey 6
Jim Shorkey 6Jim Shorkey 6
I figured it out.

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

Anupama SamantroyAnupama Samantroy
@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();
   
    }
}
Please try the above code. We have to pass the list of sObject in the constructor of set controller.


Thanks
Anupama
Jim Shorkey 6Jim Shorkey 6
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
Jim Shorkey 6Jim Shorkey 6
I figured it out.

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

 
This was selected as the best answer
Rabbani Basha 1Rabbani Basha 1
@isTest 
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();
 }