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.
Figure 5-1. Sample unit test generated by Android Studio, under app/src
The generated test is shown in Example 5-1.
Example 5-1. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access