在上一个项目中,我使用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.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中,以避免将示例配置为从另一个目录中提取源文件,同样也是为了使示例尽可能简单。)
发布于 2014-08-07 23:23:07
Maven Surefire通过它们的名字找到测试类。或者将类名更改为ATest
,或者重新配置Surefire使用的名称模式。spock-example项目的POM演示了如何执行后一种操作。
发布于 2014-12-18 05:42:11
此答案纯粹是对@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>
正如我在评论中提到的,我不确定为什么**/*Spec.groovy
不能工作,但我很高兴能够在这里使用普通的Spock约定。
发布于 2017-08-31 23:06:34
我也有同样的需求,将Spock添加到我现有的java web应用程序中。我试过彼得斯,但对我不起作用。gmavenplus-plugin不知何故(不知道)用一个非常旧的google lib替换了我的芭乐依赖,我的Spring应用程序崩溃了,抱怨一个不存在的方法。
经过大约2到3次尝试,我终于能够集成我的Spock单元测试,以及集成测试,更重要的是将Spock groovy类的编译与我现有的Java/Junit Spring/Hibernate应用程序隔离开来。
当然,如果我有gradle,它就可以解决issue...but,这是一个遗留项目,因此我别无选择。
下面是我添加的插件。请注意,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。所以,它对你来说应该是有效的。这是一个Java1.7项目。
...and最后,只是为了给你一些信心,这不是一个垃圾帖子,我做了多次测试,以确保上面的工作:
Groovy全新安装-Dmaven.test.skip=true
Groovy test-compile
mvn全新安装-DskipITs
mvn failsafe:integration-test
我想包括上面的截图作为证据,但它必须是censored...So,我真诚地希望这对你有帮助,因为我正在发疯地试图得到这个working...Maven是一个巨大的主题领域。祝你好运( :=)
https://stackoverflow.com/questions/25186004
复制相似问题