I am trying to be a good little programmer and set up Unit tests for my Grails 2.2.3 app. The unit tests that use GORM's injected .save() method are apparently not persisting to the mock test DB. For an example, here is what one test consists of:
@TestFor(TermService)
@Mock(Term)
class TermServiceTests {
void testTermCount() {
def t = new Term(code: "201310").save(validate: false, flush: true, failOnError: true)
println "Printing Term: " + t.toString()
assert 1 == Term.count() // FAILS
assert service.isMainTerm(t) // FAILS
}
}
I did a println that ends up printing Printing Term: null, meaning the Term did not save and return a Term instance. The first assertion is false with Term.count() returning 0.
Does anyone know why this might be? I have a mock Term and TermService (via the TestFor annotation, I believe), so I'm not quite sure why this wouldn't work. Thanks!
Edit: Here is my Term class.
class Term {
Integer id
String code
String description
Date startDate
Date endDate
static mapping = {
// Legacy database mapping
}
static constraints = {
id blank: false
code maxSize: 6
description maxSize: 30
startDate()
endDate()
}
}
validate:truesee if you are lacking any constraints - AlidadTermlook like? - dmahapatrovalidate: falsewould mean that validation wouldn't trigger on a save so I don't have to fill out all fields. @dmahapatro I added Term to the original post. - grantmcconnaugheyInteger id? GORM default isLong idand do not need to specify it explicitly. - dmahapatroidgenerator assigned? Can you tryt.id = 1and thent.save(...)in the test, without settingidas the map construct. - dmahapatro