Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso Test Failed: Wanted to match 1 intent, Actually matched 2 intents

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.

like image 522
SohailAziz Avatar asked Dec 24 '15 06:12

SohailAziz


People also ask

What is test intent?

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.

What is espresso testing used for?

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.

What is Espresso UI Test?

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.


1 Answers

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));

like image 175
Henry Avatar answered Sep 24 '22 15:09

Henry