I am purposely causing a cat instance to fail. The following test passes.
void testSomething() {
Cat.metaClass.save = {
throw new Exception("Asdasd")
}
shouldFail(Exception){
Cat cat = new Cat(name: "asd")
cat.save()
}
GroovySystem.metaClassRegistry.removeMetaClass(Cat.class)
}
But, when i set the failOnError property for the save method then this fails. How can i alter the save using metaClass in order to make the save(failOnError:true) throw an exception? I appreciate any help! Thanks!
void testSomething() {
Cat.metaClass.save = {
throw new Exception("Asdasd")
}
shouldFail(Exception){
Cat cat = new Cat(name: "asd")
cat.save(failOnError: true)
}
GroovySystem.metaClassRegistry.removeMetaClass(Cat.class)
}
One alternative to doing the same test is to pass in invalid parameters to the domain instance so that the validation fails and exception is thrown but this will not work in all cases because in some cases the domain instance doesn't require any parameters given by the user. So, in order to simulate the failure of domain save() in this case we will need a way to mock the save failure. So, i appreciate if anyone has answer to how to mock save with or without save params like save(flush:true), save(failOnError:true). Thanks!