Data Driven Testing

In my last blog post I’ve committed myself to writing one blog post every week and it seems I’ve already failed this goal, since there wasn’t any post last week :(. But let us not hesitate in the face of yesterdays defeat and instead continue our quest towards a glorious test-driven string padding library in service of the whole interweb.

The first Spock blog post introduced some of the general concept but now I’d like to show you the really good stuff. One of my favorite Spock features is something called Data Driven Testing. This features allows to run the same tests for different input and expected output data – really useful to easily follow the DRY convention in your test classes and still cover all the edge cases of your production code. There are different ways to use this feature, but I think the easiest and in general most applicable one is to use the Data Tables feature.

Back in the first Spock post I’ve already suggested to extend our test method with additional input and output data in order to test drive the production code towards the real solution. The test method inside PadServiceSpec with applied Data Tables looks like this:

All you have to do is define input and expected output in a table and Spock will automatically generate multiple distinct test cases for you, sweet! Also take a look at the @Unroll annotation – this annotation will tell Spock to report each parameterized run as a distinct result, substituting the #variable in the method name with the used parameter values.

If run with the old production code all tests but the first one will fail of course (I think I’ve defined the most useful permutations). So the next step is to use these tests as the specification for the production code and get into coding the feature until all tests pass.

The following code makes the tests results light up as green as the WindowsXP default wallpaper meadows:

And since IntelliJ IDEA has quite a good support for Spock, this is how the test report looks like inside this marvelous IDE:
alt text

Like the last time, the source code is available on GitHub and should be runnable directly after checkout, thanks to the cool included Gradle-Wrapper (And maybe I should upload this library into Maven-Central to make it includable as an external dependency in a wide variety of projects and other libraries :P).

Sorry for the missing syntax highlighting. It seems like the last WordPress (or Jetpack?) update broke the SyntaxHighlighter Evolved plugin. Any recommendations for good syntax highlight in WordPress?