When writing new code, it makes sense that it should be documented by the person closest to it: the author. That’s my rationale for developer testing. – Noah Sussman Testing With Kotlin Why do we need tests?
TDD test framework for Kotlin • www.github.com/jetbrains/Spek https://github.com/JetBrains/spekhttps://github.com/JetBrains/spek Testing With Kotlin Why Spek?
player guesses the number will be even, the other guesses it will be odd. • The two players pick a number and the winner will be the one that guessed the parity of the sum of those numbers Testing With Kotlin Spek Demo - “Par ou Ímpar” Game
= OddEvenGame() afterEach { oddEvenGame.clear() } context("when odd player picks an odd number") { beforeEach { oddEvenGame.recordChoice(Player.ODD, 3) } context("when even player picks a even number") { beforeEach { oddEvenGame.recordChoice(Player.EVEN, 4) } it("should declare the odd player as the winner") { assertEquals(Result.ODD_PLAYER, oddEvenGame.winner) } } context("when even player picks an odd number") { beforeEach { oddEvenGame.recordChoice(Player.EVEN, 3) } it("should declare the even player as the winner") { assertEquals(Result.EVEN_PLAYER, oddEvenGame.winner) } } (...) } Testing With Kotlin Spek Demo - “Par ou Ímpar” Game
= OddEvenGame() afterEach { oddEvenGame.clear() } context("odd player picks an odd number") { beforeEach { oddEvenGame.recordChoice(Player.ODD, 3) } context("even player picks a even number") { beforeEach { oddEvenGame.recordChoice(Player.EVEN, 4) } it("should declare the odd player as the winner") { assertEquals(Result.ODD_PLAYER, oddEvenGame.winner) } } } } context("when even player picks an odd number") { assertEquals(Result.EVEN_PLAYER, oddEvenGame.winner) } } (...) Testing With Kotlin Spek Demo - “Par ou Ímpar” Game
= OddEvenGame() afterEach { oddEvenGame.clear() } context("odd player picks an odd number") { beforeEach { oddEvenGame.recordChoice(Player.ODD, 3) } context("even player picks a even number") { beforeEach { oddEvenGame.recordChoice(Player.EVEN, 4) } it("should declare the odd player as the winner") { assertEquals(Result.ODD_PLAYER, oddEvenGame.winner) } } context("even player picks an odd number") { beforeEach { oddEvenGame.recordChoice(Player.EVEN, 3) } it("should declare the even player as the winner") { assertEquals(Result.EVEN_PLAYER, oddEvenGame.winner) } } } (...) } Testing With Kotlin Spek Demo - “Par ou Ímpar” Game
= OddEvenGame() afterEach { oddEvenGame.clear() } context("odd player picks an odd number") { beforeEach { oddEvenGame.recordChoice(Player.ODD, 3) } context("even player picks a even number") { beforeEach { oddEvenGame.recordChoice(Player.EVEN, 4) } it("should declare the odd player as the winner") { assertEquals(Result.ODD_PLAYER, oddEvenGame.winner) } } context("even player picks an odd number") { beforeEach { oddEvenGame.recordChoice(Player.EVEN, 3) } it("should declare the even player as the winner") { assertEquals(Result.EVEN_PLAYER, oddEvenGame.winner) } } } (...) } Testing With Kotlin Spek Demo - “Par ou Ímpar” Game
isOffline(): Boolean fun login(userEmail: String, password: String) fun logout() } data class UserInteractor( private val webService: WebService, private val email: String, private val password: String) { fun login() { if (!webService.isOffline()) { webService.login(email, password) } } fun logout() { if (!webService.isOffline()) { webService.logout() } } }
and Equals ◦ Static methods ◦ Final classes ◦ Final methods https://github.com/dpreussler/kotlin-testrunner https://github.com/dpreussler/kotlin-testrunner Testing With Kotlin Mockito
and Equals ◦ Static methods ◦ Final classes ◦ Final methods https://github.com/dpreussler/kotlin-testrunner https://github.com/dpreussler/kotlin-testrunner Testing With Kotlin Mockito https://github.com/dpreussler/kotlin-testrunner