Writing Testable Android MVVM App: Prelude

Brian Lee
3 min readNov 4, 2015

--

I ran into @vishnurajeevan at a few meetups (here and here), and he talked to me about how he’s been using MVVM on Android and how it’s been turning out well for him.

Inspired by the discussion, I looked up a lot of great resources (like this, this, and this), and I soon realized that writing a testable Android MVVM app would be quite an interesting journey. So I decided to start a project.

Why?

Testing always has been a big part of my development life. In Motorola, before the days of smartphones, I worked in the Multimedia Engine team. Every now and then, we’d get JSR-135 MMAPI specification issues near ship dates. So I created a J2ME MIDlet app to test the JSR-135 spec.

When working on the Orbitz Android App, I worked with Robolectric, re-wrote our Robotium test suite, and re-wrote the test suite again when Espresso 1.0 came out.

At Trunk Club, I wrote around 150 unit tests and 270+ Espresso 2.0 test cases for the Trunk Club Android app. And no, I’m not a test engineer =).

Espresso is great, and with Spoon, it becomes even more cool. However, I was never 100% satisfied with how I tested Android apps.

The Project

I wanted to explore an Android app that:

  1. Has (almost) no business logic in the Activity / Fragment.
  2. Has fully unit testable ViewModels, including rotation (sorry Espresso!).
  3. Uses Android Data Binding.
  4. Has minimum Espresso testing, solely to verify the bindings.

Of course, the main reason for the project is because I want to learn. At the same time, I hope to start something that can serve as a basis and a sample for writing testable Android MVVM app, and get community feedback to make it better and start a discussion. Even if it ends up being a total failure, well, at least we learn what not to do =)

The Plan

Over the next few weeks, I will take a simple Android app, and iterate over it to make a testable Android MVVM app that fulfills the project goals above. My tentative plan is as follows:

  1. Write a standard Android app with TextView, Button, and RecyclerView.
  2. Create a MvvmApp Library to serve as a basis.
  3. Iterate the app to use the MvvmApp library and implement MVVM.
  4. Write unit tests.
  5. Write Espresso tests.

The goal of this journey is to share what it’s like to create a testable Android MVVM app and to get feedback on how it can be done better. As some of you can imagine, I will bring in Dagger2 and Mockito at some point, and I also hope to build more complex examples involving RxJava, Retrofit, and Parse.

The Standard Android App

The standard Android app for this post is tagged as prelude and available on GitHub. As I iterate, I will continue tagging the commits so it’s easier to see what the major changes are. In the next post, I will take this Android app and iterate to bring some MVVM into it.

Ready to move on to Part 1?

--

--