I have a custom controller. There is a dropdown on the VF page and a button. Once, I select an option and click on the button, the value selected is displayed in the outputPanel.

public class SelectedValues1 {
    public List<SelectOption> countries = new List<SelectOption>();
    public String country{get; set;}

    public List<SelectOption> getCountries() {
        return countries;

    public SelectedValues1() {
        countries.add(new SelectOption('US','US'));
        countries.add(new SelectOption('CANADA','Canada'));
        countries.add(new SelectOption('MEXICO','Mexico'));
        countries.add(new SelectOption('INDIA','India'));

VF Page:

<apex:page controller="SelectedValues1">
        <apex:pageBlock >
            <apex:pageBlockSection columns="1">

                <apex:selectList value="{!country}" size="1">
                    <apex:selectOptions value="{!countries}" />

                <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>

     <apex:outputPanel id="out">
          <apex:actionstatus id="status" startText="testing...">
             <apex:facet name="stop">
                  <p>Country selected:</p>
                 <apex:outputText value="{!country}"/>

I want to write a simple test case to simply verify that the value from selected in the SelectList. How do I do that?


Take a look at Salesforce's recommendations for UI Testing. I started writing up an answer and then realized most of what I want to say is there.

As a best practice, custom Visualforce controllers should be covered by Apex unit tests, and the Visualforce pages by UI integration tests as well. For UI integration test, Selenium is an excellent choice.

Unit Tests

Here is what you can do in a Unit Test:

SelectedValues1 controller = new SelectedValues1();
controller.country = controller.getCountries()[0];
system.assertEquals('US', controller.country);

Not very informative. It certainly tells you nothing about what the UI does with this information. But that's not the purpose of Unit Tests.

UI Integration Tests

This strategy is what you need. Whether or not you automate this testing process via Selenium, you need to actually use the UI and demonstrate that it behaves as expected. The only way to actually see if the front-end interacts with the back-end as you expect is to view the behavior in browser.


You need pass value for a country String variable in a test class, assuming user would select one of the value from a drop down list.

private class SelectedValues1TestClass {
    static testMethod void SelectedValues1Method1() {
      SelectedValues1  ctrl = new SelectedValues1 ();
      ctrl.country = 'US';
      System.assertEquals(ctrl.country, 'US');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.