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
DDayDDay 

Test method for List<SelectOption>

Hello,
 
I have a method that returns a List<SelectOption> containing a list of months to be used in a menu on a VF page. I'm still new to writing Apex code and I can't figure out what the correct way to write the test method for this is. If I run tests on the following code I get this error:
 
 

System.Exception: Assertion Failed: Expected: (System.SelectOption[value="01", label="Jan", disabled="false"], System.SelectOption[value="02", label="Feb", disabled="false"], System.SelectOption[value="03", label="Mar", disabled="false"], System.SelectOption[value="04", label="Apr", disabled="false"], System.SelectOption[value="05", label="May", disabled="false"], System.SelectOption[value="06", label="Jun", disabled="false"], System.SelectOption[value="07", label="Jul", disabled="false"], System.SelectOption[value="08", label="Aug", disabled="false"], System.SelectOption[value="09", label="Sep", disabled="false"], System.SelectOption[value="10", label="Oct", disabled="false"], ...), Actual: (System.SelectOption[value="01", label="Jan", disabled="false"], System.SelectOption[value="02", label="Feb", disabled="false"], System.SelectOption[value="03", label="Mar", disabled="false"], System.SelectOption[value="04", label="Apr", disabled="false"], System.SelectOption[value="05", label="May", disabled="false"], System.SelectOption[value="06", label="Jun", disabled="false"], System.SelectOption[value="07", label="Jul", disabled="false"], System.SelectOption[value="08", label="Aug", disabled="false"], System.SelectOption[value="09", label="Sep", disabled="false"], System.SelectOption[value="10", label="Oct", disabled="false"], ...)

 

 

public class Months {

public List<SelectOption> getMonthList() {

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

options.add(new SelectOption('01', 'Jan'));

options.add(new SelectOption('02', 'Feb'));

options.add(new SelectOption('03', 'Mar'));

options.add(new SelectOption('04', 'Apr'));

options.add(new SelectOption('05', 'May'));

options.add(new SelectOption('06', 'Jun'));

options.add(new SelectOption('07', 'Jul'));

options.add(new SelectOption('08', 'Aug'));

options.add(new SelectOption('09', 'Sep'));

options.add(new SelectOption('10', 'Oct'));

options.add(new SelectOption('11', 'Nov'));

options.add(new SelectOption('12', 'Dec'));

return options;

}

 

static testMethod void testMonthList() {

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

test_options.add(new SelectOption('01', 'Jan'));

test_options.add(new SelectOption('02', 'Feb'));

test_options.add(new SelectOption('03', 'Mar'));

test_options.add(new SelectOption('04', 'Apr'));

test_options.add(new SelectOption('05', 'May'));

test_options.add(new SelectOption('06', 'Jun'));

test_options.add(new SelectOption('07', 'Jul'));

test_options.add(new SelectOption('08', 'Aug'));

test_options.add(new SelectOption('09', 'Sep'));

test_options.add(new SelectOption('10', 'Oct'));

test_options.add(new SelectOption('11', 'Nov'));

test_options.add(new SelectOption('12', 'Dec'));

 

Months my_months = new Months();

List<SelectOption> method_options = my_months.getMonthList();

 

system.assertEquals(method_options, test_options);

}

}

 

I copied/pasted the 'expected' and 'actual' responses to a text editor and they look identical to me. Is there another way I should be writing this test?
 
Thanks,
Dan 
 
 
 
 
Best Answer chosen by Admin (Salesforce Developers) 
AlsoDougAlsoDoug

Dan,

 

I don't think assertEquals works with lists/arrays/etc only basic data types. 

 

From the apex language reference doc the arguments types are AnyDataType (which I think is just String, Boolean etc).

 

My best practice on testing what you have would be make sure both lists are the same size (fail if not) and then loop checking the nth element in each list misc element against each other 

 

IE (not real code of course)


//start of test

system.assertEquals(method_options.size(), test_options.size());

if( method_options.size() == test_options.size() ){
//do a for loop
//in loop assert each data element in the select option equals the other

 


 

Sorry would have offered more but should be enough to get you started.

Message Edited by AlsoDoug on 06-30-2009 06:14 PM

All Answers

AlsoDougAlsoDoug

Dan,

 

I don't think assertEquals works with lists/arrays/etc only basic data types. 

 

From the apex language reference doc the arguments types are AnyDataType (which I think is just String, Boolean etc).

 

My best practice on testing what you have would be make sure both lists are the same size (fail if not) and then loop checking the nth element in each list misc element against each other 

 

IE (not real code of course)


//start of test

system.assertEquals(method_options.size(), test_options.size());

if( method_options.size() == test_options.size() ){
//do a for loop
//in loop assert each data element in the select option equals the other

 


 

Sorry would have offered more but should be enough to get you started.

Message Edited by AlsoDoug on 06-30-2009 06:14 PM
This was selected as the best answer
DDayDDay

That was exactly what I needed, thanks for push in the right direction! 

 

Sincerely, 

Dan 

Subhasini Bhosal 6Subhasini Bhosal 6
Any idea on how to display two tables on a vf page and compare each row of the two tables and display the output as matched or not matched on click of a button ?In the second table there should be a flag column that should be updated with true or false if any of the rows from two table match..