Wednesday, November 12, 2014

Spock - framework for Java developers, who doesn't like Unit testing

I'm Java developer! Coding is base of our profession. It's testing is somewhere nearby. Honestly - I hate writing unit tests. Subconsciously I can not accept the fact that I have to write 20 lines of code in order to test 5. You can make mistake in any code, even the one, which intended to test another one, what about them? In addition you have to know bunch of testing frameworks (JMock, EasyMock, PowerMock), testing standards, best practices, etc.

Please, don't get me wrong, I do not belittle importance of testing. I was writing those tests even without excessive passion. I just didn't like the way I had to do it. And apparently, in one one of our projects client insisted on Spock - another framework for testing, but on Groovy. "Happy client!" - is our company mantra, so we are learning new for us framework, and..... HERE IT IS! For the first time testing become simple, logic, intuitive and FAST!



Let me explain:
  • Tests - is the part of project which is not part of project execution, but requires sizable amount of time for development.
  • Groovy - is an agile and dynamic language for the Java Virtual Machine, builds upon the strengths of Java but has additional power features, which really speed up development but sometimes have negative effect on performance.
  • Spock - is Groovy framework for BDD testing Java and Groovy applications.
We used Spock in our Groovy and Java projects. With less effort than we expected, we managed to increase code coverage with readable and supportable tests in short amount of time.

Before I was trying to convince everyone that Flash is better than JavaScript (yes, I know it is dying, but I like it anyhow) and that Wicket is the best Java Frontend framework. Now this list was supplemented by the best Unit-testing framework.

My findings:
  • Writing tests on Groovy is simpler and faster
  • BDD tests are more logic and readable
  • Spock (BDD tests in Groovy) - makes testing Effective (less time and nerves)

And here is the best presentation I found: