Blog

Using Intent extras with Espresso Rules

06 Sep, 2015

The android-test-support library has ditched the ActivityInstrumentationTestCase2 base class for a cleaner approach using JUnit4 rules. This is nice and clean, but te documentation is currently lacking on how to proceed if your Activity requires Intent extras to run. This post demonstrates how to make that work.

Heres the simplest case, where all activities are launched with the "android.intent.action.MAIN" action:
[sourcecode language=”java” collapse=”false”]
@SmallTest
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ActivityTestRule<>(MainActivity.class);
@Test
public void someTest() {
/* Your activity is initialized and ready to go. */
}
}
[/sourcecode]
Here’s how to make all methods in your test class use the same intent, with extras:
[sourcecode language=”java” collapse=”false”]
@SmallTest
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ActivityTestRule<MainActivity>(MainActivity.class) {
@Override
protected Intent getActivityIntent() {
Context targetContext = InstrumentationRegistry.getInstrumentation()
.getTargetContext();
Intent result = new Intent(targetContext, MainActivity.class);
result.putExtra("Name", "Value");
return result;
}
};
@Test
public void someTest() {
/* Your activity is initialized and ready to go. */
}
}
[/sourcecode]
If you want each test method to provide its own set of extras, you can do that too:
[sourcecode language=”java” collapse=”false”]
@SmallTest
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ActivityTestRule<>(MainActivity.class, true, false);
@Test
public void someTest() {
Context targetContext = InstrumentationRegistry.getInstrumentation()
.getTargetContext();
Intent intent = new Intent(targetContext, MainActivity.class);
intent.putExtra("Name", "Value");
mActivityRule.launchActivity(intent);
/* Your activity is initialized and ready to go. */
}
}
[/sourcecode]
I have put a complete, running example on GitHub at barend/android-espresso-intent-sample.

guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Joe
Joe
6 years ago

getActivityIntent does not seem to be working. I keep getting “Test failed to run to completion. Reason: ‘Instrumentation run failed due to ‘java.lang.NullPointerException”. Check device logcat for details
Test running failed: Instrumentation run failed due to ‘java.lang.NullPointerException'”, but nothing shows up on Logcat. For some reason the extras I’m defining (exactly how you do in your example) are not being places in the intent that is trying to launch.
here is my code:
@Rule
public ActivityTestRule mActivityRule = new ActivityTestRule(
ShareActivity.class, true, true){
@Override
protected Intent getActivityIntent() {
Context targetContext = InstrumentationRegistry.getInstrumentation()
.getTargetContext();
Intent result = new Intent(targetContext, ShareActivity.class);
result.putExtra(ShareActivity.EXTRA_FILE_URI, mUri);
result.putExtra(ShareActivity.EXTRA_MULTIPLE, false);
result.putExtra(ShareActivity.EXTRA_CAPTURED, true);
result.putExtra(ShareActivity.EXTRA_FROM_CONTENT_RESOLVER, false);
return result;
}
};
Interesting thing to note is that if I launch this intent with the same credentials inside a @test instead of in getActivityIntent everything functions properly.

Jonathan Santos
Jonathan Santos
4 years ago
Reply to  Joe

Your rule has 3 parameters (Activity, true, true). Remove the booleans if you want to provide the same Intent for all methods.

Explore related posts