Adam Varhegyi
Adam Varhegyi

Reputation: 9894

Testing launching activity intents with espresso

I would like to make a simple UI test, where a button being pressed and an activity being launched.

I have tried based on this documentation. (android developers)

Code of SplashActivity:

public class SplashActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    findViewById(R.id.launch_register_activity_btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(SplashActivity.this, RegisterActivity.class);
            startActivity(intent);
        }
    });
}
}

Testing Code:

@RunWith(AndroidJUnit4.class)
public class SimpleIntentTest {

@Rule
public IntentsTestRule<SplashActivity> intentsRule = new IntentsTestRule<>(SplashActivity.class);

@Test
public void newActivityLaunchingTest() {


    onView(withId(R.id.launch_register_activity_btn)).perform(click());

    Log.i("register-package_name", RegisterActivity.class.getPackage().toString());
    Log.i("register-class_name", RegisterActivity.class.getName());
    Log.i("register-short_name", RegisterActivity.class.getSimpleName());


    intended(allOf(
            hasComponent(hasShortClassName(".RegisterActivity")),
            toPackage("re.example.common")
    ));


}
}

Logcat:

01-22 13:37:51.615: I/register-package_name(25292): package re.example.common, Unknown, version 0.0
01-22 13:37:51.615: I/register-class_name(25292): re.example.common.RegisterActivity
01-22 13:37:51.615: I/register-short_name(25292): RegisterActivity

Package structure:

enter image description here

Test output:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents.

IntentMatcher: (has component: has component with: class name: an instance of java.lang.String package name: an instance of java.lang.String short class name: is ".RegisterActivity" and resolvesTo: re.example.common)

Matched intents:[]

No matter how I try, the test does not validate that I had launched RegisterActivity. The test fails.

As far as I know I did exactly like in the espresso-testing example at the link above.

What am I doing wrong?

I have also tried:

1. Removing the . (dot) from ".RegisterActivity"

intended(allOf(
            hasComponent(hasShortClassName("RegisterActivity")),
            toPackage("re.example.common")
    ));

2. Removing the . (dot) from ".RegisterActivity" and toPackage("re.example.common")

intended(allOf(
            hasComponent(hasShortClassName("RegisterActivity"))
    ));

My RegisterActivity's package name is "re.example.common.RegisterActivity". The short name is "RegisterActivity". Isn't it?

Why the test fails?

What's happening here, what am I misunderstand or misconcept?

Thanks in advance.

Upvotes: 3

Views: 3780

Answers (1)

Adam Varhegyi
Adam Varhegyi

Reputation: 9894

Application id (app package name is) is: re.example (source gradle)

The activity's full name is: re.example.common.RegisterActivity

This way, hasShortClassName meant to be .common.RegisterActivity as shortName and not .RegisterActivity nor RegisterActivity

Meh.


Upvotes: 4

Related Questions