I have a Domain class called User. When running test I wanted to redefine the get method of the User class by doing the following
User.metaClass.static.get = {Long id -> [username:"joe", id:id]}
But applying the above does not seem to have an impact when I call
User.get(2)
Can I use metaClass in the static Domain GORM methods like get() or list() to change their behavior ? Thanks
Here it is my testCase:
@Test
void testMe(){
User.metaClass.static.get = { id -> [username:"joe", id:id]} def user = User.get(3) assert user.username == "joe"}
and I get an NPE Cannot get property 'username' on null object
I can actually do it using groovy MockFor
def mockControl = new MockFor(User.class) mockControl.demand.get {id -> return [username:"joe"]} mockControl.use { def user = User.get(3) assert user.username == "joe" }
@Mockis used for domain classes. Are you specifically trying to testUser.getworks as expected? - dmahapatro