【问题标题】:Spock Error:Groovyc: Could not instantiate global transform classSpock 错误:Groovyc:无法实例化全局变换类
【发布时间】:2017-03-12 14:31:18
【问题描述】:

我是Spock的新手,想写一个简单的Spock但失败了:

Error:Groovyc: Could not instantiate global transform class org.spockframework.compiler.SpockTransform specified at jar:file:.../.m2/repository/org/spockframework/spock-core/1.0-groovy-2.4/spock-core-1.0-groovy-2.4.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation  because of exception java.lang.NullPointerException

这是我的规格:

@Unroll
class UserFilterSpec extends Specification {
    private static final String USER_ID_1 = "someUserId1";
    private static final String USER_ID_2 = "someUserId2";
    private static final String USER_ID_3 = "someUserId3";

    def filter = new UserFilter()

    def User user1 = setupTestUser(USER_ID_1)
    def User user2 = setupTestUser(USER_ID_2)
    def User user3 = setupTestUser(USER_ID_3)

    def "given a list of users and list of user ids, should return list of user with those users removed"() {
        expect:
        filter.filterUserDataByIds(userList, userIdList) == filterList

        where:
        userList                                | userIdList                    || filterList
        Lists.newArrayList(user1, user2, user3) | Lists.newArrayList(USER_ID_1) || Lists.newArrayList(user2, user3)

    }
}

这是我的 pom.xml:

<!-- Mandatory plugins for using Spock -->
            <plugin>
              <!-- The gmavenplus plugin is used to compile Groovy code. To learn more about this plugin,
              visit https://github.com/groovy/GMavenPlus/wiki -->
              <groupId>org.codehaus.gmavenplus</groupId>
              <artifactId>gmavenplus-plugin</artifactId>
              <version>1.4</version>
              <executions>
                <execution>
                  <goals>
                    <goal>compile</goal>
                    <goal>testCompile</goal>
                  </goals>
                </execution>
              </executions>
            </plugin>
            <!-- Optional plugins for using Spock -->
            <!-- Only required if names of spec classes don't match default Surefire patterns (`*Test` etc.) -->
            <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.6</version>
              <configuration>
                <useFile>false</useFile>
                <includes>
                  <include>**/*Spec.java</include>
                  <include>**/*Test.java</include> <!-- Just in case of having also "normal" JUnit tests -->
                </includes>
              </configuration>
            </plugin>

...
    <!-- Mandatory dependencies for using Spock -->
    <dependency>
      <groupId>org.spockframework</groupId>
      <artifactId>spock-core</artifactId>
      <version>1.0-groovy-2.4</version>
      <scope>test</scope>
    </dependency>
    <!-- Optional dependencies for using Spock -->
    <dependency> <!-- use a specific Groovy version rather than the one specified by spock-core -->
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.4.1</version>
    </dependency>
    <dependency> <!-- enables mocking of classes (in addition to interfaces) -->
      <groupId>cglib</groupId>
      <artifactId>cglib-nodep</artifactId>
      <version>3.1</version>
      <scope>test</scope>
    </dependency>
    <dependency> <!-- enables mocking of classes without default constructor (together with CGLIB) -->
      <groupId>org.objenesis</groupId>
      <artifactId>objenesis</artifactId>
      <version>2.1</version>
      <scope>test</scope>
    </dependency>

我的 Spec 或 pom 设置有什么问题?我必须安装 Groovy 才能使其工作吗?

【问题讨论】:

  • 应该可以。我注意到您没有指定 Spock 版本。你是怎么配置的?
  • @chrylis:这就是我在 pom 中关于 Spock 的所有设置,所以 spock-core: 1.0-groovy-2.4,我如何指定 Spock 版本?

标签: java maven groovy spock


【解决方案1】:

就我而言,我通过从 spring boot 2.3.4 升级到 2.5.3 解决了 groovy 错误

https://issueexplorer.com/issue/spockframework/spock-example/44

【讨论】:

    【解决方案2】:

    这是你的测试,用更惯用的 Spock/Groovy 重写:

    @Unroll
    class UserFilterSpec extends Specification {
        static final String USER_ID_1 = "someUserId1"
        static final String USER_ID_2 = "someUserId2"
        static final String USER_ID_3 = "someUserId3"
    
        @Shared user1 = setupTestUser(USER_ID_1)
        @Shared user2 = setupTestUser(USER_ID_2)
        @Shared user3 = setupTestUser(USER_ID_3)
    
        @Shared filter = new UserFilter()
    
        def "given a list of users and list of user ids, should return list of user with those users removed"() {
            expect:
            filter.filterUserDataByIds(userList, userIdList) == filterList
    
            where:
            userList              | userIdList  || filterList
            [user1, user2, user3] | [USER_ID_1] || [user2, user3]    
        }
    }
    

    几点说明:

    • Groovy(其中编写了 Spock 测试)对声明集合具有原生支持。因此[user1, user3] 而不是Lists.newArrayList
    • 测试中使用的类级变量需要@Sharedstatic。这将解决您的编译问题。
    • 我强烈建议您阅读 Groovy 用户指南,这是一本很棒的读物,将极大地帮助您开始使用 Spock。风格指南在这里:http://www.groovy-lang.org/style-guide.html,您可以从那里探索文档页面,了解其他有趣的花絮。
    • Spock 文档也非常出色,可以一口气轻松阅读:http://docs.spockframework.org

    【讨论】:

    • 我发现 NPE 是由我的 maven 存储库中的多个 groovy 版本引起的,在我清理它们并重新下载后,NPE 消失了。感谢您告诉我编写 Spock 测试的正确方法
    【解决方案3】:

    为非方法变量添加静态或@Shared - 'filter'、'user 1-3'。 您无法从“where”部分访问非静态类属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-04
      • 2014-03-16
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      相关资源
      最近更新 更多