title: testing menu_main.xml (Android)
body: <p>I'm attempting to write unit tests for a very simple app.  Here is the menu_main.xml</p>
  
  <pre><code>&lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"&gt;
      &lt;item android:id="@+id/action_settings" android:title="@string/action_settings"
          android:orderInCategory="100" app:showAsAction="never" /&gt;
      &lt;item android:id="@+id/action_refresh" android:title="@string/action_refresh"
          android:orderInCategory="100" app:showAsAction="never" /&gt;
  &lt;/menu&gt;
  </code></pre>
  
  <p>App runs fine, both items show up.  MainActivity.xml can clearly find them because</p>
  
  <pre><code>@Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  </code></pre>
  
  <p>works just fine.</p>
  
  <p>I have unit tests that descend from <code>ActivityInstrumentationTestCase2</code>, one of which checks the menu_main.xml.</p>
  
  <pre><code>public void testSettingsMenuItemExists() {
      Object view = activity.findViewById(R.id.action_settings);
      assertNotNull("Settings menu item does not exist", view);
  }
  </code></pre>
  
  <p><code>R.id.action_settings</code> obviously exists because the code compiles, but when the tests run, they do not find item.  They do find elements from activity_main.xml.  I've tried getting the action bar via <code>activity.getActionBar()</code>, it is null.  I thought maybe the action bar was owned by the application, not the activity, but Applications don't even have <code>getActionBar()</code>.</p>
  
  <p>How do I find menu items so I can test them in unit tests?</p>