File Lottery Kata

When I do some coding, I use test driven approach, have lots of fake and mock objects, and I use dependency injection for putting everything together. I usually don’t work alone, and from time to time I have to introduce these methods to someone with less experience in this particular area. Unfortunately, it takes me a lot of time to find the right example for them, because, frankly, the existing solutions are not that lightweight: spring context with configuration, annotations, different mock libraries, or even fake objects where mocking is impossible.

So I came up with a simple code kata exercise, which allows one to

  • practice the usage of dependency injection

  • try out different dependency injection frameworks

  • practice faking and mocking

  • try out different mocking libraries

The kata is quite simple (15 minutes):

Implement an application which follows the iterator** design pattern. This particular application receives a directory as argument, and at each consecutive call, it returns a file name from the directory that hasn’t been returned before, in a random order. When the application reaches the end of the content of the directory, it shall start over**, again returning the directory contents in a random order. If the directory is empty, then every call shall return an empty string. **If the argument is not a directory but a file, then its name shall be returned on each consecutive call.

The random order makes testing the application quite complicated. In order to do proper testing - have an expected random number in each test case - the random generator shall be injected and mocked (or faked). If you find the right way to do it, then this exercise is pretty straightforward.

A very simple solution with fake test objects is available on my github page.

comments powered by Disqus