Select Page

My 6 reasons to switch to Spock

Although it tends to be assumed that unit tests they are a natural product of the software's own construction, it really is a practice that must be taken care of, as well as a clean architecture or some requirements aligned with the expected value. Historically the development of unit tests it has always been tedious, unrewarding, and unrecognized work. Thanks to Spock This is changing and in this article we will explain it to you.

Spock it's a framework testing to test Java and Groovy applications that makes writing tests not as complex, boring and verbose as with others testing frameworks Traditional.

Thanks to its expressive DSL and the power of language, Groovy makes it possible for us to write tests in a very simple, clear and easily readable way even by the business clumsy :-). It is inspired by other testing frameworks, languages ​​and technologies, but giving them one more twist and keeping the best of each house:

1 Compatibility:

Spock supports JUnit, so we can easily integrate it with our standard tools that we are currently using as our IDE (Eclipse, Netbeans), our continuous integration environment (Jenkins, Bamboo), or with our project management and construction tool (Maven, Gradle).

2. Descriptive tests:

No more cryptic test names on CamelCase that are hard to read. With Spock we write descriptive sentences of what the test does thanks to its style syntax DB: given - when - then.

3. The Power of Groovy:

makes it easy to handle data structures like lists and maps with very little code. In Java it would take us a few extra lines of code, making our tests more complex and less readable.

4. Data Driven Tests:

Ease of testing a piece of code with many different values ​​(tables, lists, maps ... or any other data structure that is "iterable").


Unit test using data tables

5. Power assertions:

Thanks to them, in the execution of each test, the values ​​of how the assert has been evaluated step by step are reported in a very visual way. This saves us from filling our tests with logs or having to debug them to see what is happening.

Java testing with Spock

Report a failed test using Power Asserts

6.Test doubles:

Spock also makes it easy for us to create double tests without the need to include additional libraries (such as Mockito in the case of JUnit) to be able to create mocks in our tests very easily.


In summary, Java Testing with Spock makes it easier for us to approach software testing. And of all this he spoke to us Ivan Lopez (@ilopmar) in the last talk 101 Panel Tech Days. Although the session was about SpockIvan cannot, nor does he want to, hide his love for Groovy. A contagious passion that encourages you to "give it a try" to software development with Groovy. No doubt in Panel Sistemas has already managed to place it in the "ToDo" of several of the attendees, with the development of software unit tests being an excellent starting point.

We leave you here the video of his talk, in which he shows us Java Tests with Spock or why relying on Spock to test Java code is a winning bet. The solvency with which Iván navigates between the practical examples he uses is one more evidence of the improvement in productivity to which we must aspire with Spock in our toolbox.

Java testing with Spock

Watch Video


If you want to go deeper into Spock, here are the examples from the session and some interesting links:

David Vazquez Novella

David Vazquez Novella

David is a Development Engineer at Panel Sistemas. You can contact him via e-mail in this direction.

Leave us your comment


Share This