首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spock错误:Groovyc:无法实例化全局转换类

Spock错误:Groovyc:无法实例化全局转换类
EN

Stack Overflow用户
提问于 2016-10-30 13:54:37
回答 3查看 11.1K关注 0票数 6

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

代码语言:javascript
复制
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

以下是我的规格:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
<!-- 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>

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-11-01 03:18:29

下面是您的测试,用更地道的Spock/Groovy重写:

代码语言:javascript
复制
@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]    
    }
}

以下是几个注意事项:

Spock Groovy测试就是在其中编写的,它本身就支持声明集合。因此,测试中使用的类级变量需要[user1, user3],而不是Lists.newArrayList.

  • @Sharedstatic。这将解决你的编译问题。Groovy强烈推荐你阅读这本书,这是一本很棒的书,它将极大地帮助你开始使用

。样式指南在这里:http://docs.spockframework.org.,您可以从那里浏览http://www.groovy-lang.org/style-guide.html页面,以获得其他有趣的花边新闻。

  • Spock文档也非常出色,可以很容易地一次阅读到:Spock
票数 4
EN

Stack Overflow用户

发布于 2016-10-31 21:50:02

为非方法变量添加static或@Shared - 'filter','user 1-3‘。您不能从“where”节中访问非静态类属性。

票数 1
EN

Stack Overflow用户

发布于 2021-12-15 14:43:09

在我的例子中,我通过从spring boot 2.3.4升级到2.5.3解决了groovy错误

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40326406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档