使用Maven运行Spock单元测试 [英] Running spock unit tests with Maven

查看:178
本文介绍了使用Maven运行Spock单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我做了一个简单的例子来证明我的问题有两个文件:


  • pom.xml

  • src / test / java / ASpec.groovy






pom.xml

 < project xmlns =http://maven.apache.org /POM/4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi: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>

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

< build>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-compiler-plugin< / artifactId>
< version> 3.1< / version>
<配置>
< compilerId> groovy-eclipse-compiler< / compilerId>
< / configuration>
<依赖关系>
< dependency>
< groupId> org.codehaus.groovy< / groupId>
< artifactId> groovy-eclipse-compiler< / artifactId>
< version> 2.8.0-01< / version>
< /依赖关系>
< dependency>
< groupId> org.codehaus.groovy< / groupId>
< artifactId> groovy-eclipse-batch< / artifactId>
< version> 2.1.8-01< / version>
< /依赖关系>
< /依赖关系>
< / plugin>
< / plugins>
< / build>
< / project>

ASpec.groovy 的内容:

  import spock.lang.Specification 

class ASpec extends Specification {

def Test A(){
//总是失败
expect:false
}
}






当我执行 mvn clean test (或 mvn干净安装)我期望我的单个单元测试运行并失败。在编译时,Maven不会运行它。 是否有人知道如何从Maven运行Spock单元测试(或者可能吗?)

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

解决方案

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


On a previous project I used the Spock testing framework to unit test my Java code. I found this really productive so I am trying to add Spock tests to my current project which uses Maven as its build tool (The previous project used Gradle). While I can get Maven to compile my Spock tests (using groovy-eclipse-compiler), I am unable to get Maven to run the tests.

I've made a simple example to demonstrate my problem with 2 files:

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

Contents of 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>

Contents of ASpec.groovy:

import spock.lang.Specification

class ASpec extends Specification {

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


When I execute mvn clean test (or mvn clean install) I would expect my single unit test to be run and fail. While it is compiled, Maven does not run it. Does any one know how to run a Spock unit test from Maven (or if it is possible?)

(I have not put my test in a package to keep the example simple. Also I have put my groovy code in src/test/java to avoid configuring the example to pick up source files from an additional directory, again to keep the example as simple as possible.)

解决方案

Maven Surefire finds test classes by their name. Either change the class name to ATest, or reconfigure the name pattern used by Surefire. The POM for the spock-example project demonstrates how to do the latter.

这篇关于使用Maven运行Spock单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆