How to Supercharge Your Android Testing Using Espresso Test Framework

android testing

Android Espresso has been one of the most used automation test framework for Android testing. Despite the fact that Espresso test framework was targeted for developers who can build test scripts for their apps, Google has recently introduced Espresso Test recorder to quickly create tests for Android testing.

As Android testing framework Espresso aim to improve the productivity and make developers work more efficient, it can also provide effective improvements in mobile app quality.

Today, in our Android app tutorial, we’ll see different methods of Android testing.

Let’s Get Started

How to write test rule in espresso:

public ActivityTestRule<LoginActivity> mActivityTestRule = new ActivityTestRule<>(LoginActivity.class, true);

Above test rule defines for which activity you’r going to write test class.


new ActivityTestRule<>(LoginActivity.class, true);

if you do want to open activity which is not launcher activity then pass true in constructor else pass nothing for second parameter

new ActivityTestRule<>(LoginActivity.class);

To Open Activity:

public class PerfomClickAndCheckTextError {

public ActivityTestRule<LoginActivity> mActivityTestRule = new ActivityTestRule<>(LoginActivity.class, true);

You need to define that your going to use junit4 test class to run with below test class. If your test case is large then mention @LargeTest above class.

Now, before going any further with real test code other things to mention here is if we want to run some method before activity launches.

You can do it by specifying @Before annotation above your method.

public class PerfomClickAndCheckTextError {

public ActivityTestRule<LoginActivity> mActivityTestRule = new ActivityTestRule<>(LoginActivity.class, true);

//To get resources in testing
Resources resources;

public void initThingsHere() {
//do stuff like database or preference or image copying here
resources = InstrumentationRegistry.getTargetContext().getResources();

Let’s check how to find view from the screen in Espresso.

To check, there are two steps:

Find View on Screen

As we have findViewById in android, so in background what android system will do is it will check for that id view from rootview and give it back, we have onView() but to find views we can use multiple methods not by just ids. we have withId(), withText(), withTagKey() etc.

Check if exists or displayed

To check we have check() method in which we will give matcher which will conditionalize the view like if it displayed or not.

onView(withText("Hello Floks!")).check(matches(isDisplayed()));

Now that we have succeeded in finding view and performing checks we will move to step 2 which is perform events like typing and clicking.

To click:


To Merge click and checks in one:


Now, open activity with data in bundle as it’s important to pass data with activity you want to check activity with custom data.

public class PassDataInActivityTest {

public ActivityTestRule<LoginActivity> mActivityTestRule = new ActivityTestRule<LoginActivity>(LoginActivity.class, true) {
protected Intent getActivityIntent() {
Log.d(PassDataInActivityTest.class.getCanonicalName(), "getActivityIntent() called");
Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
Intent intent = new Intent(targetContext, LoginActivity.class);
intent.putExtra("testingcheck", true);
return intent;

public void initThingsHere() {
//do stuff like database or preference or image copying here

public void checkBlankEmailError() {
//to check view on screen
Bundle bundle = mActivityTestRule.getActivity().getIntent().getExtras();
assertThat(bundle.getBoolean("testingcheck"), is(true));
System.out.println("testingcheck:" + bundle.getBoolean("testingcheck"));

Now, to respond to external intents like gallery picks It’s hard to control external apps as with device applications can have different views so it’s not steady like your UI. in this condition what you can do is develop dependency injected code where you can mock the intents results or you can give result of intents in testing. We are trying to archive this:

Let’s check without Dependency Injection

For this you need to have espresso intents dependency in build.gradle file

androidTestCompile ('', {
exclude group: '', module: 'support-annotations'

Now after dependency added we can move on to the main course which is to mock the intent results.

2 things to keep in mind is intending and intended.

Intending It is used for specifying Espresso that when unit test wants to open this type of intent please respond with intended result which i’m giving you to give.

Intended It is used to check if the event intended to open some activity or package? we can check that thing by this.

Intent resultData = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Matcher<Intent> MediaPickIntent = allOf(hasAction(Intent.ACTION_PICK), hasData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI));
intending(MediaPickIntent).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData));

we have action pick event matcher which gives espresso hint that i’m finding this intent and by initlizing the intent we are starting intent checks for every intents. while intending tells that when I intend to do respond with the intent i’m giving.

And done!

Testing Android apps is a deep and involved topic, but now you should have everything you need to get started. And, if you’d liked to get to more advanced level, or test your Android application for bugs and improvements, you may hire an Android app development company to get your Android app tested.

Grab a free copy of Android Espresso Testing demo from Github.


Want to Develop Android App From Scratch? Contact Us Now

Get your free consultation now

Leave a Reply

Your email address will not be published. Required fields are marked *