Take the 2-minute tour ×

I need to mock a GrailsControllerClass interface. Instance should have a static variable defined. The problem is that MockFor and StubFor don’t give you an option for adding static members. So, I write my abstract class that extends GrailsControllerClass

abstract class MyController implements GrailsControllerClass {
   static myDefinition  = [name:"item1", action:"action1"]

Now, MockFor and StubFor raise an error: groovy.lang.MissingMethodException: No signature of method: groovy.util.ProxyGenerator.instantiateAggregateFromBaseClass() is applicable for argument types: (java.lang.Class, null) values… which seems to be a reported bug: http://jira.codehaus.org/browse/GROOVY-3720 Alas, grails comes with groovy 1.6.

I do not want to make MyController a huge concrete class with empty method declarations. Any alternatives?

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

Have you tried GMock, yet? It supports mocking of static methods.

share|improve this answer
I would like my tests to extend GrailsTestCase, so that I can use grails test runner. While it seems you can use @WithGMock annotation, it seems that your assertions have to be enclosed inside play{}. It seems to me that gmock is more than mocking fwk.. –  Dan Nov 10 '09 at 20:29
Yes, GMock is more sophisticated than standard Groovy MockFor or StubFor, but it's still a mocking framework and nothing more IMO. The execution part of your test has to be inside the play closure, which separates the setting of expectations and adding of behaviour from the execution. This is somewhat similar to what EasyMock does, but it feels more natural. Yes, you're right, if you want to extend GrailsUnitTestCase instead of GMockTestCase, then you have to use the @WithGMock annotation. –  Christoph Metzendorf Nov 11 '09 at 10:12
add comment


class Tests extends GrailsUnitTestCase {

SUT systemUnderTest = new SUT()


ClaasToBeMocked mockedClass = new ClassToBeMocked()

mockedClass.staticMethod = [staticMethod: {return "TestValue"}] as ClassToBeMocked

assertEquals("something", systemUnderTest.methodThatCallsStaticMethod())
share|improve this answer
add comment

You could tweak the metaClass I believe:

GrailsControllerClass.metaClass.'static'.myDefinition = [name:"item1", action:"action1"]

Or use a map to mock your controller interface:

def myController = [getMyDefinition:{[name:"item1", action:"action1"]}] as GrailsControllerClass

I'm not sure based on your post if that's what you're looking for or not.

share|improve this answer
doesn't seem to work for static fields: GrailsControllerClass.metaClass.'static'.myDefinition = [name:"item1", action:"action1"] –  Dan Nov 11 '09 at 14:59
Yes, I'm seeing some difficulty with static fields in my own post here: stackoverflow.com/questions/1707830/…. I wonder if it's possibly related to this bug: jira.codehaus.org/browse/GROOVY-3873? –  Matt Lachman Nov 12 '09 at 18:30
add comment

Your Answer


