When I wrote my android-test-demo
app, I wanted to use Jake Wharton's ActivityRule
, but couldn't because I wanted to customize the launch intent for each test method.
Today I got my solution from Google: Test rules in Espresso 2.1. ActivityTestRule
can be configured to take a different launch intent per test method like this:
@Rule public ActivityTestRuleactivityRule = new ActivityTestRule<>( MainActivity.class, true, // initialTouchMode false); // launchActivity. False to set intent per method
I then supply a launch intent in the test method:
@Test public void intent() { DateTime dateTime = new DateTime(2014, 10, 15, 0, 0, 0); Intent intent = new Intent(); intent.putExtra(MainActivity.KEY_MILLIS, dateTime.getMillis()); activityRule.launchActivity(intent); onView(withId(R.id.date)) .check(matches(withText("2014-10-15"))); }
See MainActivityTest.java
for more details.
ActivityTestRule
makes tests much cleaner. Thank you Google for making Espresso better and better!
Like this article? Take a look at the outline of my Espresso book and fill in this form to push me to write it! Also check out the published courses: https://gumroad.com/chiuki
Inline coding questions will not be answsered. Instead, ask on StackOverflow and put the link in the comment.
I'm glad the Espresso guys revealed the ActivityTestRule to us. It makes testing activities much easier.
ReplyDelete