Why to use Spock

From Spock documentation:
Spock is a testing and specification framework for Java and Groovy applications. What makes it stand out from the crowd is its beautiful and highly expressive specification language.

Spock in an amazing testing framework, which I believe is a better replacement for JUnit library. Using Spock will make your tests not only easier to write, but what is more important, it will make them easier to read and understand their results.

What could be the biggest challenges to switch from JUnit to Spock:

  1. Spock is using Groovy (not Java) so you need to learn new language.
  2. Spock is not JUnit so it is not compatible with other software (i.e. IDE, build tools, continuous integration tools).

Fortunatelly, the above statements are false!

  1. It is true that Spock is using Groovy, but Java code is (in like 99%) valid Groovy code, so Groovy has extremely low entry point for Java developers. Just start writing your tests using Java-like syntax and you can move to use Groovy features gradually.
  2. Actually, Spock is like JUnit when it comes to running tests. It’s because Spock is using JUnit runner which is supported by most (if not all) Java IDEs, continuous integration and build tools (Maven/Gradle). So… just write your tests in Spock, but run them the same way as JUnit tests.

OK, so it’s fine… now you know why not to avoid Spock, but why to invest your time (even if it’s not much) in learning new thing when JUnit works fine?

Why to use Spock:

  • Groovy syntax helps to write less code which is more expressive.
  • Parameterising tests using data tables is easy and very readable (even test names can be parametrised) (see example below).
  • Still on Java 7 or earlier? – features of the language like lambdas (which are unavailable before Java 8) can be used in tests even for JDK 6 projects.
  • Build-in mocking is powerful, more expressive and much easier to use than EasyMock.
  • Still you can use EasyMock or Mockito if you prefer.
  • Test structure (Given/When/Then) is enforced, so all tests will have the same structure, making it easier to understand after some time.
  • More developed setup / cleanup configuration in tests, makes possible to e.g. override cleanup per test or share it by multiple test classes.
  • Because of the Groovy Power Assertions feature, writing assertions is extremely easy and readable (JUnit/Hamcrest/AssertJ/Fest Assert cannot be even compared with that)(see example below).
  • Result printed in case of test failure contains values of all variables used in assert, which makes understanding what went wrong much easier (see example below).
  • It’s possible to generate human readable reports from tests (especially useful for acceptance tests).
  • Nice support for testing REST endpoints (see example below).
  • Spring supports Spock for integration tests (see here).
  • Spock is really well documented.
  • Data structures syntax from Groovy is much easier to write and read (see example below).
  • It is more fun writing code in Spock with Groovy than in JUnit with Java 🙂 .


I could add more items on this list, but instead here are a few examples of the Spock greatness:

Parameterised test example:

Power assertion example:

REST endpoint testing example:


I highly recommend anyone who is using Java to write tests in Spock instead. It is really easy to try and I believe, you will quickly realise how much better it is.

More about Spock to read and watch:

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *