Christian Meese
4 min readOct 29, 2016

Test Driven Development (TDD) sounds cool. It sounds like it makes a lot of sense. It seems any developer you talk to wants to do it. Yet when it comes to actually writing code test driven, it can be hard to know how to even start. Getting used to the red-green-refactor flow can be difficult, especially when you have been coding without tests for a while. In TDD by example Kent Beck shows how to build the Fibonacci algorithm using TDD. The code is simple and compact enough that I think it is a great candidate for a minimal TDD kata, so I started to repeatedly code this little snippet to start my journey towards becoming a better TDD engineer.

Setup

My initial attempts at coding this mini-kata are from memory, having read the TDD book a few weeks ago. Interestingly, my first learnings are not only related to TDD, but also about editor shortcuts and gradle setup. As I was looking for a quick way to set up and execute my java test class, I realized how easy a basic gradle setup is – creating a one line build.gradle file containing only

apply plugin: 'java'

is enough to successfully run

gradle build

in any folder (provided gradle is set up on your machine). Next up is junit which is actually a few more lines in the same file:

Now it’s finally time to write some java. Gradle expects test classes to be located in “./src/test/java/” and we add our package “com/kata/” to the path as well before creating FibonacciTest.java. The following is what I consider the starting point for the Fibonacci kata:

This is where the TDD thought process needs to kick in. We want to write the simplest failing test, make it compile, run successfully, refactor and then start over with writing the next failing test.

Initially, I tried to do the following all from the command line and the vi text editor. This works fine, but with just a basic vi setup, you’ll loose all the niceties of modern IDEs such as proper code completion + templates and instant test execution. Therefore, I recommend to try this kata in your regular development environment in order to create your automatisms and learn about new shortcuts etc. right where you need to. However, also think about using a kata to get to know a different IDE.

Fibonacci Kata

The simplest test for the fibonacci sequence is

f(1) = 1

We’re writing the test first:

This will not compile, because the method fib(1) will not be found. Creating it is as easy as one code completion in IntelliJ / AndroidStudio, so we end up with the below. Note that because of the simple nature of this implementation, the “production” code is placed in the test class. In a more complex environment, we would create a new class in ./src/main/java/ now.

private int fib(int i) {
return 0;
}

We’re still at the red stage, having written code that compiles but does not pass. Getting the code to pass is as simple as returning 1 instead of 0. Note that we’re not using any knowledge about the Fibonacci algorithm at this point. We don’t care that the method is implemented correctly yet, we will fix that as and when we have written tests that show that we’re not quite there yet.

We continue by adding the next test case, which conveniently passes without modifying the production method.

assertEquals(1, fib(2));

This is certainly too good to be true, so adding the following test brings us back to red:

assertEquals(2, fib(3));

Now it is time to actually think about the Fibonacci algorithm, which is defined as fib(n) = fib(n-1) + fib(n-2), so we can fix the test by modifying the production code once again, resulting in the following implementation of fib:

private int fib(int i) {
if (i > 2) {
return fib(i-1) + fib(i-2);
}
return 1;
}

We can now test our implementation with another case that will increase our confidence, so we will add the below line to the test cases:

assertEquals(55, fib(10));

We might think we’re done now, but one thing to carefully test is edge cases, so we add this test:

assertEquals(0, fib(0));

And the test fails! We can fix this by adding another if-clause, which brings us back to green. But the below starts to look a bit messy and does not actually reveal much about how the Fibonacci algorithm works:

private int fib(int i) {
if (i == 0) {
return 0;
}
if (i > 2) {
return fib(i-1) + fib(i-2);
}
return 1;
}

So this time, we don’t omit the refactoring step. Actually, we run the tests multiple times while doing these refactorings, changing as little as possible between each run.

Conclusion

Performing this kata will probably take less time than reading this text, especially after a few repetitions. As you practice, focus on how you transition between the red-green-refactor phases, but also on how you move within your IDE and which shortcuts might be available to speed up your coding.

I hope to follow up on this post with more challenging examples soon. For now, thanks for reading and happy coding. Let me know if you have comments, questions or concerns.

PS: If you’re intrigued by the concept of TDD, i strongly recommend to have a look at the book Test Driven Development by Example by Kent Beck.