使用Maven运行spock单元测试

时间:2014-08-07 15:09:30

标签: java unit-testing maven groovy spock

在之前的项目中,我使用Spock测试框架对我的Java代码进行单元测试。我发现这非常有效,所以我试图将Spock测试添加到我当前的项目中,该项目使用Maven作为其构建工具(之前的项目使用了Gradle)。虽然我可以让Maven编译我的Spock测试(使用groovy-eclipse-compiler),但我无法让Maven运行测试。

我做了一个简单的例子,用2个文件来证明我的问题:

  • pom.xml
  • src/test/java/ASpec.groovy

pom.xml的内容:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.group</groupId>
    <artifactId>my-artifact</artifactId>
    <version>0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
            <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.0.8</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>0.7-groovy-2.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <compilerId>groovy-eclipse-compiler</compilerId>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-compiler</artifactId>
                        <version>2.8.0-01</version>
                    </dependency>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-batch</artifactId>
                        <version>2.1.8-01</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

ASpec.groovy的内容:

import spock.lang.Specification

class ASpec extends Specification {

    def "Test A"(){
        // Always fail
        expect: false
    }
}

当我执行mvn clean test(或mvn clean install)时,我希望我的单个单元测试能够运行并失败。在编译时,Maven不会运行它。 有没有人知道如何从Maven运行Spock单元测试(或者如果可能的话?)

(我没有把我的测试放在一个包中以保持示例简单。另外我把我的groovy代码放在src / test / java中以避免配置示例从另一个目录中获取源文件,再次保持这个例子尽可能简单。)

4 个答案:

答案 0 :(得分:22)

这个答案纯粹是对@ PeterNiederwieser的答案的补充。在其中他提到您可以配置Surefire使用的名称模式。这是一个对我有用的例子:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18</version>
    <configuration>
        <includes>
            <!-- By default only files ending in 'Test' will be included, so also include support for Spock style naming convention -->
            <!-- Oddly enough for Groovy files, *Spec.groovy does not work, but *Spec.java does -->
            <include>**/*Test.java</include>
            <include>**/*Spec.java</include>
        </includes>
    </configuration>
</plugin>

Source

正如我在评论中提到的,我不确定为什么**/*Spec.groovy没有用,但我很高兴能够在这里使用正常的Spock约定。

答案 1 :(得分:20)

Maven Surefire按名字查找测试类。将类名更改为ATest,或重新配置Surefire使用的名称模式。 spock-example项目的POM演示了如何完成后者。

答案 2 :(得分:1)

我有同样的要求将Spock添加到我现有的Java Web应用程序中。 我试过彼得斯,但它对我不起作用。 gmavenplus-plugin 以某种方式(不知道)用一个非常古老的google lib替换了我的guava依赖,我的Spring应用程序破坏了抱怨一种不存在的方法。

经过2到3次尝试后,我终于能够集成我的Spock Unit测试和集成测试,更重要的是将Spock groovy类的编译与现有的Java / Junit Spring / Hibernate应用程序隔离开来。 / p>

当然,如果我有一个问题,它就可以解决问题......但这是一个遗留项目,因此我没有选择。

以下是我添加的插件。 请注意Spock单元测试以Spec结束。 Spock集成测试以IT结束(但最有可能是SpecIT)。 我将我的Spock测试放在src / test / groovy下。

         <plugins>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <!-- Without joint compilation - no dependencies between Java and Groovy (inheritance)-->
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sources>
                        <source>
                            <directory>${project.basedir}/src/main/java/groovy</directory>
                            <includes>
                                <include>**/*.groovy</include>
                            </includes>
                        </source>
                    </sources>
                    <testSources>
                        <testSource>
                            <directory>${project.basedir}/src/test/groovy</directory>
                            <includes>
                                <include>**/*.groovy</include>
                            </includes>
                        </testSource>
                    </testSources>

                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <testSourceDirectory>src/test/groovy</testSourceDirectory>
                    <testSourceDirectory>src/test/java</testSourceDirectory>
                    <includes>
                        <include>**/*Spec.java</include>
                        <!-- Yes, .java extension -->
                        <include>**/*Test.java</include>
                        <!-- Just in case having "normal" JUnit tests -->
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <useFile>false</useFile>
                    <includes>
                        <include>**/*IT.java</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

这是我的依赖:

        <!--Spock -->
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy</artifactId>
            <version>2.4.7</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>1.1-groovy-2.4</version>
        </dependency>

        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-spring</artifactId>
            <version>1.1-groovy-2.4</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.groovy.modules.http-builder</groupId>
            <artifactId>http-builder</artifactId>
            <version>0.7.1</version>
        </dependency>
        <!--Spock mocking dependencies -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>3.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.objenesis</groupId>
            <artifactId>objenesis</artifactId>
            <version>2.6</version>
        </dependency>

只是为了让你知道,我原来的POM绝对没有明确的插件。所以我的项目有一个非常简单的POM。所以, 应该为你工作。 这是一个Java 1.7项目。

...最后,为了让你有信心这不是垃圾邮件,我做了多次测试以确保上述工作:

  1. 只需在没有测试的情况下构建WAR,然后在本地进行部署和冒烟测试 mvn clean install -DskipTests -Dmaven.test.skip = true

  2. 进行测试编译,看看Groovy Unit测试是否也被编译了 mvn -X clean test-compile

  3. 在没有集成测试的情况下进行全新安装(我确保此测试失败)并查看是否运行了Groovy单元测试 mvn clean install -DskipITs

  4. 只需运行集成测试
    mvn failsafe:integration-test

  5. 我本来希望包含上面的截图作为证据,但它必须经过审查......所以,我真诚地希望这可以帮助你,因为我正在努力让这个工作...... Maven是一个巨大的学科领域。祝你好运:=)

答案 3 :(得分:0)

我遇到的一个问题是不兼容的依赖项。

spock-core 依赖项当然会依赖它需要的 groovy 版本。但其他依赖项或插件(我认为在我的例子中是 gmaven-plus 插件)可能会引入不同的、不兼容的 groovy 版本。

我通过明确提供对我想要使用的 groovy 包的依赖来解决这个问题。