The Spock Framework is one of the best unit testing frameworks compatible with JUnit. It’s based on the Groovy language and it takes its capabilities to an upper level (thanks to huge AST transformations performed during compilation). You can write beautiful yet powerful tests, including but not limited to: data series, mocking, behavior testing, and detailed reporting about failed assertions.

I believe that the Atlassian Clover 3.3 is the best code coverage tool for Spock-based tests. Let me tell you why.

Detecting Spock’s test classes (aka specifications)

Clover 3.3 comes with a new test detector, which handles Spock’s specifications. It means that you will no longer see specifications on the “Classes” (instead of the “Tests”) tab and you will no longer have to configure custom test pattern to treat them as test code.

Clover 3.3 detects Spock's Specifications

Handling user-friendly test names

In the Spock Framework, tests are defined using a descriptive caption, such as:

def "this is my test" { ... }

Under the hood, Spock transforms such code into methods named like “$spock_feature_n_m”. You no longer have to see such cryptic names in the report – Clover 3.3 handles these statically defined names, so you can see them on a method list, for instance:

Spock's test names are used instead of cryptic, generated methods

Handling test names evaluated at runtime

The Spock Framework lets you define a data series and run the same test multiple times using different inputs. But how do you detect which iteration has failed among hundreds of test iterations which were run? Spock comes with a solution: The @Unroll annotation appends a sequence number to a test name. Furthermore, you can define variables or even use Groovy selectors in it. For instance:

def "minimum of #a and #b is #c"() { ... }
def "#person.name is a #sex.toLowerCase() person"() { ... }

Clover 3.3 tightly integrates with Spock’s test runner so in the HTML report you can see test names exactly as they were evaluated by Spock.

Example:

Test name containing Groovy selectors

Importing test results from external XML files

The clover-report task allows to define an external source of test results (instead of relying on Clover’s internal test tracking).

Clover 3.3 handles test reports generated by Spock (in the JUnit-compatible XML format) – not only can test results be listed, but they can also be linked to corresponding methods in the code, including the ones with the @Unroll annotation. So, taking an example from a screen shot below – from the “Wilma is a female person” test you can directly navigate to the “#person.name is #sex.toLowerCase() person” method.

Clover 3.3 allows to navigate from a test to code

Want more?

You’re in luck! Clover 3.3 comes with a similar support for JUnit4 parameterized tests as well.

See the Integrating Clover with JUnit4 Parameterized Tests article for more details.

Excited to try this out?

1. Download the latest Clover version:

Download Clover 3.3

2. Clone the ‘clover-examples‘ Mercurial repository and run the spock-example and the  parameterized-junit4-example projects.