Curso De Testing Kotlin ((better)) -

src/ test/kotlin/ # Unit tests (run fast, no Android/Server) integrationTest/ # Integration tests (use real DB) testFixtures/ # Shared test data (factories, builders)

Use TestDispatcher and advanceUntilIdle() to control time precisely. Module 4: Mocking with MockK (Not Mockito) If you come from Java, you know Mockito. For Kotlin, you need MockK . Why? Because Mockito fails when dealing with final classes (Kotlin classes are final by default) and suspend functions. Mocking a suspend function import io.mockk.coEvery import io.mockk.coVerify import io.mockk.mockk class RepositoryTest { curso de testing kotlin

@Test fun `state flow emits new values`() = runTest { val viewModel = MyViewModel() val collector = viewModel.stateFlow.test { viewModel.doAction("Click") // Await the next emission val item = awaitItem() assertEquals("Loading", item.status) // Ensure no extra items came cancelAndIgnoreRemainingEvents() } } A professional Kotlin project separates test sources by type: src/ test/kotlin/ # Unit tests (run fast, no

// Real code class ApiClient { suspend fun fetchUser(id: String): User { delay(3000) // Simulate network return User("John Doe") } } If it fails, it the input to the smallest failing case

Kotest will automatically generate 1000 random pairs of integers (including negative, zero, positive) and run the test. If it fails, it the input to the smallest failing case. Module 6: Testing Flows & SharedFlows Testing StateFlow and SharedFlow requires collecting values in a controlled way. Use TestCollector or launchIn .

@Test fun `fetchUser returns data after network call`() = runTest { val client = ApiClient() // This virtual time will skip the delay instantly. val user = client.fetchUser("123") assertEquals("John Doe", user.name) } }