Robolectric - 如何模拟com.actionbarsherlock.view.MenuItem?

Ser*_*ici 3 android mocking robolectric actionbarsherlock

我正在尝试使用Robolectric为使用SherlockActionBar的应用程序编写测试.如果选择了一个应用程序,我需要测试应用程序是否正确MenuItem,但是android.view.MenuItem当应用程序使用该方法时,Robolectric lib仅提供模拟onOptionItemSelected(com.actiombarsherlock.view.MenuItem).

所以我的问题是:

  • 可能有可用性来嘲笑com.actionbarsherlock.view.MenuItem

  • 或者是变通方法还是什么?

提前致谢...

Ser*_*ici 5

所以......因为没有更优雅的方式来嘲笑com.actionbarsherlock.view.MenuItem我这样做了:

  • 制作了我自己的课程 com.actionbarsherlock.view.MenuItem
  • 在我的mock类中为itemId添加了一个int字段.
  • MenuItem界面中的其他方法留空(可能我将在其他测试中使用它们)

结果我得到了这样的测试:

com.actionbarsherlock.view.MenuItem item = new TestSherlockMenuItem(R.id.some_action);

activity.onOptionsItemSelected(item);

ShadowActivity shadowActivity = Robolectric.shadowOf(activity);
Intent startedIntent = shadowActivity.getNextStartedActivity();
assertNotNull(startedIntent);

ShadowIntent shadowIntent = Robolectric.shadowOf(startedIntent);
assertThat(shadowIntent.getComponent().getClassName(),
                equalTo(NextActivity.class.getName()));
Run Code Online (Sandbox Code Playgroud)

顺便说一句,感谢Eugen Martynov试图理解我的问题:)

  • 使用Mockito节省实施时间.你需要两行代码:'com.actionbarsherlock.view.MenuItem item = mock(com.actionbarsherlock.view.MenuItem);' 和'when(item.getId()).thenReturn(R.id.some_action);' (2认同)