Run a specific test in a single test class with Spock and Maven

前端 未结 3 706
广开言路
广开言路 2021-01-05 01:40

I am using Spock framework for testing (1.0-groovy-2.4 release). Junit offers this option to run a specific test using the command line (with Maven):

mvn -Dt         


        
3条回答
  •  北海茫月
    2021-01-05 02:13

    After a further investigation, the answer is most probably: No, you can't invoke specific test methods with Spock using that Surefire Plugin feature. Below the reason.

    Given the following Spock test case:

    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"
        }
    }
    

    And given the following plugins configuration:

    
        org.codehaus.gmavenplus
        gmavenplus-plugin
        1.5
        
            
                
                    addTestSources
                    testCompile
                
            
        
        
            
                
                    ${pom.basedir}/src/test/java
                    
                        **/*.groovy
                    
                
            
        
    
    
    
        org.apache.maven.plugins
        maven-surefire-plugin
        2.18.1
        
            
                **/*Test.java
                **/*Spec.java
            
        
    
    

    It runs fine as part of the Maven test phase executing 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   
    

    Even executing mvn clean test -Dtest=HelloSpec you get exactly the same result as above, successfully execution.

    So, why we can't run a single method?

    If we execute the javap command on the HelloSpec sample 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 in the generated bytecode there is no sayHello method, because Spock changes methods name in order to allow spaces in them. So the method name you write is never actually the real method name as part of the compiled class.

    In this case, the method name is most probably $spock_feature_0_0, not something really friendly.

    This is also confirmed in this answer and the comments of Peter Niederwieser, author of Spock actually, so a more than reliable source.

    You could try running the following:

    mvn clean test -Dtest=HelloSpec#*spock*
    

    which should indeed match the real method name, but you would however get an error

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

    This is most likely because with the direct invocation name we are bypassing the glue between JUnit and Spock and as such the execution fails.

提交回复
热议问题