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:


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:




comments powered by Disqus