Setting Up Your Test

Now to build out your SoundViewModel test. Android Studio has created a class file for you, called SoundViewModelTest.kt. (You can find this class under the source set labeled test inside your app module.) The template starts out with a single function called setUp():

    class SoundViewModelTest {

        fun setUp() {

A test needs to do the same work for most objects: build an instance of the object to test and create any other objects that object depends on. Instead of writing this same code for every test, JUnit provides an annotation called @Before. Code written inside a function marked @Before will be run once before each test executes. By convention, most JUnit test classes have one function marked ...

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.