简体   繁体   English

对关系受约束的领域类的Grails单元测试

[英]Grails unit test for a domain class with constraint on relationship

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). 并且我正在尝试为此编写一个简单的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
     }
}

when I run the test (grails test-app Door) it fails with error: 当我运行测试(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

However when I remove the constraint lock nullable: false from the Door class, the test runs fine. 但是,当我从Door类中删除lock nullable: false的约束lock nullable: false ,测试运行正常。

Is it a Grails bug or I do something wrong? 是Grails的错误还是我做错了?

PS Tried to add mockDomain(DoorLock) before mockForConstraintsTests(Door) => nothing changes PS试图在mockForConstraintsTests(Door)之前添加mockDomain(DoorLock) mockForConstraintsTests(Door) =>没有任何变化

PPS I have Grails 2.3.7 and hibernate plugin ":hibernate:3.6.10.9" PPS我有Grails 2.3.7和休眠插件“: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. 我发现了该错误的可重现方案:仅在测试域类时出现错误,该域类具有名称为“ lock”的属性(任何类型的属性),并且对该属性具有约束。 Looks like Grails bug. 看起来像Grails错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM