I'm writing a unit test for some simple methods. The issue I am having is that 'save' is not working for my domain object even though the domain should be mocked. When calling validate on the domain object, it comes back as true. I've even surrounded the object with try/catch to make sure it wasn't throwing any odd errors and that is not the case. Code below for call and test.开发者_StackOverflow
void test()
{
mockDomain(MyDomain)
Map map1= ["asdf":" "]
Map map2 = ["asdf":123]
InputObject input = new InputObject()
input.setForeignId("1") //not a constraint
input.setMap1(map1)
input.setMap2(map2 )
service.methodUnderTest(profile)
List list = MyDomain.getAll()
assertEquals 1, l.size() //FAILS
}
def persistPublishGuids(InputObject input)
{
try{
HashMap map1 = input.map1
for ( e in map1 )
{
String key= e.getKey()
String value = e.value
long size = input.map2.get(key)
MyDomain domain = new MyDomain (id:guid, field1:value, field2:input.foreignId, field3:size)
domain.save()
}
} catch(ex)
{
ex.printStackTrace()
}
}
I would also recommend setting the flush and failOnError flags to true when saving domain objects in tests. 'flush:true' ensures the object is saved immediately. Without failOnError validation errors will mean the object is NOT saved and this can easily be missed.
domainObject.save(flush:true, failOnError: true)
It is also worth considering setting the validate flag to false if you don't want to setup base data for all fields in the test
domainObject.save(validate: false)
See the Grails domain object save docs for full information
The problem was that the grails app needed to be cleaned and the project rebuilt.
精彩评论