简体   繁体   中英

Robolectric: Test which Activity is launched when application is run

I want to write an automated test using robolectric that confirms a given activity is launched when the application is started.

This will be my "walking skeleton" acceptance test as described in Freeman and Pryce's TDD book.

The test basically confirms that the application's main intent filter is implemented correctly:

<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

I know that it's a test that's barely worth doing, but that's the point of the "walking skeleton" test - and is one I think is worth doing.

How could this be done? The "hello world" test provided by robolectric is this:

@Before 
public void setup()
{
    this.activity = Robolectric.buildActivity(MainActivity.class).create().get();
}
@Test
public void shouldHaveHappySmiles() throws Exception 
{
    String appName = this.activity.getString(R.string.app_name);
    assertThat(appName, equalTo("MyApp"));
}

The above runs MainActivity and tests a property of it. Instead, how can I assert that when the app is launched, it actually starts MainActivity ?

As for me this test in general doesn't give much value. But maybe it is important in your case.

I would access `AndroidManifest' and check specific 'ActivityData' with 'IntentFilter':

AndroidManifest appManifest = Robolectric.getShadowApplication().getAppManifest();
ActivityData activityData = appManifest.getActivityData( "<yourpackage>.MainActivity" );
List<IntentFilterData> intentFilters = activityData.getIntentFilters();
IntentFilterData data = intentFilters.get( 0 );

assertThat( data.getActions() ).contains( "android.intent.action.MAIN" );
assertThat( data.getCategories() ).contains( "android.intent.category.LAUNCHER" );

This is just example, pay attention if you have more intent filters

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