Testing Kotlin !!hot!!: Curso De

@Test fun `adding 2 and 3 should return 5`() { val result = Calculator().add(2, 3) assertEquals(5, result) // Or even nicer: assertNotNull(result) } }

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:

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

Kotest has the best property testing implementation on the JVM.

In this guide, we will move from basic JUnit setup to advanced property-based testing and coroutine simulation. Forget the old @Test annotations that feel clunky. Kotlin allows us to write tests that read like plain English. The Setup (Gradle) // build.gradle.kts dependencies { testImplementation("org.junit.jupiter:junit-jupiter:5.10.0") testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") } The First Test: Clean Syntax Notice how we avoid assertThat(actual).isEqualTo(expected) (Hamcrest) or Assert.assertEquals() (JUnit). Instead, we use Kotlin's infix functions via the kotlin.test library. @Test fun `adding 2 and 3 should return

@Test fun `verify API is called only once`() = runTest { // 1. Create mock val api = mockk<MyApi>() // 2. Stub a suspend function (coEvery) coEvery { api.getData() } returns "Mocked Response" val repo = Repository(api) // 3. Execute val result = repo.refreshData() // 4. Verify (coVerify) coVerify(exactly = 1) { api.getData() } result shouldBe "Mocked Response" } } Traditional testing (Example-based) says: "Give input 2+2, check output 4." Property-based testing says: "For ALL integers, addition should be commutative."

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 } } }) It is the flagship testing framework for Kotlin,

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 .