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
bujjibujji 

Test Class for ApexPages.StandardSetController

Hi Guys,

I have a class in which i am using StandaraSetController to display the data with paginations. How to write the test class for the below method.


ApexPages.StandardSetController con = new ApexPages.StandardSetController(Database.getQueryLocator([select Id,Name From Account limit 1000]));

public List<Account> displayAccts
            {
                get
                {
                    if(con != null)
                        return (List<Account>)con.getRecords();
                    else
                        return null ;
                }
                set;
            }

Thanks,
Bujji
AshwaniAshwani
You can instantiate a standard controller by ApexPages.StandardController sc = new ApexPages.StandardController(sObject); in test class. When you create Apex class just pass instance in main class constructor.

Sample:
Account testAccount = new Account(Name='Test Company Name123');
            accounts.add(testAccount);
            insert accounts;

 ApexPages.StandardController sc = new ApexPages.StandardController(accounts);
        
        PageReference pageRef = Page.AccountPlan;
        pageRef.getParameters().put('id', String.valueOf(testAccount .Id));
        Test.setCurrentPage(pageRef);

MyStandardClass instance = new MyStandardClass(sc);