运行测试用例时为属性指定的不存在的映射属性[none]

时间:2015-08-31 09:35:03

标签: grails gorm spock

以下是域名:

package com.test
class Person{
    String name
    static mappedBy= [friends:'none']
    static hasMany=[friends:Person]
}

它适用于正常情况,但是当我尝试通过在spock中使用@Mock注释进行模拟来进行测试来保存时,得到以下异常:

|  org.grails.datastore.mapping.model.IllegalMappingException:

     Non-existent mapping property [none] specified for property [friends] in class [com.test.Person]
        at org.grails.datastore.mapping.model.config.GormMappingConfigurationStrategy.establishRelationshipForCollection(GormMappingConfigurationStrategy.java:364)
        at org.grails.datastore.mapping.model.config.GormMappingConfigurationStrategy.getPersistentProperties(GormMappingConfigurationStrategy.java:206)
        at org.grails.datastore.mapping.model.AbstractPersistentEntity.initialize(AbstractPersistentEntity.java:87)
        at org.grails.datastore.mapping.model.config.GormMappingConfigurationStrategy.getOrCreateAssociatedEntity(GormMappingConfigurationStrategy.java:675)
        at org.grails.datastore.mapping.model.config.GormMappingConfigurationStrategy.establishDomainClassRelationship(GormMappingConfigurationStrategy.java:632)
        at org.grails.datastore.mapping.model.config.GormMappingConfigurationStrategy.getPersistentProperties(GormMappingConfigurationStrategy.java:214)
        at org.grails.datastore.mapping.model.AbstractPersistentEntity.initialize(AbstractPersistentEntity.java:87)
        at org.grails.datastore.mapping.model.AbstractMappingContext.initializePersistentEntity(AbstractMappingContext.java:250)
        at org.grails.datastore.mapping.model.AbstractMappingContext.initialize(AbstractMappingContext.java:239)
        at grails.test.mixin.domain.DomainClassUnitTestMixin.initializeMappingContext(DomainClassUnitTestMixin.groovy:150)
        at grails.test.mixin.domain.DomainClassUnitTestMixin.mockDomains(DomainClassUnitTestMixin.groovy:144)
        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)
        at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
        at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)

以下是测试用例:

import grails.test.mixin.Mock
import grails.test.mixin.TestFor
import spock.lang.Specification

@TestFor(PersonController)
@Mock([Person])
class PersonControllerSpec extends Specification{

    def "test save person"(){
        given:"some person request parameters set for person"
        params.putAll([name:'test234', friends:[], action:'save', controller:'person'])

        when:"perosn.save is called"
        controller.save()

        then:"it must create person object"
        Person.count() == 1
      }

}

知道在这种情况下可以做些什么吗?

2 个答案:

答案 0 :(得分:2)

此问题在grails 2.5.1之后修复:https://github.com/grails/grails-core/issues/669

我通过升级到Grails 2.5.5解决了我们项目中的问题,并将使用过的hibernate4插件更新为版本hibernate4:5.0.0.RELEASE。

答案 1 :(得分:1)

虽然已存在针对同一问题的Jira jira.grails.org/browse/GRAILS-11285

我能够通过更改grails-datastore jar的版本来找到解决方法,因为我们在下面得到了例外

   Non-existent mapping property [none] specified for property [friends] 

在GormMappingConfigurationStrategy.java中,它位于grails-datastore-core jar下。

因此,在将版本从3.0.6升级到3.1.5后,为了安全起见,添加了对测试环境的检查,如下所示

        if (Environment.getCurrentEnvironment() == Environment.TEST) {

            compile 'org.grails:grails-datastore-core:3.1.5.RELEASE'
        }

现在,jar只会添加到测试环境中,我们可以继续测试用例。

干杯!!!