Like dmahaptro mentioned, you do not need to mock your person. The preferred/proper way of grabbing an object in a test via .get()
is the way you have it--you create the domain instance, and pass its id to the controller action for consumption.
However, if you need to mock a static method like this there are two approaches:
1) Use the mockFor
method provided by GrailsUnitTestMixin
and then specify the static method like so:
GrailsMock mockPerson = mockFor(Person)
mockPerson.demand.static.get() { Long id -> new Person(name:"John") }
Note that in your case, the mock's static get
method did not contain the correct parameter type (int
was used instead of Long
) so this method was not being invoked.
2) Modify the class' metaClass
. Changing the metaClass is highly discouraged (especially when the @Mock
annotation already sets everything up for you), and can lead to test leakage (i.e., other tests will not work properly after you've changed the metaClass, unless you restore the original metaclass after the test is done).
That said, for the sake of familiarity, the way you would mock the .get()
method is via:
Person.metaClass.static.get = { Long id -> return personWithId }
Again, this is considered bad practice and should be avoided, but there you have it.