经过进一步调查,答案很可能是:不,您不能使用 Spock 使用 Surefire 插件功能调用特定的测试方法。原因如下。
给定以下 Spock 测试用例:
import spock.lang.Specification
class HelloSpec extends Specification {
def hello = new Main();
def sayHello() {
given: "A person's name is given as a method parameter."
def greeting = hello.sayHello("Petri");
expect: "Should say hello to the person whose name is given as a method parameter"
greeting == "Hello Petri";
println "hello from HelloSpec"
}
}
并给出以下插件配置:
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>addTestSources</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<sources>
<fileset>
<directory>${pom.basedir}/src/test/java</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</fileset>
</sources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>**/*Test.java</include>
<include>**/*Spec.java</include>
</includes>
</configuration>
</plugin>
它作为 Maven test 阶段执行 mvn clean test 的一部分运行良好:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.sample.HelloSpec
hello from HelloSpec
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.314 sec - in com.sample.HelloSpec
即使执行mvn clean test -Dtest=HelloSpec,也会得到与上面完全相同的结果,成功执行。
那么,为什么我们不能运行一个方法呢?
如果我们在上面的 HelloSpec 示例测试中执行 javap 命令,我们会得到以下输出:
Compiled from "HelloSpec.groovy"
public class com.sample.HelloSpec extends spock.lang.Specification implements groovy.lang.GroovyObject {
public static transient boolean __$stMC;
public com.sample.HelloSpec();
public void $spock_feature_0_0();
protected groovy.lang.MetaClass $getStaticMetaClass();
public groovy.lang.MetaClass getMetaClass();
public void setMetaClass(groovy.lang.MetaClass);
public java.lang.Object invokeMethod(java.lang.String, java.lang.Object);
public java.lang.Object getProperty(java.lang.String);
public void setProperty(java.lang.String, java.lang.Object);
public java.lang.Object getHello();
public void setHello(java.lang.Object);
}
所以在生成的字节码中没有sayHello 方法,因为Spock 更改方法名称是为了在其中允许空格。所以你写的方法名永远不是作为编译类的一部分的真正的方法名。
在这种情况下,方法名称很可能是$spock_feature_0_0,不是很友好的东西。
this answer 和 Peter Niederwieser 的 cmets 也证实了这一点,实际上是 Spock 的作者,所以这是一个非常可靠的来源。
您可以尝试运行以下命令:
mvn clean test -Dtest=HelloSpec#*spock*
确实应该匹配真实的方法名,但是你会得到一个错误
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.sample.HelloSpec
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.007 sec <<< FAILURE! - in com.sample.HelloSpec
initializationError(org.junit.runner.manipulation.Filter) Time elapsed: 0.006 sec <<< ERROR!
java.lang.Exception: No tests found matching Method $spock_feature_0_0(com.sample.HelloSpec) from org.junit.internal.requests.ClassRequest@282ba1e
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:35)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:275)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:149)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
Results :
Tests in error:
Filter.initializationError » No tests found matching Method $spock_feature_0_...
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
这很可能是因为使用直接调用名称我们绕过了 JUnit 和 Spock 之间的粘合,因此执行失败。