Testing my controller with Spock

I need to test a controller action index

(generated by grails command generate-all

). I have a test like this (in Spock):

package mnm.schedule

import grails.test.mixin.*
import org.junit.*
import grails.plugin.spock.*
import spock.lang.Specification
import org.example.*;

class UserControllerSpec extends ControllerSpec {

def "test"() {
            mockLogging(UserController, true)


            redirectArgs.action == "list"



I am getting an error like this:

Error Error running script test-app :spock : cannot find shared instance field (Use --stacktrace to see the full trace)


After some time I can run the test, the test gets PASSED.

What's really going wrong? Why is it showing this error the first time? I am new to the Spock environment.

Thanks in advance.


source to share

1 answer

Given that you are using Grails 2.xx, you should use the @TestFor annotation, which extends the unit test classes with mixins.

In your case, you must add @TestFor(UserController)

as class-level annotations so that you can use the method mockLogging




All Articles