title: Instrument testing spinners using a custom arrayAdapter with Espresso
body: <p>So I'm trying to write some instrument tests for an activity that has some spinners in it. These spinners are using custom arrayAdapters so that I can plug them into ArrayLists of some model classes.</p>
  
  <p>The adapter for one spinner:</p>
  
  <pre><code>/**
  * Custom spinner adapter for car selection.
  *
  * Source: http://stackoverflow.com/questions/1625249/android-how-to-bind-spinner-to-custom-object-list
  */
  
  public class MyCarSpinnerAdapter extends ArrayAdapter&lt;Car&gt; {
  
  // Your sent context
  private Context context;
  // Your custom values for the spinner (User)
  private ArrayList&lt;Car&gt; values;
  
  public MyCarSpinnerAdapter(
          Context context,
          int textViewResourceId,
          ArrayList&lt;Car&gt; values
  ) {
      super(context, textViewResourceId, values);
      this.context = context;
      this.values = values;
  }
  
  public int getCount(){
      return values.size();
  }
  
  public Car getItem(int position){
      return values.get(position);
  }
  
  // And the "magic" goes here
  // This is for the "passive" state of the spinner
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      // I created a dynamic TextView here, but you can reference
      // your own custom layout for each spinner item
      TextView label = new TextView(this.context);
      label.setTextColor(Color.BLACK);
      // Then you can get the current item using the values array
      // (Users array) and the current position. You can NOW reference
      // each method you has created in your bean object (User class)
      label.setText(
              values.get(position).getManufacturer()
                      + " "
                      + this.values.get(position).getModel()
      );
  
      // And finally return your dynamic (or custom) view for each spinner item
      return label;
  }
  
  // And here is when the "chooser" is popped up
  // Normally is the same view, but you can customize it if you want
  @Override
  public View getDropDownView(int position, View convertView,
                              ViewGroup parent) {
      TextView label = new TextView(context);
      label.setTextColor(Color.BLACK);
      label.setText(
              this.values.get(position).getManufacturer()
                      + " "
                      + this.values.get(position).getModel()
      );
  
      return label;
  }
  
  @Override
  public final String toString() {
      return "this is the my car spinner adapter string";
  }
  }
  </code></pre>
  
  <p>The test:</p>
  
  <pre><code>/**
   * Initial values testing.
   */
  @Test
  public void initialSettingsTest() {
      ViewInteraction carSpinner = onView(withId(R.id.MyCarActivity_ManufacturerSpinner));
      ViewInteraction batSpinner = onView(withId(R.id.MyCarActivity_BatterySpinner));
      ViewInteraction tireSpinner = onView(withId(R.id.MyCarActivity_TireSpinner));
  
      carSpinner.check(matches(withSpinnerText("Ford Magic")));
      batSpinner.check(matches(withSpinnerText("10.0 kwh")));
      tireSpinner.check(matches(withSpinnerText("1.0 roll")));
  }
  </code></pre>
  
  <p>The result:</p>
  
  <pre><code>android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "Ford Magic"' doesn't match the selected view.
  Expected: with text: is "Ford Magic"
  Got: "AppCompatSpinner{id=2131492994, res-name=MyCarActivity_ManufacturerSpinner, visibility=VISIBLE, width=618, height=72, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=462.0, y=147.0, child-count=1}"
  </code></pre>
  
  <p>What's causing this? How can I test that the values in the spinner are what I expect them to be?</p>