I have a domain class
class Door {
String id
String url
Room room
static hasOne = [lock: DoorLock]
static mapping = {
id generator: 'uuid'
}
static constraints = {
url nullable: false
room nullable: true
lock nullable: true
}
and I'm trying to write a simple Spock test for it (actually without any logic first, just to check mocking capabilities of Grails).
@TestFor(Door)
class DoorSpec extends Specification {
protected void setup() {
mockForConstraintsTests(Door)
}
def cleanup() {
}
void "test something"() {
when:
def door = new Door()
then:
true
}
}
when I run the test (grails test-app Door) it fails with error:
java.lang.IllegalStateException: Method on class [citypoints.Door] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.
at citypoints.Door._clinit__closure2(Door.groovy:17)
at org.grails.datastore.mapping.config.groovy.MappingConfigurationBuilder.evaluate(MappingConfigurationBuilder.groovy:80)
at org.grails.datastore.mapping.config.AbstractGormMappingFactory.createMappedForm(AbstractGormMappingFactory.java:63)
at org.grails.datastore.mapping.keyvalue.mapping.config.GormKeyValueMappingFactory.createMappedForm(GormKeyValueMappingFactory.java:39)
at org.grails.datastore.mapping.keyvalue.mapping.config.GormKeyValueMappingFactory.createMappedForm(GormKeyValueMappingFactory.java:28)
at org.grails.datastore.mapping.keyvalue.mapping.config.KeyValuePersistentEntity.<init>(KeyValuePersistentEntity.java:35)
at org.grails.datastore.mapping.keyvalue.mapping.config.KeyValueMappingContext.createPersistentEntity(KeyValueMappingContext.java:95)
at org.grails.datastore.mapping.model.AbstractMappingContext.addPersistentEntities(AbstractMappingContext.java:174)
at grails.test.mixin.domain.DomainClassUnitTestMixin.mockDomains(DomainClassUnitTestMixin.groovy:133)
at grails.test.mixin.domain.DomainClassUnitTestMixin.mockDomain(DomainClassUnitTestMixin.groovy:166)
at grails.test.mixin.domain.DomainClassUnitTestMixin.mockDomain(DomainClassUnitTestMixin.groovy:165)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.runtime.extension.builtin.JUnitFixtureMethodsExtension$FixtureType$FixtureMethodInterceptor.intercept(JUnitFixtureMethodsExtension.java:145)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
| Completed 1 unit test, 1 failed in 0m 3s
However when I remove the constraint lock nullable: false
from the Door class, the test runs fine.
Is it a Grails bug or I do something wrong?
PS Tried to add mockDomain(DoorLock)
before mockForConstraintsTests(Door)
=> nothing changes
PPS I have Grails 2.3.7 and hibernate plugin ":hibernate:3.6.10.9"
I found reproducible scenario for this error: Error appears only when testing a domain class, which has a property (of any type) with the name 'lock' and a constraint against this property. Looks like Grails bug.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.