Grails modules do not recognize .save () in a domain class

I try to be a good programmer and set up unit tests for my Grails 2.2.3 application. Unit tests that use the GORM method introduced by .save() are apparently not stored in the test database. For example, here is what consists of one test:

 @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 made println , which ends printing Printing Term: null , which means that the term has not saved and returned an instance of Term. The first statement is false if Term.count() returns 0.

Does anyone know why this could be? I have a Term and TermService layout (via the TestFor annotation, I believe), so I'm not quite sure why this will not 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() } } 
+4
source share
1 answer

It looks like the id assigned generator since you mentioned using an outdated database. Plus, the id is not bound to the domain class by default (map building will not work for id). So, I think you should end up using as shown below:

 def t = new Term(code: "201310") t.id = 1 t.save(...) 
+8
source

Source: https://habr.com/ru/post/1495313/


All Articles