In my Grails app, I have a suite of Geb tests wherein I invoke various GORM methods to save/retrieve data. This was working fine, until very recently. But now, whenever I try to invoke a GORM method from a Geb test, I get the following error:
Method on class [com.example.MyDomainClass] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.
I'm aware of the fact that under certain circumstances you need to use the remote control plugin to use GORM methods in Geb tests. However, in my case, I run the tests via grails test-app functional
and I don't have any JVM forking configured in Config.groovy
. In other words, the tests should be running in the same JVM as the app being tested, so I should be able to use GORM methods without the remote control plugin.
I'm using Grails 2.3.11 and Geb 0.10.0.
You really should always use grails remote control plugin. You should not depend on the fact that your tests and the application under test run in the same JVM and that you are able to call grails code from your test. This approach will only work under certain circumstances while using the remote control plugin for this will work always.
Even if your tests and app are running in the same JVM (as most probably is still the case for you) some methods, like GORM for example, might be restricted by grails to be only called from a context of a request, because they need that context to be able to work correctly.
来源:https://stackoverflow.com/questions/28505134/invoking-gorm-methods-from-geb-tests