Ultimate Kotlin TDD FizzBuzz

Published: 02 August 2024
on channel: Pairing with Duncan
785
50

I’m currently helping a less experienced developer (hi Connor) learn Test Driven Development, and this week we looked at the FizzBuzz kata.

FizzBuzz is a really good introduction to TDD. Because there is really no complication to get bogged down in, we can focus on the following the process.

1. Write a test that the production code doesn’t pass. The test should show the smallest useful increment in behaviour towards our goal.
2. Change the production code to pass the test in the simplest way possible.
3. If necessary refactor production code or tests (but not both at once) to improve their design while still passing the tests.
4. Repeat until done.

This video adds an element that I haven’t seen from other FizzBuzzers, which is acceptance tests. Ideally we will write at least some of our acceptance tests before the main TDD cycle. They should be part of the specification that we agree with the product owner, and when they pass we know that this story is done, barring any final refactoring.

In the bonus refactoring, we’ll look at how to output all the fizzbuzzes, or at least the first 2^63 - 1 of them, with, full disclosure. another accidental 2^63 - 1 negative ones. Is that a bug? It turns out even TDD doesn’t guarantee software quality!

In this episode

00:01:20 Read the spec
00:02:02 Create an acceptance test
00:02:38 Replacing stdout to capture printing
00:05:09 IntelliJ Introduce Variable bug
00:06:40 Have our Acceptance test call main
00:07:07 Test the output is as per the spec
00:08:59 Disable the acceptance tests until we are done
00:09:39 Create the simplest unit test we can
00:10:24 Our interface for unit tests is different
00:11:18 Make the test pass in the simplest way possible
00:11:55 Outside in meets inside out
00:14:28 Test 3 is Fizz
00:16:14 We only add complication backed by a test
00:16:39 Testing can encourage structure
00:18:02 Refactor our tests (on green) to be simpler and broader
00:19:28 Run the acceptance tests when we think we are done
00:20:09 Improve the acceptance tests to make sure
00:22:11 Can we improve the design?
00:22:59 Sequences are better for lots of data
00:26:45 Wrap up

The code is on GitHub - https://github.com/dmcg/FizzBuzz/

There is a playlist of FizzBuzz episodes    • FizzBuzz  

I get lots of questions about the test progress bar. It was written by the inimitable @dmitrykandalov. To use it install his Liveplugin (https://plugins.jetbrains.com/plugin/...) and then this gist https://gist.github.com/dmcg/1f56ac39...

If you like this video, you’ll probably like my book Java to Kotlin, A Refactoring Guidebook (http://java-to-kotlin.dev). It's about far more than just the syntax differences between the languages - it shows how to upgrade your thinking to a more functional style.


Watch video Ultimate Kotlin TDD FizzBuzz online without registration, duration hours minute second in high quality. This video was added by user Pairing with Duncan 02 August 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 785 once and liked it 50 people.