java - How to test menu in Android with Robolectric -


I use

Robolectric source code menu to write tests for the menu in Android application Is required:.

  @Override public boolean onCreateOptionsMenu (Menu) {getMenuInflater () increased (R.menu.main, menus). Return Super. Secret Option Menu (menu); } @Override public boolean onMenuItemSelected (integer featureId, MenuItem item) {switch (item.getItemId ()) {case R.id.exit: this.finish (); break; Default: Toast.makeText (this, GetString (R.string.errMsg), Toast.LENGTH_SHORT) .show (); break; } Return super. Paramountime selected (feature ID, item); }  

Help to write test

Should the following example Under the AndroidStudio, Robolectric 3.0 is going to be a good example for anyone who is starting with the Robolectric.

  @RunWith (RobolectricGradleTestRunner.class) @Config (constant = BuildConfig.class, SDK = 19) public class MainActivityTest {@Test public void shouldCloseActivity () {MainActivity activity = Robotrik. Setup priority (main activity category); MenuEntem MenuItam = New Robomenuite (RID.exe); Activity.onOptionsItemSelected (MENUITEM); Shadow Activation Shadow Shape = Shadow Shadow (activity); AssertTrue (shadowActivity.isFinishing ()); }}  

Comments