Testing Kotlin — Curso De

class MathProps : StringSpec({ "Addition should be commutative" { forAll { a: Int, b: Int -> // The property we want to test (a + b) == (b + a) } } "String length should be non-negative" { forAll<Int> { length -> val str = "x".repeat(length.coerceAtLeast(0)) str.length >= 0 } } })

@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) } } curso de testing kotlin

src/ test/kotlin/ # Unit tests (run fast, no Android/Server) integrationTest/ # Integration tests (use real DB) testFixtures/ # Shared test data (factories, builders) The Golden Rule: runTest Never use Thread

result.shouldBe(200) list.shouldContain("Kotlin") exception.shouldThrow<IllegalArgumentException> Kotlin Coroutines are amazing for production, but they are a nightmare for testing if you don't know the tricks. A naive test will pass when it should fail because the coroutine hasn't finished yet. The Golden Rule: runTest Never use Thread.sleep() in tests. Use kotlinx-coroutines-test . b: Int -&gt