简体   繁体   中英

Unit Testing Menu Items in Actionbar Overflow

im writing a simple Unit Test in Junit trying to test if my intent's to my 2 actionbar overflow menu items open the right activity. Im having issue's with my test coming back as

junit.framework.AssertionFailedError: expected:<true> but was:<false>  (**FIXED**)

I also am trying to figure out how to verify that the activity was opened successfully and it was the expected activity launched.

Any help, examples and or comments are greatly appreciated.

public void testThatMenuWillOpenSettings() {
    // Will be sending Key Event to open Menu then select something
    ActivityMonitor am = getInstrumentation().addMonitor(
            Settings.class.getName(), null, false);

    // Click the menu option
    getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
    getInstrumentation().invokeMenuActionSync(mActivity,
            com.example.app.R.id.menu_settings, 0);

    // If you want to see the simulation on emulator or device:
    try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

    Activity a = getInstrumentation().waitForMonitorWithTimeout(am, 1000);
    assertEquals(true, getInstrumentation().checkMonitorHit(am, 1)); 

      // Check type of returned Activity:
      assertNotNull(a);
      assertTrue(a instanceof Settings);

      a.finish();

}

I am using (ActivityInstrumentationTestCase2) for this Unit Test as well.

Your test code is perfect fine. The AssertionFailedError shows that the Activity opened by menu click simulation is not the one ActivityMonitor is monitoring. According to the name menu_settings , I guess it's your app's perference Activity, whereas you are monitoring a different WebView Main Activity, this is the reason why ActivityMonitor is not hit. To fix this inconsistency, either change ActivityMonitor to monitor Activity_Pref_Settings, or change the menu click simulation to open R.id.menu_webview_main.

I also am trying to figure out how to verify that the activity was opened successfully and it was the expected activity launched.

You can use instanceof check the type of returned activity:

public void testThatMenuWillOpenSettings() {
  // Use false otherwise monitor will block the activity start and resulting waitForMonitorWithTimeout() return null: 
  ActivityMonitor am = getInstrumentation().addMonitor(Activity_Webview_Main.class.getName(), null, false);

  ... ...

  // If you want to see the simulation on emulator or device:
  try {
    Thread.sleep(1000);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }

  Activity a = getInstrumentation().waitForMonitorWithTimeout(am, 1000);
  assertEquals(true, getInstrumentation().checkMonitorHit(am, 1));

  // Check type of returned Activity:
  assertNotNull(a);
  assertTrue(a instanceof Activity_Webview_Main);

  a.finish();

}

Note that further check against the returned activity is not necessary but possible, for instance, check the title, label text and etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM