简体   繁体   中英

How to use robolectric to test activity with extra intent data

I have an activity LoanDemandEntry.class

I am taking index from intent

selectedMemberIndex = getIntent().getExtras().getInt("selectedMemberIndex");

but when I run robolectric test to see if the views exist it will fail because of this line of code.

Any suggestions?

Just pass your parameter before run your test like this:

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);

    Intent mIntent = new Intent(ApplicationProvider.getApplicationContext(), YourActivity.class);
    mIntent.putExtra("selectedMemberIndex", 10);

    mActivity = Robolectric.buildActivity(YourActivity.class, mIntent).create().resume().visible().get();
}

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