espresso dynamic spinner testing


I am trying to test a dynamically generated Spinner. I am able to click on the spinner but then I need to select an option from the list with a given text that is shown (I found out from hierarchyviewer that a PopupWindow is shown but am unable to get to the required text which is offscreen). The spinner uses an ArrayAdapter of custom objects (code below),

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        Spinner mySpinner = (Spinner) findViewById(;
        List<MyValue> list = Arrays.asList(
                new MyValue("One", "1"),
                new MyValue("Two", "2"),
                new MyValue("Three", "3"),
                new MyValue("Four", "4"),
                new MyValue("Five", "5"),
                new MyValue("Six", "6"),
                new MyValue("Seven", "7"),
                new MyValue("Eight", "8"),
                new MyValue("Nine", "9"),
                new MyValue("Ten", "10"),
                new MyValue("Eleven", "11"),
                new MyValue("Twelve", "12"),
                new MyValue("Thirteen", "13"),
                new MyValue("Fourteen", "14"),
                new MyValue("Fifteen", "15"),
                new MyValue("Sixteen", "16"),
                new MyValue("Seventeen", "17"),
                new MyValue("Eighteen", "18"),
                new MyValue("Nineteen", "19"),
                new MyValue("Twenty", "20"),
                new MyValue("Twenty One", "21")

        final ArrayAdapter<MyValue> adapter = new ArrayAdapter<>(this,
        mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.d("MySpinnerTest", "current = " + adapter.getItem(position));
            public void onNothingSelected(AdapterView<?> parent) {}

    public class MyValue {
        String name;
        String code;

        public MyValue(String name, String code) {
   = name;
            this.code = code;

        public String toString() {
            return name;

I tried with onData(withSpinnerText("Twenty")).perform(click()) but get a PerformException and I tried to find out how else to match the correct view based on a given text, in the spinner popupwindow but could not figure out how to do this.

Any help would be greatly appreciated.



It seems your Spinner has no dropdown view, before mySpinner.setAdapter(adapter);

Please add this line :


This video can help you solving your question :)
By: admin