Writing Tests
Now that your initial setUp() function is written, you are ready to write your tests.
A test is a function in your test class annotated with @Test
.
Start by writing a test that asserts existing behavior in SoundViewModel: The title
property is connected to the Sound’s name
property.
Write a function that tests this (Listing 20.8).
Listing 20.8 Testing the title property (SoundViewModelTest.kt
)
class SoundViewModelTest { ... @Before fun setUp() { ... } @Test fun exposesSoundNameAsTitle() { assertThat(subject.title, `is`(sound.name)) } }
Two functions will show up red: the assertThat(…) function and the is(…) function. Key in Option-Return (Alt-Enter) on assertThat(…) and select Assert.assertThat(…) from
Get Android Programming: The Big Nerd Ranch Guide, 4th Edition 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.