Hi,
I'm very new to Grails (with only a Java background) so I'm not sure if it's a bug or a feature. But I noticed a strange behavior using the build-test-data plugin. When I set a service method as readOnly=true, the domain objects previously created with build() are not saved, unless I read my Domain objects before calling my service.
Here is the code to reproduce my problem:
============= in /grails-app/domain =============
package mypackage
class MyDomain {
}
============= in /grails-app/services/mypackage =============
package mypackage
import grails.transaction.Transactional
@transactional
class MyService {
def listEntities() {
MyDomain.list()
}
@Transactional(readOnly=true)
def listEntitiesReadOnly() {
MyDomain.list()
}
============= in /test/unit/mypackage =============
package mypackage
import grails.buildtestdata.mixin.Build
import grails.test.mixin.TestFor
import spock.lang.Specification
@testfor(MyService)
@build(MyDomain)
class MyServiceSpec extends Specification {
void "test list() using default"() {
given:
MyDomain.build()
when:
def res = service.listEntities()
then:
res.size()==1
}
void "test list() with service read-only"() {
given:
MyDomain.build()
when:
def res = service.listEntitiesReadOnly()
then:
res.size()==1
}
void "test list() with service read-only with assert before"() {
given:
MyDomain.build()
MyDomain.list()
when:
def res = service.listEntitiesReadOnly()
then:
res.size()==1
}
}
I'm using Grails 2.4.4 with build-test-data 2.4.0.
The first and third test work, not the second one. Does anybody know why?
Thanks in advance.