O
O
Oleg Gamega2016-08-11 15:35:16
Android
Oleg Gamega, 2016-08-11 15:35:16

Why shadowOf(activity).getOptionsMenu(); returns null?

Hello.
There is no experience with tests (somehow not everyone is ready to pay, and those who are ready do not have the tasks that require testing)
A simple example with robolectric

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class MainActivityTest {
    @Test
    public void onCreateShouldInflateTheMenu() {
        Activity activity = Robolectric.setupActivity(MainActivity.class);

        final Menu menu = shadowOf(activity).getOptionsMenu();
      
       assertThat(menu.findItem(R.id.action_settings).getTitle()).isEqualTo("Settings");
    }
}

crashes with npe since menu is null, but at least
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class MainActivityTest {
    @Test
    public void onCreateShouldInflateTheMenu() {
        Activity activity = Robolectric.setupActivity(MainActivity.class);

        final Menu menu = shadowOf(activity).getOptionsMenu();
        assertThat(menu).isEqualTo(null);
    }
}

green =))
An example is almost one to one with the docks, how is it treated?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question