0

How to get the (List)SetCon.getRecords() in test class which is used in pagination.can anyone explain me exactly what it means?

      public class customcontroller{

        public Map<Id,wrapperlst> mplg{get;set;}

        public List<Wrapper> wrapperlst{
                      get{

                          List<Wrapper> wrap = new List<Wrapper>();

                            if(maplg.keySet()!=null){
                                for (X__c lg : (List<X__c>)SetCon.getRecords() ){

                                     if (maplg.get(lg.Id)!=null){
                                         wrap.add(mmaplg.get(lg.Id));
                                     } 
                                } 
                            }

                          return wrap;
                      }
                      set;

             }     

   public class wrapper{

       public X__C xc{get;set;}
       public boolean selected {get;set;}

      public wrapper(X__c xc1 , boolean selected){
        xc=xc1;
        selected = true;
}


    }
}

unable to cover the code in test class in for loop

for (X__c lg : (List<X__c>)SetCon.getRecords() ){

                                     if (maplg.get(lg.Id)!=null){
                                         wrap.add(mmaplg.get(lg.Id));
                                     } 

I have written the code in test class :

PageReference pageRef = Page.PAGE_NAME; 
        Test.setCurrentPage(pageRef);
        ApexPages.StandardSetController Con = new ApexPages.StandardSetController(lIST<X__x>);

2 Answers 2

0

Try this test method. As you did not provide complete code modify the test method as necessary.

@isTest
public class sample_test
{
   static testMethod void sampletestmethod()
   {
        //----Insert records
        //----Here your object is X__c
        //----Pass inserted List<X__c> listXs to standardsetcontroller

        Test.startTest();
        Test.setCurrentPage(Page.VfpageName);
        ApexPages.StandardSetController sc = new ApexPages.StandardSetController(listXs);
        customcontroller controller = new customcontroller(sc);

        //----Populate values in maplg
        //----Then verify
        System.assert(!controller.wrapperlst.isEmpty());
        Test.stopTest();
    }
}
4
  • If it is a custom controller how can we put 'ApexPages.StandardSetController sc' to controller .directly can we add it? Jun 1, 2017 at 13:23
  • @sfdclearner Yes. You can pass sc directly. See my updated code.
    – Reshma
    Jun 2, 2017 at 3:09
  • @Reshma- how can we pass SC to controller other than mentioned above because if we pass to the SC to controller it throws error as constructor not defined. Jun 7, 2017 at 17:56
  • @sfdclearner You should create a constructor in controller like customcontroller(ApexPages.StandardSetController controller){}
    – Reshma
    Jun 8, 2017 at 7:06
1

you can use the following example of code to set that records

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();
2
  • can you explain what does this means - (List<X__c>)SetCon.getRecords() and how to get this list? Jun 1, 2017 at 12:27
  • X__c is a name of your custom object. to populate this list you need to use the next line of my post: ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(lstXCustomObjects); and then result of (List<X__c>)SetCon.getRecords() will be lstXCustomObjects Jun 1, 2017 at 12:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .