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?

Iterative Blog Development

Originally I wanted my next blog post to continue the previous one about testing Java with Groovy and Spock, but since I’m currently enrolled in the free blogging course by Simple Programmer founder John Sonmez, I felt obligated to follow the call of duty in my current homework assignment and write some words about the aforementioned course (and receive a free backlink in return, yay!)

I’m a big advocate of agile software development methodologies (please take note of the correct usage of the word agile, Dave Thomas would be proud) and some of my favourite concepts are short development cycles and frequent releases. It’s kind of funny that John gives some similar advice in this course regarding blogging, like a constant (and frequent) blogging interval – and to be totally honest, this constant interval, to which I have committed myself to, is the only reason I’m currently writing this blog post, although I’m really tired from participating at today’s Magic: The Gathering Pre-Release-Event of Shadows Over Innistrad. Failing my first release date wasn’t how I wanted to start my blogging career and so I had to pull myself together, open my text editor (GitHub’s Atom editor with markdown syntax highlight – that’s what I call a clutter free writing experience!) and get started on writing.

This course (combined with John’s book Soft Skills: The software developer’s life manual) helped me kickstart my blog as well as provided me with the framework to deliver one blog post every week. So far, I’d call this a quite positive course outcome and if everything goes according to plan, the next blog post should be released in one week 😉