Java 使用 Maven 运行 spock 单元测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25186004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running spock unit tests with Maven
提问by Neil Stevens
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.
在之前的一个项目中,我使用 Spock 测试框架对我的 Java 代码进行单元测试。我发现这真的很有成效,所以我尝试将 Spock 测试添加到我当前使用 Maven 作为其构建工具的项目(之前的项目使用 Gradle)。虽然我可以让 Maven 编译我的 Spock 测试(使用groovy-eclipse-compiler
),但我无法让 Maven 运行测试。
I've made a simple example to demonstrate my problem with 2 files:
我做了一个简单的例子来演示我的 2 个文件的问题:
pom.xml
src/test/java/ASpec.groovy
pom.xml
src/test/java/ASpec.groovy
Contents of pom.xml
:
内容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
:
内容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?)
当我执行mvn clean test
(或mvn clean install
)时,我希望我的单个单元测试运行并失败。当它被编译时,Maven 不会运行它。有谁知道如何从 Maven 运行 Spock 单元测试(或者如果可能的话?)
(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.)
(我没有将我的测试放在一个包中以保持示例简单。此外,我将我的 groovy 代码放在 src/test/java 中以避免将示例配置为从附加目录中获取源文件,再次将示例保持为尽可能简单。)
采纳答案by Peter Niederwieser
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-exampleproject demonstrates how to do the latter.
Maven Surefire 按名称查找测试类。要么将类名更改为ATest
,要么重新配置 Surefire 使用的名称模式。spock-example项目的 POM演示了如何执行后者。
回答by mnd
This answer is purely supplemental to @PeterNiederwieser's answer. In it he mentions that you can configure the name pattern used by Surefire. Here is an example of what worked for me:
这个答案纯粹是对@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>
As I mention in the comments, I'm not sure why **/*Spec.groovy
didn't work, but I'm happy to be able to use the normal Spock convention here.
正如我在评论中提到的,我不确定为什么**/*Spec.groovy
不起作用,但我很高兴能够在这里使用正常的 Spock 约定。
回答by Beezer
I had the same requirement to add Spock to my existing java web app. I tried Peters but it did not work for me. gmavenplus-pluginsomehow (no idea) replaced my guava dependency with a very old google lib and my Spring application broke complaining about a non-existent method.
我有同样的要求将 Spock 添加到我现有的 Java Web 应用程序中。我试过彼得斯,但它对我不起作用。 gmavenplus-plugin不知何故(不知道)用一个非常旧的谷歌库替换了我的番石榴依赖,我的 Spring 应用程序崩溃了,抱怨一个不存在的方法。
After literally maybe 2 or 3 dozen attempts, I finally was able to integrate my Spock Unit tests, and Integration tests and more importantly to isolate the compilation of the Spock groovy classes from my existing Java/Junit Spring/Hibernate application.
经过大约 2 到 3 次尝试,我终于能够集成我的 Spock 单元测试和集成测试,更重要的是将 Spock groovy 类的编译与我现有的 Java/Junit Spring/Hibernate 应用程序隔离开来。
Of course if I had gradle it would have solved the issue...but this is a legacy project and therefore I had not the choice.
当然,如果我有 gradle 它会解决这个问题......但这是一个遗留项目,因此我没有选择。
Below are the plugins I added. Please note the Spock unit tests end with Spec. The Spock integration tests end with IT (but most probably should be SpecIT). I put my Spock tests under src/test/groovy.
下面是我添加的插件。请注意 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>
And here are my dependencies:
这是我的依赖项:
<!--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>
And just to let you know, my original POM had absolutely no explicit plugins AT ALL. So I had a very simply POM for my project. So, it shouldwork for you. It is a Java 1.7 project.
只是为了让您知道,我的原始 POM 完全没有明确的插件。所以我的项目有一个非常简单的 POM。所以,它应该适合你。它是一个 Java 1.7 项目。
...and finally, just to give you some confidence that this is not a rubbish post, I did multiple tests in order to ensure the above worked:
...最后,为了让您确信这不是垃圾帖子,我进行了多次测试以确保上述内容有效:
Just build the WAR without the tests and deploy and smoke test it locally
mvn clean install -DskipTests -Dmaven.test.skip=trueDo a test compile and see if the Groovy Unit tests get compiled as well
mvn -X clean test-compileDo a clean install without the Integration test (i made sure it was failing for this test) and see if the Groovy unit tests are run
mvn clean install -DskipITsJust run the integration test
mvn failsafe:integration-test
只需在没有测试的情况下构建 WAR,然后在本地部署和冒烟测试
mvn clean install -DskipTests -Dmaven.test.skip=true做一个测试编译,看看 Groovy 单元测试是否也被编译了
mvn -X clean test-compile在没有集成测试的情况下进行全新安装(我确保它在此测试中失败)并查看 Groovy 单元测试是否运行
mvn clean install -DskipITs只需运行集成测试
mvn failsafe:integration-test
I would have liked to include screenshots of the above as proof but it would have had to be censored...So, I sincerely hope this helps you, as I was going mental trying to get this working...Maven is a huge subject area. Good luck :=)
我本来希望包括上面的截图作为证据,但它必须被......所以,我真诚地希望这对你有帮助,因为我正在努力让这个工作......Maven是一个巨大的主题区域。祝你好运:=)