Creating a Test Class

The most convenient way to write unit tests is within a testing framework. The framework makes it easier to write and run a suite of tests together and see their output in Android Studio.

JUnit is almost universally used as a testing framework on Android and has convenient integrations into Android Studio. Your first job is to create a class for your JUnit tests to live in. To do this, open up SoundViewModel.kt and key in Command-Shift-T (Ctrl-Shift-T). Android Studio attempts to navigate to a test class associated with the class you are looking at. If there is no test class (as is the case here), you are given the option to create a new test class (Figure 20.1).

Figure 20.1  Trying to open a test class

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.