Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Robolectric select a particular spinner item

I am new to Robolectric and am at a roadblock. I have some custom event that fires on selection of a item in spinner and i want to test that using robolectric. I saw that ShadowSpinner class provides helper function like stateSpinner.clickFirstItemContainingText to click a particular item. I populate my spinner with proper values which i test printing out each item as

 for (int i = 0; i < spinner_items.length; ++i) {
            spinner_items[i] = (String) spinner.getAdapter().getItem(i);
        }
 System.out.println("Spinner Items "
                + Arrays.asList(spinner_items).toString());

However when i use clickFirstItemContainingText with one of the po pulated values, i get a IllegalArgumentException. No item found containing test which is strange as above code shows that particular value do exists.

Any help would be greatly appreciated

like image 563
Ritesh Kadmawala Avatar asked May 18 '13 13:05

Ritesh Kadmawala


1 Answers

I couldn't get Robolectric to work with spinners. The problem is that the views for the list-items in the spinner won't exist apart from the one selected. This can be seen as spinner.getChildCount() will always return 1. Just call spinner.setSelection().

like image 133
DDD Avatar answered Oct 26 '22 16:10

DDD