Maven + Spock - Extra Test Reported for Parameterized Tests

33 Views Asked by At

I'm working on cleaning up our test suite, and one thing I'm seeing is that in parameterized Spock tests that get unrolled, maven surefire is reporting an extra "test" for the data-less header row. This is using Spock 2.3. Here is an example test file:

package com.sample.utility

import org.joda.time.LocalDate
import spock.lang.Specification

class DateUtilityTest extends Specification {

    def "#years years from today being over 21 is #result"() {
        expect:
        DateUtility.isOver21(new LocalDate().plusYears(years).toDate()) == result

        where:
        years || result
        -20   || false
        -21   || true
        -22   || true
    }

    def "#years years from today being over 18 is #result"() {
        expect:
        DateUtility.isOver18(new LocalDate().plusYears(years).toDate()) == result

        where:
        years || result
        -17   || false
        -18   || true
        -19   || true
    }
}

Expectation of course is for 6 tests to be run, 3 for each method, and that's how Intellij reports it if I run the tests there. However, when running mvn test:

[INFO] Running com.sample.DateUtilityTest
[INFO] Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.025 s -- in com.sample.DateUtilityTest

Which is also how it's being reported in Jenkins, with test names:

  • #years years from today being over 18 is #result
  • #years years from today being over 21 is #result
  • -17 years from today being over 18 is false
  • -18 years from today being over 18 is true
  • -19 years from today being over 18 is true
  • -20 years from today being over 21 is false
  • -21 years from today being over 21 is true
  • -22 years from today being over 21 is true

Plugin definition in pom is

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.1.2</version>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.1</version>
        </dependency>
    </dependencies>
</plugin>

Is there a way to configure things so that maven isn't reporting an extra test for the header rows?

1

There are 1 best solutions below

0
kriegaex On

This is a quasi duplicate of this Q/A. As you can see there, this is a JUnit 5 platform feature and by no means limited to Spock, but also affects parametrised JUnit Jupiter tests.

As for how to make your reported names more human-friendly, I suggest something like this:

@Unroll("age #years years, result is #result")
def "check if at least 21 years old"()

Then, the test container would have a clean name, and each parametrised test inside it would, too. It would also look nicer in IDEs like IntelliJ IDEA.