Josias Sena

MVP Mockito.jpg

As software developers, we try our best to do what is right and make sure that we are not incompetent, and try to have others and our employers trust in the code we write. We all try to follow best practices and apply good architecture patterns, but sometimes many of us find it difficult to actually test what we code.

Personally, I have seen a few open-source projects where the developers are great at building awesome products—and can build any application you can think of—but for some reason lack at writing the proper tests, if any at all.

This here is another simplified tutorial on how to unit test the "oh so amazing" MVP architecture pattern that many of us try to follow.

Before I continue, I want to mention that I assume you are familiar with the MVP pattern, and have used it before. I will not go over the MVP pattern at all, and I will not explain how it works. With that in mind, I'd like to mention ahead of time that I am using one of my favorite libraries for MVPs created by a guy named Hannes Dorfman called Mosby. For simplicity's sake, I am also using the view binding library called ButterKnife.

So what is this application we will be looking at?

It is a very simple Android app, and it does one thing and one thing only: It hides and displays a TextView with the click of a button. That's it.

Here's how the app looks initially:

Initial

Here's how it looks when the button is clicked:

724E8fE.png

For the sake of this article, let's imagine that this is a multi-million-dollar product, and that the way it is now is the way it should be for a very long time. And if it were to ever change, we should be notified immediately.

So we have three things in this app: a blue toolbar with the app name, a text view that displays "Hello World," and a button that hides/shows the TextView.

Before I start, I would like to mention that you can find all of the code for this article on  my GitHub; if you do not want to read any of my rambling, please feel free to skip the rest of the post and go straight to the code. Comments will be added for clarity.

Now, lets get testing!

The Espresso tests

The first thing we want to test is our awesome ToolBar design. I mean, it is a million-dollar app after all; we need to make sure it stays that way!

So first, here's the complete code used to test the TooBar design. If you have no idea what is going on here, don't worry: We will walk through it together.


@RunWith (AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityTestRule activityTestRule =
            new ActivityTestRule<>(MainActivity.class);

    @Test
    public void testToolbarDesign() {
        onView(withId(R.id.toolbar)).check(matches(isDisplayed()));

        onView(withText(R.string.app_name)).check(matches(withParent(withId(R.id.toolbar))));

        onView(withId(R.id.toolbar)).check(matches(withToolbarBackGroundColor()));
    }

    private Matcher<? super View> withToolbarBackGroundColor() {
        return new BoundedMatcher<View, View>(View.class) {
            @Override
            public boolean matchesSafely(View view) {
                final ColorDrawable buttonColor = (ColorDrawable) view.getBackground();

                return ContextCompat
                        .getColor(activityTestRule.getActivity(), R.color.colorPrimary) ==
                        buttonColor.getColor();
            }

            @Override
            public void describeTo(Description description) {
            }
        };
    }
}

First things first: We need to tell JUnit what sort of test we are running. This is what the first line does (@RunWith (AndroidJUnit4.class)). It says. "Hey, listen, I want to run an Android test that uses JUnit4 on an actual connected device."

So what exactly is an Android test? An Android test is a test that runs on the device instead of locally on the Java Virtual Machine (JVM) on your computer. This means that a device needs to be connected to your computer in order to run the test. This gives the test code access to functional Android framework APIs.

These tests go in the androidTest directory.

android_test_directory

Next lets take a look at this thing called an "ActivityTestRule". Since the android documentation explains it really well, here it is:

"This rule provides functional testing of a single activity. The activity under test will be launched before each test annotated with Test and before methods annotated with Before. It will be terminated after the test is completed and methods annotated with After are finished. During the duration of the test you will be able to manipulate your Activity directly."

This basically says, "This is the activity I want to run my test."

Lets get in to the testToolbarDesign() method and see what the hell is going on.

Testing the toolbar


    onView(withId(R.id.toolbar)).check(matches(isDisplayed()));

What this test does is find a view with the ID that matches "R.id.toolbar," then checks to make sure that this view is visible/displayed. If this line were to fail, the test would end right there and wouldn't even bother with the rest.


     onView(withText(R.string.app_name)).check(matches(withParent(withId(R.id.toolbar))));

This one says "Hey, lets see if there is some text that equals 'R.string.app_name' and has a parent whose id is R.id.toolbar."

The last line in this test is a bit more involved. So basically what it is trying to do is to make sure that the background of the toolbar is equal to the app's primary color.


    onView(withId(R.id.toolbar)).check(matches(withToolbarBackGroundColor()));

So by default, Espresso does not provide a straightforward way to do this, so we need to create what is called a Matcher. A Matcher is exactly what we have been using previously to match some view property to another. In this case, we want to match the primary color to the toolbars background.

What we do is we create a Matcher and override the matchesSafely() method. The code inside this method is pretty easy to understand. First we get the toolbar's background, then we compare it to the app's primary color. If it is equal, it returns true; false otherwise.

Test TextView hides/shows properly

Ok, before I show any code, I just want to mention that this code is a bit more verbose, but pretty straightforward to read. I have added some comments to show what exactly is going on.

@RunWith (AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityTestRule activityTestRule =
            new ActivityTestRule<>(MainActivity.class);
            
    // ...

    @Test
    public void testHideShowTextView() {

        // Check the TextView is displayed with the right text
        onView(withId(R.id.tv_to_show_hide)).check(matches(isDisplayed()));
        onView(withId(R.id.tv_to_show_hide)).check(matches(withText("Hello World!")));

        // Check the button is displayed with the right initial text
        onView(withId(R.id.btn_change_visibility)).check(matches(isDisplayed()));
        onView(withId(R.id.btn_change_visibility)).check(matches(withText("Hide")));

        // Click on the button
        onView(withId(R.id.btn_change_visibility)).perform(click());

        // Check that the TextView is now hidden
        onView(withId(R.id.tv_to_show_hide)).check(matches(not(isDisplayed())));

        // Check that the button has the proper text
        onView(withId(R.id.btn_change_visibility)).check(matches(withText("Show")));

        // Click on the button
        onView(withId(R.id.btn_change_visibility)).perform(click());

        // Check the TextView is displayed again with the right text
        onView(withId(R.id.tv_to_show_hide)).check(matches(isDisplayed()));
        onView(withId(R.id.tv_to_show_hide)).check(matches(withText("Hello World!")));

        // Check that the button has the proper text
        onView(withId(R.id.btn_change_visibility)).check(matches(isDisplayed()));
        onView(withId(R.id.btn_change_visibility)).check(matches(withText("Hide")));
    }
    
    // ...
}

The gist of this code is that it is making sure that when the app opens, the TextView with ID "R.id.tv_to_show_hide" is displayed, and the text displayed on the TextView says "Hello World!"

Then we check that the button is also displayed properly, and that the text on the button (by default) is set to "Hide".

Next we click on the button. A click on a button is very straightforward, and it is very easy to read how it is done. This time instead of calling ".check" after we find a view by ID, we call .perform(), and inside the perform() method, we pass in click(). The perform() method says "Please do the following action," and then "performs" whatever action is passed in. In our case it is a click() action.

Since the "Hide" button was clicked, we need to make sure the TextView is actually hidden now. We do this by adding a "not()" in front of the isDisplayed() method we used previously, and the button text has been changed to "Show". This is the same as doing "!=" in plain ol' java.

@RunWith (AndroidJUnit4.class)
public class MainActivityTest {
    // ...

    @Test
    public void testHideShowTextView() {
    
        // ...

        // Check that the TextView is now hidden
        onView(withId(R.id.tv_to_show_hide)).check(matches(not(isDisplayed())));

        // Check that the button has the proper text
        onView(withId(R.id.btn_change_visibility)).check(matches(withText("Show")));
        
        // ...
    }
    
    // ...
}

The code after these lines is the reverse of what we did before. We click the button again, make sure the TextView is visible again, and make sure that the button text has changed to successfully to match the situation.

And that's it!

Here is the full UI test code:

@RunWith (AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityTestRule activityTestRule =
            new ActivityTestRule<>(MainActivity.class);

    @Test
    public void testToolbarDesign() {
        onView(withId(R.id.toolbar)).check(matches(isDisplayed()));

        onView(withText(R.string.app_name)).check(matches(withParent(withId(R.id.toolbar))));

        onView(withId(R.id.toolbar)).check(matches(withToolbarBackGroundColor()));
    }

    @Test
    public void testHideShowTextView() {

        // Check the TextView is displayed with the right text
        onView(withId(R.id.tv_to_show_hide)).check(matches(isDisplayed()));
        onView(withId(R.id.tv_to_show_hide)).check(matches(withText("Hello World!")));

        // Check the button is displayed with the right initial text
        onView(withId(R.id.btn_change_visibility)).check(matches(isDisplayed()));
        onView(withId(R.id.btn_change_visibility)).check(matches(withText("Hide")));

        // Click on the button
        onView(withId(R.id.btn_change_visibility)).perform(click());

        // Check that the TextView is now hidden
        onView(withId(R.id.tv_to_show_hide)).check(matches(not(isDisplayed())));

        // Check that the button has the proper text
        onView(withId(R.id.btn_change_visibility)).check(matches(withText("Show")));

        // Click on the button
        onView(withId(R.id.btn_change_visibility)).perform(click());

        // Check the TextView is displayed again with the right text
        onView(withId(R.id.tv_to_show_hide)).check(matches(isDisplayed()));
        onView(withId(R.id.tv_to_show_hide)).check(matches(withText("Hello World!")));

        // Check that the button has the proper text
        onView(withId(R.id.btn_change_visibility)).check(matches(isDisplayed()));
        onView(withId(R.id.btn_change_visibility)).check(matches(withText("Hide")));
    }

    private Matcher<? super View> withToolbarBackGroundColor() {
        return new BoundedMatcher<View, View>(View.class) {
            @Override
            public boolean matchesSafely(View view) {
                final ColorDrawable buttonColor = (ColorDrawable) view.getBackground();

                return ContextCompat
                        .getColor(activityTestRule.getActivity(), R.color.colorPrimary) ==
                        buttonColor.getColor();
            }

            @Override
            public void describeTo(Description description) {
            }
        };
    }
}

The Unit Tests

The great thing about unit tests—as opposed to Android tests—is that they run locally on your machine on the JVM. No need to have a device attached, and the tests run so much faster. The downside is that they do not have access to functional Android framework APIs. Overall, when testing anything other then UI you should try your best to write unit tests instead of Android/Instrumentation tests. The faster the tests, the better.

Lets start with the directory of the unit tests. The unit tests go in a different location then the Android tests did.

different_location

Before moving on lets take a look at our presenter and what we are going to consider our model for this tutorial.

Lets start with the presenter

public class MainPresenterImpl extends MvpBasePresenter implements MainPresenter {

    @Override
    public void reverseViewVisibility(final View view) {
        if (view != null) {
            if (view.isShown()) {
                Utils.hideView(view);

                setButtonText("Show");
            } else {
                Utils.showView(view);

                setButtonText("Hide");
            }
        }
    }

    private void setButtonText(final String text) {
        if (isViewAttached()) {
            getView().setButtonText(text);
        }
    }
}

Simple enough. We have two methods: One checks to see if the view is visible. If it is, hide it, otherwise show it. Once this is done, we call into our view and say "Hey, change the button text to either 'Hide' or 'Show'."

Our reverseViewVisibility() method calls into what we call (for this tutorial at least) our "model" to set the proper visibility on the view passed in.

Lets take a look at our model.

public final class Utils {

    // ...

    public static void showView(View view) {
        if (view != null) {
            view.setVisibility(View.VISIBLE);
        }
    }

    public static void hideView(View view) {
        if (view != null) {
            view.setVisibility(View.GONE);
        }
    }
}

There are two methods: showView(View) and hideView(View). What these methods do is very straightforward and self-explanatory. We check if the view is null; if not, we either hide it or show it.

Great, now that we are familiar with both our presenter and our "model," lets go ahead and test them. After all, this is a multi-million-dollar product, and we cannot have anything go wrong.

Let start start testing our presenter first. When it comes to a presenter—ANY presenter—we need to make sure that the view is attached to the presenter. Note: We are NOT testing the view. We just need to make sure that the view is attached so that we can verify the proper view methods are being made in the right time. Keep this in mind as this is very important.

We will be using PowerMock to run our tests, so just like we did with our unit tests, we ned to tell Android, "Hey, we want to run these tests with the PowerMockRunner." To do so we add the @RunWith (PowerMockRunner.class) annotation on top of our test class.

So we know two things right from the start: We need a mocked View because our presenter uses a View Object to hide or show it, and we also need our mocked presenter.

This is how we mock using mockito (PowerMock in this case): **PowerMock** is an extension to Mockito that allows mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers, and more.

@RunWith (PowerMockRunner.class)
public class MainPresenterImplTest {

    @Mock
    MainPresenterImpl presenter; // our mocked presenter

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this); // Required when using the mockito @mock annotation
    }
    
    // ...
}

Like I mentioned before, these objects are mocked objects, technically they're not the real thing. So we need to also "mock" what happens when certain methods are called on these objects. If we don't do this, we will get errors all over the place.

First we need to do two things. We need to attach some kind of view to our mock presenter to use when calling certain methods, such as the getView().setButtonText(text) call we saw earlier.

Take a look at the presenter setButtonText() method. First it checks if our view is attached, and then it calls the views setButtonText(text) method.


@RunWith (PowerMockRunner.class)
public class MainPresenterImplTest {

    // ...

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        // This says that when .isViewAttached() is called, we should return true
        when(presenter.isViewAttached()).thenReturn(true);
        
        // This says that when getView() is called inside the presenter, we return a mocked view to be used. 
        // Our View implements a MainView interface, so we mock the interface which contains the method called by the presenter.
        when(presenter.getView()).thenReturn(mock(MainView.class));
    }
    
    // ...

}

Now, there is one very important thing that needs to happen in the next line: We need to tell Mockito that, when the reverseViewVisibility() method is called in our presenter, to call the actual method instead of a mocked one. When I say that we want to call the actual method, it means that all the logic and inner class calls actually happen rather then a mocked implementation of it.

This is done by calling the Mockito doCallRealMethod():


    doCallRealMethod().when(presenter).reverseViewVisibility(any(View.class));

That is it for our setUp() method. Here is the full thing:

@RunWith (PowerMockRunner.class)
public class MainPresenterImplTest {

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        when(presenter.isViewAttached()).thenReturn(true);
        when(presenter.getView()).thenReturn(mock(MainView.class));

        doCallRealMethod().when(presenter).reverseViewVisibility(any(View.class));
    }
}

The first actual test we want to write is called "testReverseViewVisibilityFromVisibleToGone". As the name says, we are going to make sure that the presenter sets the proper visibility when the view passed in to the reverseViewVisibility() method is visible.

   @Test
    public void testReverseViewVisibilityFromVisibleToGone() throws Exception {
        final View view = Mockito.mock(View.class);
        when(view.isShown()).thenReturn(true);

        presenter.reverseViewVisibility(view);

        Mockito.verify(view, Mockito.atLeastOnce()).setVisibility(View.GONE);
        Mockito.verify(presenter.getView(), Mockito.atLeastOnce()).setButtonText(anyString());
    }

Let's step through this together. What is actually going on here? Well, when the isShown() method is called on the view that is passed in to the presenter, we want to say yes, the view is shown, because we are testing from visible to gone, so we need to start at visible. Then, we call the presenters reverseViewVisibility() method by passing in the mocked view. Now we need to verify that the mocked views .setVisibility() was called at least once, and it was set to View.GONE. Afterwards, we need to verify that the presenters view setButtonText() method was called. Not that hard right?

Alright, lets do the opposite. Before moving on and looking at the next piece of code, take some time to figure it out yourself. How would we test going from hidden to visible? Think about what you already know.

Here's the code:

    @Test
    public void testReverseViewVisibilityFromGoneToVisible() throws Exception {
        final View view = Mockito.mock(View.class);
        when(view.isShown()).thenReturn(false);

        presenter.reverseViewVisibility(view);

        Mockito.verify(view, Mockito.atLeastOnce()).setVisibility(View.VISIBLE);
        Mockito.verify(presenter.getView(), Mockito.atLeastOnce()).setButtonText(anyString());
    }

Lets move on to testing our "Model." As before, we start by adding the @RunWith (PowerMockRunner.class) annotation on top of our class.


@RunWith (PowerMockRunner.class)
public class UtilsTest {
    // ...
}

As mentioned previously, our Utils class first checks if the view is not null. If not, a visibility is applied, otherwise nothing is done.

The tests for this class are very easy, so I am just going to put the whole thing here without stepping through it line by line.


@RunWith (PowerMockRunner.class)
public class UtilsTest {

    @Test
    public void testShowView() throws Exception {
        final View view = Mockito.mock(View.class);

        Utils.showView(view);

        Mockito.verify(view).setVisibility(View.VISIBLE);
    }

    @Test
    public void testHideView() throws Exception {
        final View view = Mockito.mock(View.class);

        Utils.hideView(view);

        Mockito.verify(view).setVisibility(View.GONE);
    }

    @Test
    public void testShowViewWithNullView() throws Exception {
        Utils.showView(null);
    }

    @Test
    public void testHideViewWithNullView() throws Exception {
        Utils.hideView(null);
    }
}

I'll explain what is going on in the testShowViewWithNullView() and testHideViewWithNullView() methods. Why would we want to test something like this? Well if we think about it for a second, we do not want our method calls to crash the entire application because the view was null.

Lets take a look at the Utils showView() method. If we remove the null check, the app will throw a NullPointerException and would crash.


public final class Utils {

    // ...
    
    public static void showView(View view) {
        if (view != null) {
            view.setVisibility(View.VISIBLE);
        }
    }
    
    // ...
}

In other scenarios we may want the app to actually throw an exception. How would we test an exception? Its actually very easy: You would just pass in an expected param to the @Test annotation like so:


@RunWith (PowerMockRunner.class)
public class UtilsTest {

    // ...

    @Test (expected = NullPointerException.class)
    public void testShowViewWithNullView() throws Exception {
        Utils.showView(null);
    }
}

If no exception is thrown, the test would fail.

Again, you can find all of the code on GitHub.

Now that we are at the end of the post, I want to mention something to you guys: Testing will not always be as straightforward as it was in this example, but it doesn't mean it can't, and shouldn't be done. As software developers, we need to make sure that our applications work properly and as expected. We need to make sure others trust our code. I have been doing this for many years and you couldn't imagine how many times tests have saved me, even for the simplest thing such as changing a view ID.

No one is perfect, but tests help us get a step closer. Keep on coding, keep on testing and until next time! 

Josias.jpg Josias Sena  Android Developer at RapidSOS.

He is a self-taught Android Developer passionate about everything Android and tech. You can find him on his website.

Related Search Term(s): Testing

Create, Design, Develop and Connect at AnDevCon D.C. 2017!

Thoughts? Leave a comment: