Bootstrap is not performed for unit tests. I personally would prefer to create a mockFoo method that does the above meta-programming, and then I will call mockFoo from the test setup. Also look at GrailsUnitTestCase.registerMetaClass. Register the metaclass before adding mock methods so that they donโt leak in other tests.
registerMetaClass(SecurityUtils) SecurityUtils.metaClass.'static'.getSubject = { -> return [logout: { return true } ] as Subject }
I know that you want your dynamic methods to be available for all unit tests, but there is nothing like a bootstrap for unit tests. Therefore, you must do this in every test.
You can create a MockHelper with a static method and call it from test setUp.
source share