making domain.save(failOnError:true) fail when doing integration tests


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!


Your first instance of metaClassing the save() is fine.

When trying to metaClass the save(failOnError: true) version, you have to alter your metaClassing statement to match the signature of the actual employed method. A "save()" invocation is not the same as a "save(failOnError:true)" invocation. Try this (I suspect the parameter is strictly typed, so I'm using Map. :

Cat.metaClass.save = { Map map -> throw new Exception("failOnError is true") }


  • Odd behavior with default index action when unit testing a RestfulController in grails
  • Generate .jar file and javadoc in ship.io
  • regex c# optional group - should act greedy?
  • Can't inherit from auto_ptr without problems
  • Google Spreadsheet App Script will not wait for results to load
  • d3js updates only once
  • Using AT commands with an Android phone
  • USING webservice in PHP which returns XML
  • Permanent (301) .htaccess redirect
  • asp.net eval script in ajax html reponse
  • Go Unmarshaling YAML into struct
  • LIKE operator for array value of column
  • numba @jit slower that pure python?
  • SQLite: Update column with value from another table
  • Words frequency in percentage java
  • cvc-complex-type.2.1: Element 'property' must have no character or element information ite
  • Upgrading Rails 3.2 to Rails 4 and Params
  • AngularJS sorting a list with nulls
  • How to remove key+value pair from an object of Array [duplicate]
  • How to run multiple commands from ant exec task
  • Regexp, capture between parentheses, javascript
  • Undefined references when compiling gSOAP client
  • how to save the state in userdefaults of accessory checkmark-iphone
  • Database structure design with variable amounts of fields
  • Deselecting radio buttons while keeping the View Model in synch
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • Adding custom controls to a full screen movie
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to set the response of a form post action to a iframe source?
  • Java static initializers and reflection
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • unknown Exception android
  • Checking variable from a different class in C#
  • reshape alternating columns in less time and using less memory
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app