android6-2 Exercise 1: ViewModel tests =================================== // Module build.gradle.kts (versions are placeholders; the Compose test libraries follow your Compose BOM) dependencies { // Local tests (src/test): run on your computer testImplementation("junit:junit:") testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:") // Instrumented tests (src/androidTest): run on a device or emulator androidTestImplementation("androidx.compose.ui:ui-test-junit4") androidTestImplementation("androidx.test.ext:junit:") androidTestImplementation("androidx.room:room-testing:") // Room 3.0: room3-testing // Needed by createComposeRule(): the test manifest, in debug builds only debugImplementation("androidx.compose.ui:ui-test-manifest") } import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.TestDispatcher import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.setMain import org.junit.rules.TestWatcher import org.junit.runner.Description // Replaces Dispatchers.Main for every test that uses it (docs: Dispatchers.setMain) @OptIn(ExperimentalCoroutinesApi::class) class MainDispatcherRule( private val dispatcher: TestDispatcher = UnconfinedTestDispatcher() ) : TestWatcher() { override fun starting(description: Description) = Dispatchers.setMain(dispatcher) override fun finished(description: Description) = Dispatchers.resetMain() } // FakeNotesRepository: let a test choose what refresh() returns class FakeNotesRepository : NotesRepository { var refreshResult: RefreshResult = RefreshResult.Success // ...the notes flow, addNote and deleteNote from Chapter 5... override suspend fun refresh(): RefreshResult = refreshResult } // app/src/test/.../NotesViewModelTest.kt (a local test: fast, no emulator) class NotesViewModelTest { @get:Rule val mainDispatcherRule = MainDispatcherRule() private fun viewModel(repository: FakeNotesRepository = FakeNotesRepository()) = NotesViewModel(repository, FakeSettingsRepository()) @Test fun emptyNoteShowsMessage() = runTest { val vm = viewModel() backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { vm.uiState.collect { } } vm.addNote(" ") assertEquals("A note cannot be empty", vm.uiState.value.userMessage) } @Test fun failedRefreshShowsMessage() = runTest { val repository = FakeNotesRepository().apply { refreshResult = RefreshResult.NoConnection } val vm = viewModel(repository) backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { vm.uiState.collect { } } vm.refresh() assertEquals("No connection", vm.uiState.value.userMessage) } } Third test (sort order): give FakeSettingsRepository a public way to set the flow, add two notes with different createdAt values, then: @Test fun oldestFirstReversesTheOrder() = runTest { val settings = FakeSettingsRepository() val vm = NotesViewModel(FakeNotesRepository(), settings) backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { vm.uiState.collect { } } settings.setNewestFirst(false) val times = vm.uiState.value.notes.map { it.createdAt } assertEquals(times.sorted(), times) } Report whether they pass and the time. Local tests should take seconds. If uiState.value never changes, check that the collector is started (WhileSubscribed). Code unrun.