+ Start a Discussion
neil_edwards_fcca1.389781386123482E12neil_edwards_fcca1.389781386123482E12 

Wrapper Class test class - how to replicate marking a record as being selected in Test class

Hi, I'm struggling to find a way to replicate selecting a record in a visualforce page. Without somehow making the record in the test class selected nothing is being checked against the test code, Any help greatly appreciated! 
Extract from Test Class


// Set-up of Test Records here

// Set up visualforce parameters

PageReference pageRef = Page.CloneDeal;
PageRef.getParameters().put('id',Deal.id);

Test.setCurrentPage(pageRef);

// create an instance of the controller
Checkbox_class myPageCon = new Checkbox_class();



test.starttest();

mypageCon.getselected();
mypageCon.getselectedfee();
mypageCon.getselectedpay();
mypageCon.getselectediss();
mypageCon.getselectedtar();
mypageCon.getselectedaq();
myPageCon.getPayments();
myPageCon.getFees();
myPageCon.getPayments();
myPageCon.getIssuer();
myPageCon.getTargets();
myPageCon.getaq();
myPageCon.getex();
myPageCon.getDeals();



myPageCon.doClone();

test.stoptest();

e in the test class. 
Extract from Visualforce page

<apex:page controller="Checkbox_Class" Tabstyle="Deal_Protocol__c">
 <apex:stylesheet value="/sCSS/21.0/sprites/1297816277000/Theme3/def​ault/gc/versioning.css" /> 
  <apex:sectionHeader title="Clone Deal: "/>

  <apex:form id="theform" >
  <apex:PageBlock >
   <apex:PageBlockButtons >
   
      <apex:commandButton action="{!doClone}" value="Clone this Deal" /> 
      <apex:commandButton action="{!CloseFrm}" value="Cancel & Return" />
    </apex:PageBlockButtons>
 
            <apex:pageBlockSection Title="Transaction to be Cloned - Check box to select item(s) to clone">
                <apex:dataTable value="{!deals}" var="a" columnswidth="50px,50px" cellpadding="4" border="1">
                    <apex:column >
                        <apex:facet name="header"> <apex:inputCheckbox >
                            <apex:actionSupport event="onclick" action="{!GetSelected}" onsubmit="checkAll(this)" rerender="Selected_PBS"/>
                            </apex:inputCheckbox>
                        </apex:facet>
                        <apex:inputCheckbox value="{!a.selected}" id="checkedone">
                        <apex:actionSupport event="onclick" action="{!GetSelected}" rerender="Selected_PBS"/></apex:inputCheckbox>
                    </apex:column>
                    <apex:column headervalue="Deal Name" value="{!a.acc.Name}" />
                    <apex:column headervalue="Deal Type" value="{!a.acc.Agreement_Type__c}" />
                  
                </apex:dataTable>
            </apex:pageBlockSection>

<apex:pageBlockSection Title="List of Fees that can be cloned (does not include deleted fees)">
                <apex:dataTable value="{!fees}" var="f" columnswidth="50px,50px" cellpadding="4" border="1">
                    <apex:column >
                    
                        <apex:inputCheckbox value="{!f.selectedf}" id="checkedone">
                        <apex:actionSupport event="onclick" action="{!GetSelectedfee}" rerender="Selected_PBS"/></apex:inputCheckbox>
                    </apex:column>
                    <apex:column headervalue="Fee Name" value="{!f.fee.Name}" />
                    <apex:column headervalue="Playbook Category" value="{!f.fee.Playbook_Category__c}" />
                    <apex:column headervalue="Playbook Ref" value="{!f.fee.Playbook_Ref__c}" /> 
                                     
                </apex:dataTable>
            </apex:pageBlockSection>


Extract from Apex class

public class Checkbox_Class
{

    String DealID = ApexPages.currentPage().getParameters().get('id');

    List<DealWrapper> DealList = new List<DealWrapper>();
    List<FeeWrapper> FeeList = new List<FeeWrapper>();
    
     
    List<Deal_protocol__c> selectedDeal = new List<Deal_protocol__c>();
    List<Fees__c> selectedFee = new List<Fees__c>();
    
     
  
    public List<DealWrapper> getDeals()
    {
        for(Deal_protocol__c a : [select Id, Name,Agreement_Type__c from  Deal_protocol__c where id=:dealid limit 1])
        DealList.add(new DealWrapper(a));
        return DealList;
    }

  public List<FeeWrapper> getFees()
    {
        for(Fees__c f : [select Id, Name,Playbook_Category__c,Playbook_Ref__c from Fees__c where Deal_protocol_ref__c =:dealid and deleted__c=false order by playbook_category__c ASC])
        FeeList.add(new FeeWrapper(f));
        return FeeList;
    }

 public PageReference getSelected()
    {
        selectedDeal.clear();
        for(DealWrapper accwrapper : DealList)
        if(accwrapper.selected == true)
        selectedDeal.add(accwrapper.acc);
        return null;
    }
    
      public PageReference getSelectedFee()
    {
        selectedfee.clear();
        for(FeeWrapper fwrapper : FeeList)
        if(fwrapper.selectedf == true)
        selectedfee.add(fwrapper.fee);
        return null;
    }

    public class DealWrapper
    {
        public Deal_protocol__c acc{get; set;}
        public Boolean selected {get; set;}
        public DealWrapper(Deal_protocol__c a)
        {
            acc = a;
            selected = false;
        }
    }
    
     public class FeeWrapper
    {
        public Fees__c fee{get; set;}
        public Boolean selectedf {get; set;}
        public FeeWrapper(fees__c f)
        {
            fee = f;
            selectedf = false;
        }
     }


Best Answer chosen by neil_edwards_fcca1.389781386123482E12
Eli Flores, SFDC DevEli Flores, SFDC Dev
Oh, I misunderstood... You manipulate the members  or the getters of the Controller class directly. After it's all loaded up do stuff like. 

mypageCon.DealList.get(0).selected = true;//select the first item using the members

myPageCon.getDeals.get(0).selected = true;//select the first item using the getter

All Answers

Eli Flores, SFDC DevEli Flores, SFDC Dev
You will have to manually construct the test data in your test class. So you'll have to do stuff like 

Deal_c testDeal = new Deal__c();
testDeal.field = yadda;
...
insert testDeal;

Deal_Protocol__c testProtocol = new Deal_Protocol__c();
...
testProtocol.Deal_protocol_ref__c = testDeal.id;
etc, etc, etc

don't worry about it being inserted into your production. The testing protocol is smart enough to to carve out a "separate" space for test record and delete them after the test is done. It's great because you can set up all the appropriate scenarios but it's tedious in that you have to set them up by hand. 

You can also load test data from csvs you store as static resources using 
Test.loadData();
You can learn a bit more on this here

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_load_data.htmI

I favor doing it code and creating some factory classes to help create objects with common settings. That one you only have one spot to see what the parameters of the test are. 
neil_edwards_fcca1.389781386123482E12neil_edwards_fcca1.389781386123482E12
Thanks - but that wasnt my question. The test records have all been created (just excluded from the code extract).  The visualforce page you see has a checkbox to select which records you want to clone. I need a way to replicate setting the 'selected' box to true in the test class as at the moment calling the 'getFees' method for example doesnt flag anything as selected=true and therefore nothing is tested as the size of the lists are zero.  How can I set the 'selected' flag to true in the test class??

Thanks
Eli Flores, SFDC DevEli Flores, SFDC Dev
Oh, I misunderstood... You manipulate the members  or the getters of the Controller class directly. After it's all loaded up do stuff like. 

mypageCon.DealList.get(0).selected = true;//select the first item using the members

myPageCon.getDeals.get(0).selected = true;//select the first item using the getter
This was selected as the best answer
neil_edwards_fcca1.389781386123482E12neil_edwards_fcca1.389781386123482E12
Perfect. This got me to the right place. For completeness for others I needed to do a couple of other things. (1) add the @TestVisible before all of my list list variables ( I think you could also use Public instead). The test class couldn't read the lists without this, and (2) call the list first ( see attached code extract) before changing the selected status.

Thanks very much DevArrah for your prompt reply
myPagecon.FeeList = myPageCon.getFees();
myPagecon.FeeList.get(0).selectedf = true;
mypageCon.getselectedfee();