The status quo in unit testing

Inertia is the biggest enemy of innovation. People want to work with familiar tools and upgrading to a newer tool is always a big challenge if re-training is required. Even though software development is considered a progressive discipline with a continuous flow of new technologies, there are still some areas when the status quo goes unchallenged.

One such area is the automated testing of Java applications at the code level (also known as unit testing). The undisputed king of unit testing is currently JUnit (http://junit.org) and has been since the beginning of time. JUnit is stable, mature, well known and most importantly a part of the xUnit family of testing frameworks that govern most programming languages.

JUnit is deeply entrenched in the mind of Java developers; some of them even go as far to believe that unit tests and JUnit code are a tautology. JUnit is of course one of the possible ways to perform unit testing, but not the only one. For a brief period in time, TestNG (http://testng.org/) attempted to challenge JUnit out of its throne but without any luck. All innovative features of TestNG were quickly ported to JUnit ending the battle in one swift blow.

Now a new contender has entered the area of testing frameworks: Spock, the groovier testing library (http://docs.spockframework.org)!

Spock:  a new unit testing framework

Spock takes a completely different approach to unit testing, departing from the xUNIT family of testing frameworks.

It is based on the Groovy programming language (http://www.groovy-lang.org/) but can equally test Java as well as Groovy code. The big advantage Spock brings to the table is its holistic approach to unit testing:

Spock has entered the arena after studying the main weakness of JUnit. JUnit is simply not enough for today’s enterprise applications. It can only offer basic unit testing abilities. As soon as further testing requirements appear (such as mocking or BDD style specification), JUnit falls flat.

The usual solution to this limitation is to add more Java libraries. For example, Mockito (http://mockito.org) is a popular solution when it comes to mocking/stubbing (something not offered by JUnit). Other testing frameworks such as Cucumber (https://cucumber.io/) or JBehave (http://jbehave.org/) are also used in the testing mix.

Spock offers instead a comprehensive testing package that covers all the needs of an enterprise application regardless of its size. Unlike JUnit it also comes with an expressive testing language (a testing DSL) that is even understandable by non-technical people. Business analysts will particularly like the Spock testing syntax.

Moreover, Spock comes supercharged with several additional features such as in-context error reporting, tabular parameterized tests and Groovy closure/mocks that bring back the joy of unit testing.

In summary, Spock can function as the main building block in a sound testing process, covering all areas from the basic code testing, up to functional tests and it would be a worthy addition to your toolset, even if you believe that JUnit is the only solution when it comes to unit testing.

Trasys is already using Spock in production and so should you!

Konstantinos Kapelonis – Senior Software Engineer