在 GitHub 上叉我

使用基于 JUnit5 引擎的 Spock

使用 Spock/Groovy 和 JUnit5 的示例项目

示例代码向您展示了 Failsafe 插件如何与 Spock/Groovy 和 JUnit5 一起使用。该项目包含两个依赖项spock-corejunit-jupiter-engineGroovy 编译器gmavenplus-plugin

您可以在测试源目录中为 Groovy(即src/test/groovy)创建测试。这是一个执行两次的参数化测试示例。

import pkg.Calculator
import spock.lang.Specification

class CalculatorTest extends Specification
{
    def "Multiply: #a * #b = #expectedResult"()
    {
        given: "Calculator"
        def calc = new Calculator()

        when: "multiply"
        def result = calc.multiply( a, b )

        then: "result is as expected"
        result == expectedResult
        println "result = ${result}"

        where:
        a  | b | expectedResult
        1  | 2 | 3
        -5 | 2 | -3
    }
}

如果您想另外将 Spock 测试与 JUnit4 混合,您应该在测试依赖项中添加 JUnit Vintage Engine。

<dependencies>
    [...]
    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-core</artifactId>
        <version>2.0-M2-groovy-3.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.6.2</version>
        <scope>test</scope>
    </dependency>
    [...]
</dependencies>
...
<build>
    <plugins>
        [...]
        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.9.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compileTests</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.0.0-M5</version>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
                <execution>
                    <id>verify</id>
                    <goals>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        [...]
    </plugins>
</build>
...