Stubbing / mocking intent in Android Espresso test

I want to test the following thread in my application:

  • User clicks scan button
  • onClick ZXing app launches
  • If the correct qr code returns, we continue another user gets the opportunity to enter the code manually

I want to test this flow using espresso. I think I need to use the intended or intending 1 , but I'm not sure how to check if the intent was ZXing and how to get back to the application.

+4
source share
1 answer

The general flow for using espresso intent is as follows:

  • Call intending(X).respondWith(Y) to customize the layout.
  • Take an action that should result in the sending of the intent.
  • Call intended(Z) to verify that the layout has the expected intention.

X and Z can be the same, but I tend to make X as generalized as possible (for example, only matching the name of the component) and make Z more specific (check the values ​​of additional parameters, etc.).

eg. For ZXing, I could do something like this (warning: I have not tested this code!):

 Intents.intending(hasAction("com.google.zxing.client.android.SCAN"); // Match any ZXing scan intent onView(withId(R.id.qr_scan_button).perform(click()); // I expect this to launch the ZXing QR scanner Intents.intended(Matchers.allOf( hasAction("com.google.zxing.client.android.SCAN"), hasExtra("SCAN_MODE", "QR_CODE_MODE"))); // Also matchs the specific extras I'm expecting 
+2
source

All Articles