350 pages of hands-on tutorial right into your email inbox. Learn how to use state of the art development environment and build a full-fledged command-line application. You will receive free updates to the tutorial. Start learning Kotlin today!
See the code above? It’s actually a runnable JUnit4 test in Kotlin. It’s also a description of business rules readable in English.
Mind. Blown.
Let’s dive in how did I get there.
class GameTest {
@Test
fun `scissors cut paper`() {
val game = Game()
val winner = game.play(Throw.SCISSORS, Throw.PAPER)
assertThat(winner).isEqualTo(Winner.FIRST_PLAYER)
}
}
At this point, the test doesn’t compile: Game, Throw, and Winner are not defined. Let’s create classes and enums for these:
class Game {
fun play(first: Throw, second: Throw): Winner {
TODO("not implemented")
}
}
enum class Throw {
SCISSORS, PAPER
}
enum class Winner {
FIRST_PLAYER
}
Now it compiles. Before we run the test suite, let’s make an assumption of how it’s going to fail. It’s going to fail with the “not implemented” error. Let’s run the test suite and see:
Great. Exactly as expected.
This technique is essential to TDD. It has a name—“Calling a Shot.” If we call a shot, and something unexpected happens, that means that we have a mistake in our test. That is a way to test your tests when writing them.
Let’s make it pass with the simplest implementation. Just return the FIRST_PLAYER
as a winner:
fun play(first: Throw, second: Throw): Winner {
return Winner.FIRST_PLAYER
}
If we run the tests now, they all pass:
Making the test pass with the simplest implementation is what makes TDD produce near 100% test coverages. And I’m not talking about lines or branches—about TRUE test coverage.
Basically, the simplest implementation is when no matter what you “subtract” from it—at least one test will fail. In a sense, writing such an implementation you’re exposing test cases that you didn’t even think about before, and likely wouldn’t have written them as automated tests.
At this point, all our tests are passing. This is called a “Green” stage of the TDD cycle. In this stage, we are free to clean up and refactor anything we want. So we take a look at both production code and test code:
Everything looks fine right now—there is no duplication, names are alright, and there is nothing weird. So we loop back to the first stage of TDD again—“Red” stage.
The next test flows naturally from our current simplest implementation. Basically, the question that we need to ask at this point—how can we prove the current implementation wrong?
Since we are returning the constant value, we can prove the implementation wrong by providing inputs where the expected winner is different. We can do it if we were to reverse SCISSORS
and PAPER
arguments:
@Test
fun `scissors cut paper - reverse`() {
val game = Game()
val winner = game.play(Throw.PAPER, Throw.SCISSORS)
assertThat(winner).isEqualTo(Winner.SECOND_PLAYER)
}
Winner.SECOND_PLAYER
is not defined yet, so we’ll create it as an enum constant:
enum class Winner {
FIRST_PLAYER, SECOND_PLAYER
}
Now, before we are going to run the test, we are going to call a shot. We expect that there will be an assertion error in the second test. The expected winner should be SECOND_PLAYER,
but the actual will be FIRST_PLAYER
since we are returning the same constant always at the moment.
Now, we’re ready to run the test suite:
org.junit.ComparisonFailure:
Expected: SECOND_PLAYER
Actual: FIRST_PLAYER
at rps.GameTest.scissors cut paper - reverse(GameTest.kt:22)
That is precisely what we expected.
Let’s make this test pass by wrapping the existing return statement in the simplest if
statement, and returning the SECOND_PLAYER
right after:
fun play(first: Throw, second: Throw): Winner {
if (first == Throw.SCISSORS) {
return Winner.FIRST_PLAYER
}
return Winner.SECOND_PLAYER
}
If we rerun the test suite, everything should be green now. Let’s look for refactoring opportunities now.
Those pesky fully-qualified names make the code slightly less readable. I think we can omit them by using direct imports. We’ll start with the test code:
import rps.Throw.*
import rps.Winner.*
// ...
@Test
fun `scissors cut paper`() {
val game = Game()
val winner = game.play(SCISSORS, PAPER)
assertThat(winner).isEqualTo(FIRST_PLAYER)
}
@Test
fun `scissors cut paper - reverse`() {
val game = Game()
val winner = game.play(PAPER, SCISSORS)
assertThat(winner).isEqualTo(SECOND_PLAYER)
}
And let’s do the same to the production code:
import rps.Throw.*
import rps.Winner.*
// ...
fun play(first: Throw, second: Throw): Winner {
if (first == SCISSORS) {
return FIRST_PLAYER
}
return SECOND_PLAYER
}
Now that we’ve done the refactoring, we should run the test suite. It is still green! Let’s take a look if we have any duplication we should get rid of:
The first thing that stands out in our test code is that we’re creating the game
object in every test in precisely the same way.
The rule of thumb for refactoring the duplication is that we need three occurrences. Here though, you can already tell that the next test will need to create the same object in the same way. So we might as well refactor right now.
Extracting the variable as a private field of the class works here:
class GameTest {
private val game = Game()
@Test
fun `scissors cut paper`() {
val winner = game.play(SCISSORS, PAPER)
assertThat(winner).isEqualTo(FIRST_PLAYER)
}
@Test
fun `scissors cut paper - reverse`() {
val winner = game.play(PAPER, SCISSORS)
assertThat(winner).isEqualTo(SECOND_PLAYER)
}
}
Right after making this atomic change, we should run the tests. And they still pass!
Now, the next duplication is calling to game.play
with two throw arguments and then asserting the result is correct. Again, there are only two occurrences so far, but we can easily tell that every test will have the same snippet of the code.
Let’s extract the private method assertGame:
private fun assertGame(first: Throw,
second: Throw,
expected: Winner) {
val actual = game.play(first, second)
assertThat(actual).isEqualTo(expected)
}
And the tests that are now using this function look like this:
@Test
fun `scissors cut paper`() {
assertGame(SCISSORS, PAPER, FIRST_PLAYER)
}
@Test
fun `scissors cut paper - reverse`() {
assertGame(PAPER, SCISSORS, SECOND_PLAYER)
}
Let’s verify that we didn’t break anything by rerunning the test suite. It’s all green—great!
Now if you look carefully, these two tests are actually describing a single business rule, and they are simple one-liners. And we can already tell that every rule of the Rock-Paper-Scissors game will come in a pair like this.
Let’s put these two lines together and get rid of the “reversed” test:
@Test
fun `scissors cut paper`() {
assertGame(SCISSORS, PAPER, FIRST_PLAYER)
assertGame(PAPER, SCISSORS, SECOND_PLAYER)
}
And if we run the test suite—it still passes. At this point, we have eliminated the duplication. And spelling out the next business rule will be easy.
Watch:
@Test
fun `paper covers rock`() {
assertGame(PAPER, ROCK, FIRST_PLAYER)
assertGame(ROCK, PAPER, SECOND_PLAYER)
}
And to make this pass, we can just add second == ROCK
to our if
condition:
fun play(first: Throw, second: Throw): Winner {
if (first == SCISSORS || second == ROCK) {
return FIRST_PLAYER
}
return SECOND_PLAYER
}
While there is no duplication, the test still doesn’t read very well. At this point, we can play with a few options. For example, create a custom AssertJ assertion:
assertThat(SCISSORS).beats(PAPER)
That will expand to two assertGame
calls. Another option is to use Kotlin’s infix extension functions that make English like sentences a piece of cake. Let’s implement a beats
infix function for Throw
enum:
private infix fun Throw.beats(other: Throw) {
assertGame(this, other, FIRST_PLAYER)
assertGame(other, this, SECOND_PLAYER)
}
And now we can replace double calls to assertGame
in our tests:
@Test
fun `scissors cut paper`() {
SCISSORS beats PAPER
}
@Test
fun `paper covers rock`() {
PAPER beats ROCK
}
Again, we’re running into the same problem: the code itself already telling us in English what the test is about so we can merge these tests into one:
@Test
fun `game rules`() {
SCISSORS beats PAPER
PAPER beats ROCK
}
Adding the final pair is a no-brainer now:
@Test
fun `game rules`() {
SCISSORS beats PAPER
PAPER beats ROCK
ROCK beats SCISSORS
}
Now if we run the test—it fails because:
org.junit.ComparisonFailure:
Expected: FIRST_PLAYER
Actual: SECOND_PLAYER
at rps.GameTest.assertGame(GameTest.kt:28)
at rps.GameTest.beats(GameTest.kt:19)
at rps.GameTest.game rules(GameTest.kt:15)
Oh. I’m not sure. Of course, following the stacktrace, I could figure this out, but it is not immediately apparent.
This test failure sucks. So this is the other side of the trade-off of the abstraction in the test. You also have to make the failure more readable by giving more context in the failure message:
// inside of assertGame function:
assertThat(actual)
.withFailMessage("""
For a game $first vs. $second, the winner should be $expected
Expected: $expected
Actual: $actual
""".trimIndent())
.isEqualTo(expected)
Let’s rerun the test suite and take a look at the failure message:
java.lang.AssertionError: For a game ROCK vs. SCISSORS, the winner should be FIRST_PLAYER
Expected: FIRST_PLAYER
Actual: SECOND_PLAYER
at rps.GameTest.assertGame(GameTest.kt:35)
at rps.GameTest.beats(GameTest.kt:19)
at rps.GameTest.game rules(GameTest.kt:15)
Oh, this is much better!
if
statement to match the game rules (Green)To make this assertion pass, we need to add a condition when the ROCK
is the first
throw:
if (first == SCISSORS ||
second == ROCK ||
first == ROCK) {
return FIRST_PLAYER
}
That fails with “For a game ROCK vs. PAPER, the winner should be SECOND_PLAYER.” Let’s handle this case by clarifying that not only ROCK
should be the first throw, but also SCISSORS
has to be the second one:
if (first == SCISSORS ||
second == ROCK ||
first == ROCK && second == SCISSORS) {
return FIRST_PLAYER
}
If we follow all these failures one by one, eventually we arrive at the following if
statement that is absolutely symmetrical to the test:
Great! Remember, the winning conditions do not describe the full set of game rules. We also need to handle ties:
Let’s follow the same pattern and add the helper function for the “tie” rule:
private infix fun Throw.tiesWith(other: Throw) {
assertGame(this, other, TIE)
assertGame(other, this, TIE)
}
And let’s write all the rules for it (skipping a few cycles of TDD here to keep it short):
@Test
fun `game rules`() {
SCISSORS beats PAPER
PAPER beats ROCK
ROCK beats SCISSORS
SCISSORS tiesWith SCISSORS
PAPER tiesWith PAPER
ROCK tiesWith ROCK
}
It seems like the simplest solution for all three “tie” assertions would be to compare first throw with the second one, and see if they are equal:
fun play(first: Throw, second: Throw): Winner {
if (first == second) {
return TIE
}
if (first == SCISSORS && second == PAPER ||
first == PAPER && second == ROCK ||
first == ROCK && second == SCISSORS) {
return FIRST_PLAYER
}
return SECOND_PLAYER
}
If we run our tests now, they’ll all pass. Fantastic!
If you want to, you can go even further and introduce custom verbs for each of the Throw types. I wouldn’t recommend this for pragmatic reasons—it’s an overkill and will make people ask WTF at the rate of 20/s.
You can do it, and it is quite simple. Just introduce aliases for the beats
function. Let’s do it for the SCISSORS cut PAPER
only:
private infix fun Throw.cut(other: Throw) = beats(other)
// usage:
SCISSORS cut PAPER
For most of the involved domains, you might want to have custom verbs like that. I yet to see one though.
Thank you so much for reading. I hope you learned something new. To make me entirely happy, share this article on social media with your friends and colleagues.
Where do you see yourself using this in your test suite? Leave the comment below!
Oleksii helps developers create applications in the Kotlin language. Grab his Ultimate Tutorial: Getting Started With Kotlin: https://iwillteachyoukotlin.com