user468587 user468587 - 1 month ago 14
Groovy Question

Spock Error:Groovyc: Could not instantiate global transform class

I m new to Spock, tried to write a simple Spock but it failed:

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


here is my Spec:

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

}
}


and here is my 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>


what is wrong my Spec or pom setting? do i have to install Groovy to make it work?

Answer

Here is your test, re-written in more idiomatic 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]    
    }
}

A couple of notes:

  • Groovy (in which Spock tests are written) has native support for declaring collections. Thus [user1, user3] instead of Lists.newArrayList.
  • @Shared or static is required for class-level variables that are used in tests. This will solve your compilation problem.
  • I highly recommend reading the Groovy user guide, it's a great read and will help you immensely getting started with Spock. The style guide is here: http://www.groovy-lang.org/style-guide.html, and from there you can explore the Documentation page for other interesting tidbits.
  • The Spock documentation is also excellent, and can easily be read in one sitting: http://docs.spockframework.org.
Comments