Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8031629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:10:12+00:00 2026-06-05T01:10:12+00:00

I have an activity that will grab a pojo from the extras, like so

  • 0

I have an activity that will grab a pojo from the extras, like so :

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Bundle extras = getIntent().getExtras();
        if (extras != null)
        {
            MyPojo pojo = extras.getParcelable("pojo");
            // do stuff with my pojo here
            }
        }
        // button that shows a toast message
    }

I’m having difficulty being able to test this using Robolectric, I believe I need to use a combination of ShadowIntents and ShadowActivities, but the documentation is a little thin, and any useful tutorials on this particular scenario are rather sparse.

This is what I’ve come up with so far :

@Test
    public void assertClickingTagSightingDisplaysToast()
    {
        myActivity = new MyActivity();
        myActivity.onCreate(null);
        ShadowActivity shadowMyActivity = shadowOf(myActivity);
        shadowMyActivity.setIntent(new Intent().putExtra("pojo", generateAPojo()));


        ShadowButton shadowButton = (ShadowButton) shadowOf(shadowMyActivity.findViewById(R.id.myButton));
        shadowButton.performClick();

        assertThat(ShadowToast.getTextOfLatestToast(), equalTo("Button was clicked!"));
    }

I keep getting a null pointer when I execute these tests, at the getIntent().getExtras() line of my activity, I’m assuming that my process of mocking the activities with intents is incorrect.

can anyone please help?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:10:14+00:00Added an answer on June 5, 2026 at 1:10 am

    I hit this very thing myself. It seems that you need to use the shadowActivity to manipulate the Intent, prior to calling your onCreate().

    Robolectric.shadowOf(activity).setIntent(intent);
    

    Seems like manipulating the activity itself should do the trick, but it doesn’t. Somebody way smarter than me will have to explain why this is — or someone only modestly smarter than me to tell you that the above is wrong.

    — Quick note. Since Robolectric appears to be using javassist to manipulate the shadow classed, it’s important to do your class binding prior to the first load of a class. I followed their suggestion to do the binding in the Runner.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity that will be started from a parent activity, but the
I have created an Activity that will behave like a popup menu is actually
I have an activity that launches another activity with startActivityForResult method. I would like
I have an android activity that downloads content from the web. The activity have
So i have an activity that imports a list of names from a .txt
I'm writing my first Android application and I have one activity that I'd like
In my application, I have specified a second activity that can be launched from
Activity that is sending the putExtra() @Override protected void onListItemClick(ListView l, View v, int
I have a ListActivity that displays a list of search results I grab from
I have an Activity that retrieves information from a remote server and displays it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.