How can I check the expected intent sent without actually launching activity in Espresso?
<p>I have a UI test which clicks a button, and then launch a new Activity in its onClickListener. The test checks whether expected intent is sent or not.</p>
<p>My problem is, I want to test whether expected intent is sent <strong>without actually launching the activity</strong>. Because I found that new activity initializes its state, and it makes subsequent tests flaky.</p>
<p>I know there are two <a href="https://google.github.io/android-testing-support-library/docs/espresso/intents/index.html" rel="nofollow noreferrer">Espresso Intents</a> API, which are <code><a href="https://developer.android.com/reference/android/support/test/espresso/intent/Intents.html#intended(org.hamcrest.Matcher%3Candroid.content.Intent%3E)" rel="nofollow noreferrer">intended</a></code> and <code><a href="https://developer.android.com/reference/android/support/test/espresso/intent/Intents.html#intending(org.hamcrest.Matcher%3Candroid.content.Intent%3E)" rel="nofollow noreferrer">intending</a></code>, but both fail to meet my needs. <code>intended</code> API actually launches the target activity, and <code>intending</code> API doesn't launch the activity, but it calls <code>onActivityResult</code> callback which I don't want either. Because I'm afraid that code inside <code>onActivityResult</code> may cause another flakiness.</p>
<p>Also <code>intending</code> doesn't assert whether matching intent is sent. It just calls <code>onActivityResult</code> callback when matching intent is found, which means I have to check whether <code>onActivityResult</code> is called or not!</p>
<p>Is there a clean way to achieve what I want?</p>