shadowIntent.getComponent() is null

263 views
Skip to first unread message

Stuart Grimshaw

unread,
Apr 5, 2011, 6:18:59 PM4/5/11
to robol...@googlegroups.com
I'm trying to test a button in my app that launches another activity, and following the sample application I have the following test case set up:

@Before

public void setUp() throws Exception {

activity = new MainActivity();

activity.onCreate(null);

settingsButton = (Button) activity.findViewById(R.id.btnSettings); 

}

@Test

public void shouldLaunchSettingsWhenClicked() {

settingsButton.performClick();

ShadowActivity shadowActivity = shadowOf(activity);

Intent startedIntent = shadowActivity.getNextStartedActivity();

ShadowIntent shadowIntent = shadowOf(startedIntent);

assertThat(shadowIntent.getComponent().getClassName(), equalTo(SettingsActivity.class.getName()));

}


however when I run it, the assertion fails because getComponent() returns "null"


The app works fine, and this is my first Roboelectric test so I'm wondering what I've possibly missed that's making it not work?

Rich Humphrey

unread,
Apr 6, 2011, 12:35:38 PM4/6/11
to Robolectric
Hi Stuart,
I think there may be a bug in the current implementation of
ShadowIntent, but haven't dug into it yet.

Our hasLaunchedActivity() Hamcrest matcher uses
"shadowIntent.getIntentClass()".

It seems to pass if we create our intent like this...

new Intent().setClass( context, BillingActivity.class )

... but does not if we create our intent like this...

new Intent( context, BillingActivity.class )

-Rich


On Apr 5, 3:18 pm, Stuart Grimshaw <stuart.grims...@gmail.com> wrote:
> I'm trying to test a button in my app that launches another activity, and
> following the sample application I have the following test case set up:
>
>    @Before
>
> *public* *void* setUp() *throws* Exception {
>
>  activity = *new* MainActivity();
>
>  activity.onCreate(*null*);
>
>   settingsButton = (Button) activity.findViewById(R.id.*btnSettings*);
>
> }
>
>  @Test
>
> *public* *void* shouldLaunchSettingsWhenClicked() {
>
>  settingsButton.performClick();
>
>   ShadowActivity shadowActivity = *shadowOf*(activity);
>
>  Intent startedIntent = shadowActivity.getNextStartedActivity();
>
>  ShadowIntent shadowIntent = *shadowOf*(startedIntent);
>
>  *assertThat*(shadowIntent.getComponent().getClassName(), *equalTo*
> (SettingsActivity.*class*.getName()));

Stuart Grimshaw

unread,
Apr 8, 2011, 4:51:28 AM4/8/11
to robol...@googlegroups.com
I've tried various ways of instantiating the Intent() object in code, but they all produce the same error when trying to test it. I've tried various versions of Roboelectric too, currently on v1.0 RC1

Lenciel

unread,
May 19, 2011, 2:53:22 AM5/19/11
to Robolectric
Any update on this issue? I got exactly the same thing.

new Intent().setClass( context, BillingActivity.class ) , works fine.
new Intent( context, BillingActivity.class ) , returns null.

Pete

unread,
Apr 26, 2013, 9:52:20 AM4/26/13
to robol...@googlegroups.com
 I found that this was fine for setting-up the activity in the code under test:

        Intent intent = new Intent( this, MyTargetActivity.class );
        startActivity( intent );

And that this was the "magic" required code for the test:

       assertTrue(shadowIntent.getIntentClass().getSimpleName().equals(MyTargetActivity.class.getSimpleName()));

HTH!

Pete
Reply all
Reply to author
Forward
0 new messages