我有一个域类
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
}
我正在尝试为它编写一个简单的Spock测试(实际上没有任何逻辑,只是为了检查Grails的模拟功能)。
@TestFor(Door)
class DoorSpec extends Specification {
protected void setup() {
mockForConstraintsTests(Door)
}
def cleanup() {
}
void "test something"() {
when:
def door = new Door()
then:
true
}
}
当我运行测试(grails test-app Door)时,它失败并显示错误:
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
但是,当我从Door类中删除约束lock nullable: false
时,测试运行正常。
这是Grails错误还是我做错了什么?
P.S。试图在mockDomain(DoorLock)
=&gt;之前添加mockForConstraintsTests(Door)
什么都没有改变
P.P.S。我有Grails 2.3.7和hibernate插件“:hibernate:3.6.10.9”