Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robolectric - how to mock com.actionbarsherlock.view.MenuItem?

I'm trying to write tests with Robolectric for an App that uses SherlockActionBar. I need to test if the applications does the right thing if an MenuItem is selected, but the Robolectric lib gives only mocks for android.view.MenuItem while the app uses the method onOptionItemSelected(com.actiombarsherlock.view.MenuItem).

So my questions are:

  • May be there is a potability to mock the com.actionbarsherlock.view.MenuItem?

  • Or a workaround or something?

Thanks in advance...

like image 694
Serj Lotutovici Avatar asked Dec 28 '25 16:12

Serj Lotutovici


1 Answers

So... becous there is no more elegant way to mock the com.actionbarsherlock.view.MenuItem I did this:

  • Made my own class that implements com.actionbarsherlock.view.MenuItem
  • Added a int field for itemId in my mock class.
  • Other methods from the MenuItem interface are left blank (may be I'll use them in other tests)

As a result i got this kind of test:

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()));

By the way, thanks to Eugen Martynov for trying to understand my problem:)

like image 136
Serj Lotutovici Avatar answered Dec 30 '25 04:12

Serj Lotutovici



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!