I would like to use GORM to setup test data in functional tests written with spock. Since I do not want to mock behaviour, I got an exception
java.lang.IllegalStateException: Method on class [com.test.Person]
was used outside of a Grails application. If running in the context of a
test using the mocking API or bootstrap Grails correctly.
Is there a way to use GORM this way? I've seen the remoteControl plugin, but I don't want to use it and AFAIK I should be in the same JVM since I start the tests without the --war
switch.
Since 2.3 forked execution is enabled by default (which includes run-app, test-app, run-war, console). So you have to take a look at your BuildConfig.groovy
(see the docs for more information), if forked execution is enabled. You have to disable it, but you will lose some other positive aspects of it as well. The only really option you have, is to use the remote-control plugin with some construct like this (example from the remote-control plugin docs:
def remote = new RemoteControl()
def testIt() {
def id = remote {
def person = new Person(name: "Me")
person.save()
person.id
}
// Somehow make some HTTP request and test that person is in the DB
remote {
Person.get(id).delete()
}
}