We have a simple method that gets all of a specific domain object where a property equals a hard coded string. This method is within MyDomainService.
def List<MyDomain> getAllDomain()
{
List resultList
def criteria = MyDomain.createCriteria()
resultList = criteria.list()
{
eq('property1', 'READY')
}
return resultList
}
We also are writing a simple integration test for this method. The test is as follows.
void testGetAllDomain()
{
List original = MyDomain.list()
original.each{
it.property1 = 'NOTREADY'
it.sav开发者_如何学运维e(flush:true)
}
def result = MyDomainService.getAllDomain()
assertEquals 0, result.size() //All objects should be set to NOTREADY, and not retrieved. THIS is failing.
}
I have tried setting
def transactional = false
and leaving my code as is. I tried setting transactional to false and wrapping the code in .withTransaction{}. I also tried the standard config and that didn't work. What I have noticed is that if I do
def List<MyDomain> getAllDomain()
{
List original = MyDomain.list()
original.each{
it.property1 = 'NOTREADY'
it.save(flush:true)
}
List resultList
def criteria = MyDomain.createCriteria()
resultList = criteria.list()
{
eq('property1', 'READY')
}
return resultList
}
then the results come back as expected. This makes me believe it has something to do with the transaction within the integration test. Any ideas?
Verify that you are in fact saving the objects by adding failOnError: true
to your it.save(flush: true)
, also verify that your integration test is not extending GrailsUnitTestCase or any of it's sub-classes and no other test classes have messed with a metaClass and didn't clean up after themselves. I doubt the transaction has anything to do with it. If you have set transactional=false
and after the test the values haven't been changed in your database. Then there is something wrong with your save operation, such as failed validation.
Very rarely should you ever need to set the integration test to not be wrapped in a transaction, and when you do you should have a good understanding of Hibernate's session and caches.
精彩评论