Write unit tests with Spock: a tutorial to get you started

I like Spock Framework. I love it for its clarity, brevity and ease of adoption. So, I prepared a ‘lab’ style tutorial for Java programmers to convince them that it is beneficial to start using Spock.

Get started right away with the tutorial by cloning the github repo, or by downloading the zip and typing ‘./gradlew test’ in the homedir. Make sure to read the README.md file in the root directory before you start.

These are my top reasons to use Spock over jUnit + Mockito:

  • A unit test is documentation, so readability matters. Spock tests make intuitive sense at first glance. This is a huge #win.
  • Writing tests is an integral part of software development. If you can write them more quickly, you win lots of time. Spock’s DSL for testing allows precisely that.

Upon completing the tutorial (which should not take you much more than an hour or two) you (a Java programmer) will have learned:

  • how a Spock test is structured
  • that it feels very natural to write tests in that structure
  • that it is not a problem to use Groovy to test code written in Java
  • how easy it is do mock out collaborators of your component under test using Spock
  • Don’t be fooled by the current version number. Spock is mature, I’ve used it for almost 2 years now!

Questions? please post them here and I will try to help you out.

, , ,

5 Responses to Write unit tests with Spock: a tutorial to get you started

  1. jeremy December 23, 2013 at 8:18 pm #

    So where’s the tutorial? I don’t see it in the src.zip

  2. Hans Westerbeek February 2, 2014 at 3:14 pm #

    Hi Jeremy,

    My apologies for the late reply… the tutorial is right there at github repo: https://github.com/hanswesterbeek/spock-intro

Trackbacks/Pingbacks

  1. Questa settimana in Grails (2012-44) - luca-canducci.com - Il blog di Luca Canducci: notizie, tips e nuove tecnologie dal mondo dell’IT. - November 7, 2012

    […] Write unit tests with Spock: a tutorial to get you started […]

  2. An Army of Solipsists » Blog Archive » This Week in Grails (2012-44) - November 9, 2012

    […] Write unit tests with Spock: a tutorial to get you started […]

  3. This Week in Grails (2012-44) - Grails Info - November 9, 2012

    […] Write unit tests with Spock: a tutorial to get you started […]

Leave a Reply