I am getting this error while testing activity launch using espresso.
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 2 intents.
Surprisingly other activity launch tests with same code are getting passed.
@RunWith(AndroidJUnit4.class)
public class HomeActivityTest {
@Rule
public final IntentsTestRule<HomeActivity> mHomeActivityRule = new IntentsTestRule<HomeActivity>(HomeActivity.class);
@Test
public void testFundTransferActivityStarted() {
onView(withId(R.id.button_fund_transfer)).perform(click());
intended(hasComponent("mypackage.FundTransferActivity"));
}
}
button click simply starts FundTransfer activity using startActivity.
Android Intent is used to open new activity, either internal (opening a product detail screen from product list screen) or external (like opening a dialer to make a call). Internal intent activity is handled transparently by the espresso testing framework and it does not need any specific work from the user side.
Espresso is a testing framework that helps developers write automation test cases for user interface (UI) testing. It has been developed by Google and aims to provide a simple yet powerful framework. It allows both black-box testing as well as testing of individual components during development cycles.
Espresso is a UI test framework (part of the Android Testing Support Library) that allows you to create automated UI tests for your Android app.
Could you paste the full error message you got?
The error message would have the configuration of the 2 Intents matched as well. If both those intents have the same configuration, that means that you are calling startActivity
twice, i.e, between test started and test ended. The below answer is for a specific case when you end up calling the same Intent twice.
Calling twice is a perfectly legitimate case. For instance,
Step 1: tap on Button1
to launch Gallery and pick a image and show in ImageView1
(initially ImageView1
is GONE
and now it's VISIBLE
).
Step 2: tap on ImageView1
to launch the Gallery again.
Now, If you wanted to test "Tapping on ImageView1
should launch Gallery", then you can't simply tap on ImageView1
since it's not visible initially. You need to first tap on Button1
. If you do this, you need to Launch the Gallery twice.
Hence intended(hasComponent("mypackage.FundTransferActivity"));
won't work. Instead use: intended(hasComponent("mypackage.FundTransferActivity"), times(2));
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