Automated testing in Android has become a huge trend in the last years, with many recommendations about what to test, how to test and when to test. Also, there are many tools that can help you with this, so how do we choose between Espresso, Robolectric, JUnit etc ? This article will explain the different tools that can be used, will explain why we need both unit and integration tests and focus on the integration tests.
Tools of the Trade
- JUnit. A Popular unit-testing framework for Java, provides simple and convenient APIs to write tests and perform common testing operations such as setup, teardown, and assertions with ease.
- Mockito/Easymock. The core idea of unit tests is to test a unit of code in isolation from any of its dependencies. When you are unit testing a piece of code, you really don’t care if its dependencies work properly or not, since you will need to test them separately anyway. Mockito or Easymock help you mock these dependencies.
- Dependency injection Framework. If you find your codebase littered with the “new” keyword everywhere, be sure that you will have a hard time using Mockito or Easymock to mock your dependencies. Dependency injection framework such as Dagger and Toothpick can help you with this.
- Espresso is an excellent framework developed by Google to help you easily and quickly automate UI tests that simulate real user behavior. These tests will run on a real device or emulator.
- Robolectric makes it possible to run Android tests right on your local machine by providing a dummy implementation of the Android SDK using shadow classes.
- MockWebServer is part of OkHttp. With MockWebServer, you can easily mock server responses including body, headers, status code etc.