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
bodhibodhi 

visualforce unit test for selectoption

I have a fairly simple selectoption that I use on a VF page. How do I create a unit test for it:

 

 

   public List<SelectOption> getLocationItems() {

    List<SelectOption> options = new List<SelectOption>();

       options.add(new SelectOption('On-Phone','On-Phone'));

       options.add(new SelectOption('In-Person','In-Person'));

       return options;

   }

 

 

   public List<SelectOption> getAE_LocationItems() {
    List<SelectOption> options = new List<SelectOption>();
       options.add(new SelectOption('On-Phone','On-Phone'));
       options.add(new SelectOption('In-Person','In-Person'));
       return options;
   }public List<SelectOption> getAE_LocationItems() {     List<SelectOption> options = new List<SelectOption>();        options.add(new SelectOption('On-Phone','On-Phone'));        options.add(new SelectOption('In-Person','In-Person'));        return options;    }

 

Best Answer chosen by Admin (Salesforce Developers) 
Jeremy.NottinghJeremy.Nottingh

This will work for you, just put it inside a proper test class and specify the name of the class being tested unless this is inside the class with your production code. I.e. replace "getLocationItems()" with "[nameofclass].getLocationItems()".

static testmethod void testOptionList()
{
   test.startTest();
      list<SelectOption> testoptions = getLocationItems();
   test.stopTest();

   system.assertEquals(2,testoptions.size());
   //do other asserts here as you like

}

 

Does that work?

 

Jeremy

All Answers

Jeremy.NottinghJeremy.Nottingh

This will work for you, just put it inside a proper test class and specify the name of the class being tested unless this is inside the class with your production code. I.e. replace "getLocationItems()" with "[nameofclass].getLocationItems()".

static testmethod void testOptionList()
{
   test.startTest();
      list<SelectOption> testoptions = getLocationItems();
   test.stopTest();

   system.assertEquals(2,testoptions.size());
   //do other asserts here as you like

}

 

Does that work?

 

Jeremy

This was selected as the best answer
bodhibodhi

I appreciate the prompt reply! That worked! I had already created a record as "a", so the final working result was:

 

list<SelectOption> testoptions = a.getLocationItems();

 

However, since no good deed goes unpunished ;), I'm going to throw one more part of the puzzle out to ask for assistance in creating the test class for:

 

 

   public string getAE_Location() {
       return AE_Location;
   }
   public void setAE_Location(string AE_Location) {
       this.AE_Location = AE_Location;
   }public string getAE_Location() {       

 

   public string get_Location() {

       return Location;

   }

   public void set_Location(string Location) {

       this.Location = Location;

   }

 

 

bob_buzzardbob_buzzard

Assuming that these are also methods on the record a:

 

 

 

String location='test_loc';
a.set_Location(location);
System.assertEquals(a.get_Location(), location);

 

 

bodhibodhi

Worked perfectly. Thanks!