After further investigation, the answer is likely: No, you cannot refer to special testing methods with Spock using this function of the Surefire plugin. Below are the reasons.
Given the following test case for Spock:
import spock.lang.Specification class HelloSpec extends Specification { def hello = new Main(); def sayHello() { given: "A person 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" } }
And given the following plugin configuration:
<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>
It works great as part of the Maven test phase, performing mvn clean test :
------------------------------------------------------- TESTS ------------------------------------------------------- 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
Even mvn clean test -Dtest=HelloSpec you mvn clean test -Dtest=HelloSpec , you get exactly the same result as above, successfully executing.
So why can't we run one method?
If we execute the javap command in the javap test test above, we get the following output:
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); }
So, there is no sayHello method in the generated bytecode because Spock changes the name of the method to allow spaces in them. Thus, the name of the method you are writing is never the true name of the method as part of a compiled class.
In this case, the method name is most likely $spock_feature_0_0 , and not something really friendly.
This is also confirmed in this answer and comments by Peter Niederwieser , the author of Spock, in fact, therefore a more reliable source.
You can try running the following:
mvn clean test -Dtest=HelloSpec
which should really match the name of the real method, but you will however receive an error message
------------------------------------------------------- TESTS ------------------------------------------------------- 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
This is most likely because with the direct name of the call, we bypass the glue between JUnit and Spock, and therefore the execution fails.