Is there any good way to test the result code and data in an Android Espresso test? I am using Espresso 2.0.
Suppose I have an Activity
called BarActivity.class
, which upon performing some action, calls setResult(int resultCode, Intent data)
with the appropriate payload.
I'd like to write a test case to verify the resultCode
and data
. However, because setResult()
is a final
method, I can't override it.
Some options I thought about were:
setActivityResult()
and just use that so it can be intercepted, etc...startActivityForResult()
on BarActivity
and check the result in TestActivity.onActivityResult()
Trying to think what's lesser of the two evils, or if there's any other suggestions on how to test for this. Any suggestions? Thanks!
One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class. I must caution that visibility attribute is not clearly defined in the GUI world. A view may be considered visible but may be overlapped with another view, for one example, making it hidden.
If meanwhile you switched to the latest Espresso, version 3.0.1, you can simply use an ActivityTestRule
and get the Activity result like this:
assertThat(rule.getActivityResult(), hasResultCode(Activity.RESULT_OK));
assertThat(rule.getActivityResult(), hasResultData(IntentMatchers.hasExtraWithKey(PickActivity.EXTRA_PICKED_NUMBER)));
You can find a working example here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With