Ritesh Kadmawala
Ritesh Kadmawala

Reputation: 743

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

Upvotes: 6

Views: 1158

Answers (1)

DDD
DDD

Reputation: 1462

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().

Upvotes: 5

Related Questions