1

How do you pick the values, in a test class, for a multi select pick list?

Visualforce:

<apex:pageBlockSection title="Licenses:" columns="1" collapsible="false">
                <apex:outputPanel >
                    <apex:selectList id="statelist"  
                                     value="{!States}" 
                                     multiselect="true" 
                                     size="10" 
                                     style="width:100%;">
                        <apex:selectOptions value="{!items}"/>
                    </apex:selectList>
                </apex:outputPanel>
            </apex:pageBlockSection>

Controller:

public String[] getStates() {
    return states;
}

public void setStates(String[] states) {
    this.states = states;
}

Test Class:

@isTest
public class test_licensingGridController {

    @testSetup static void setupTestData(){        
        //setup test data
    }

    @isTest static void viewAsBCS() {
        User uADM = [Select Id,Name FROM User WHERE Alias = 'TSTADMIN'];

        //Create Acorn Sessions and Benefits
        system.runAs(uADM){

            test.startTest();

            Test.setCurrentPage(Page.LicensingGrid);
            LicensingGridController controller = new LicensingGridController();
            controller.getTheHubs();

            //Pick states from list, don't care which ones
            controller.states(?????);



            test.stopTest();
        }
    }

}

1 Answer 1

2

I feel below will work:

List<String> statesToSet=new List<String>();
statesToSet.add('A');
statesToSet.add('B);
statesToSet.add('C');

controller.setState(statesToSet);
1
  • 1
    Slightly cleaner version: List<string> states = new list<string>{'Kansas','Missouri'}; controller.setStates(states);
    – Matt M
    Jul 19, 2016 at 17:04

You must log in to answer this question.

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