import kotlinx.coroutines.test.runTest import kotlinx.coroutines.delay class ApiClientTest {

Use backticks to write sentences as test names. Your test reports will read like documentation. Module 2: The Game Changer – Kotest If you take only one thing from this curso , let it be Kotest . It is the flagship testing framework for Kotlin, replacing JUnit with a radically different syntax. Why Kotest? It supports Spec styles (BehaviorSpec, StringSpec, FreeSpec) and Property Testing out of the box. Example: The Behavior Spec class UserServiceTest : BehaviorSpec({ val service = UserService() given("A user with a valid email") { val email = "test@example.com" `when`("I call register") { val result = service.register(email) then("It should return a success message") { result shouldBe "User created" } and("The user should be stored in the DB") { service.exists(email) shouldBe true } } } }) Assertions: shouldBe vs shouldNotBe Kotest replaces assertEquals with infix functions:

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 {

// Real code class ApiClient { suspend fun fetchUser(id: String): User { delay(3000) // Simulate network return User("John Doe") } }