I need to test a controller, of action index(generated by the grails generate-all command). 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"() {
setup:
mockLogging(UserController, true)
when:
controller.index()
then:
redirectArgs.action == "list"
}
}
I get a error like this :
Error Error running script test-app :spock : cannot find shared instance field (Use --stacktrace to see the full trace)
After sometime I can run the test, the Test is getting PASSED.
Whats actually going wrong? Why the first time it shows that error? I’m new to Spock environment.
Thanks in advance.
Given that you are using Grails 2.x.x, you should use the @TestFor annotation which enhances unit testing framework classes with mixins.
In your case, you should add
@TestFor(UserController)as a class-level annotation, so you can use themockLoggingmethod.