Chapter 5. Testing
5.1 Unit Testing
Problem
You want to test the non-Android parts of your app.
Solution
Use the experimental unit testing support added in version 1.1 of Android Studio and the Gradle plug-in for Android.
Discussion
The Eclipse Android Development Tools (ADT) plug-in only supported integration tests, and required developers to create a separate project just for the tests themselves. One of the advantages of the switch to Android Studio and Gradle was support for tests inside the Android project itself.
Prior to version 1.1 of Android Studio and the associated Gradle plug-in, however, those tests were still restricted to integration tests, meaning you needed either an emulator or a connected device in order to run the tests. Integration tests can be very powerful and useful, and are the subject of Recipes 5.3 and 5.4.
This recipe discusses true unit tests, which run on a local JVM on a development machine. Unlike the integration tests that use an androidTest
source set, the unit tests reside in the src/test/java directory of your app.
When you generate a new Android app in Android Studio, a sample unit test is provided for you. It resides in the src/test/java tree, but is not currently in the classpath, as Figure 5-1 shows.
The generated test is shown in Example 5-1.
Example 5-1. ...
Get Gradle Recipes for Android now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.