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.
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:
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").
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.
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
If you want to go deeper into Spock, here are the examples from the session and some interesting links:
- # 101PanelTechDays slides: http://www.slideshare.net/ilopmar/101-panel-tech-days-spock-o-por-qu-deberas-utilizarlo-para-testear-tu-cdigo-java
- Code from # 101PanelTechDays on Github: https://github.com/ilopmar/101panel-spock
- Spock Official Website: http://spockframework.org
- Official Spock Documentation: http://spockframework.org/spock/docs
- T3chFest 2016 - Iván López - From Java to Groovy: Adventure Time!