How to test menu in Android with Robolectric

robolectric test activity
robolectric android dependency
robolectric test fragment
robolectric kotlin
robolectric vs espresso
robolectric service test
robolectric setupactivity deprecated
android robolectric assertthat

I need to write tests to menu in Android application using Robolectric.

Source code of menu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onMenuItemSelected(int 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.onMenuItemSelected(featureId, item);
} 

Please help to write tests

The following example should be a good example for anyone who is getting started with Robolectric. It's using Robolectric 3.0 under AndroidStudio.

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 19)
public class MainActivityTest {
    @Test
    public void shouldCloseActivity() {
        MainActivity activity = Robolectric.setupActivity(MainActivity.class);
        MenuItem menuItem = new RoboMenuItem(R.id.exit);
        activity.onOptionsItemSelected(menuItem);
        ShadowActivity shadowActivity = Shadows.shadowOf(activity);
        assertTrue(shadowActivity.isFinishing());
    }
}

Java Code Examples org.robolectric.tester.android.view.TestMenu, This page provides Java code examples for org.robolectric.tester.android.view.​TestMenu. The examples are extracted from open source Java projects. In a nutshell. First, add Robolectric using Gradle or Maven. To write a test, create a Java Class in your project test folder. Add RunWith and Config annotations. Use a Before annotation for setting up your activity, and use Test annotations for each test stub.

Actually you should avoid using avoid using RoboMenuItem if possible. You can get the actual menu created by the activity by having robolectric create the activity and make it visible.

MainActivity activity = Robolectric.buildActivity(MainActivity.class).create().visible().get();

You can then use a ShadowActivity to get the actually created menu options;

shadowOf(activity).getOptionsMenu()

To get the actual MenuItem:

shadowOf(activity).getOptionsMenu().findMenuItem(...)

You can then pass that actual menuItem into the onOptionsItemSelected.

RoboMenuItem is just a dummy facade and actually it is recommended by the robolectric developers to avoid it if possible.

How to write Android unit tests using Robolectric, At the top menu, select Run, then click on Edit Configurations. After that, open the Defaults menu and select Android JUnit. Change the working  This article is focused on giving an overview about Android unit testing advantages and also How to write a simple Android Unit Test using Robolectric library. The link to download the complete code for this android unit testing tutorial is given at the bottom of the article.

kingargyle has a good answer on getting the MenuItem.

If you just want to be able to click on the item however, you can just use:

shadowOf(activity).clickMenuItem(R.id....);

Using Robolectric for Android unit testing on the JVM, So for all of us that want faster executing tests on the JVM, Robolectric saves the day. Robolectric provides implementations of the Android SDK by rewriting the  Robolectric simulates the runtime for Android 4.1 (API level 16) or higher and provides community-maintained fakes called shadows. This functionality allows you to test code that depends on the framework without needing to use an emulator or mock objects. Robolectric supports the following aspects of the Android platform: Component lifecycles

Unit Testing with Robolectric, robolectric/robolectric/src/test/java/org/robolectric/fakes/RoboMenuTest.java import android.app. RoboMenu menu = new RoboMenu(ApplicationProvider. If yes then, you must try Robolectric, it is a unit testing framework that allows Android applications to be tested on the JVM without an emulator or device. These are the main features of it: It provides a way to run our tests inside Android Studio, without launching app on Device or Emulator.

Writing Your First Test, Menu;. import android.view.MenuInflater;. import android.view.MenuItem; import org.junit.Test;. import org.junit.runner.RunWith;. import org.robolectric.R;. There are 2 ways to run your tests: Run a single test through Android Studio: Right click on the test class and select Run: Note: If you are presented with two options as in the diagram below, make sure to select the first one (designating to use the Gradle Test Runner instead of the JUnit Test Runner).

robolectric/RoboMenuTest.java at master · robolectric/robolectric , Before create Robolectric test Android tests, let's create a custom Roobolectric Runner. RobolectricTestOptionMenu.java for testing the option menu item  Put this class in your own project's org.robolectric.shadows package. That way Robolectric will find it when looking for a shadow for SupportMenuInflater, so that all your tests will get it automatically (no need to add a shadow Config to every test).

Comments
  • No one will write tests for you here, so make an attempt on you own and come back with a more elaborate question.
  • Hint: TestMenu, TestMenuItem.
  • check: stackoverflow.com/questions/22417800/…
  • @Daniel in Robolectric 3.x there are: TestMenu, TestMenuItem
  • Why here we need ShadowActivity? We can just call activity.isFinishing()... Not fully understand.
  • Hi, I had something like this - MainActivity activity = Robolectric.buildActivity(MainActivity.class).create().start().get(); and if I change start() to visible(), I get stackoverflow exception. Any idea what could be causing the issue?