0

I'm trying to test that calling a simple method in my Android app will start another activity. getNextStartedActivity always returns the launcher activity.

Here is the test:

@Test
public void clickingButton_shouldStartNextActivity() throws Exception {
    CurrentActivity activity = Robolectric.setupActivity(CurrentActivity.class);
    activity.startNextActivity();

    Intent expectedIntent = new Intent(activity, NextActivity.class);
    assertEquals(expectedIntent, Shadows.shadowOf(activity).getNextStartedActivity());
}

And here is the startNextActivity method in CurrentActivity:

public void startNextActivity() {
    startActivity(new Intent(this, NextActivity.class));
}

When I run the test, I get LoginActivity (launcher activity) as the actual result. Here LoginActivity in AndroidManifest.xml:

<activity
    android:name=".LoginActivity"
    android:noHistory="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
mravca
  • 699
  • 1
  • 6
  • 11
  • 1
    Can you share the code of the `NextActivity`? Maybe there is something calling the `LoginActivity` – Eugen Martynov Oct 20 '15 at 05:22
  • Thanks for the tip @EugenMartynov! I extend the activity class to redirect users to the login page if there is no api key saved in the Shared Preferences. Saving an api key to Shared Preferences in my tests solved the problem. – mravca Oct 21 '15 at 22:42

0 Answers0