我有域类:
class MarketCommoditiesConfiguration extends MarketConfiguration {
static mapWith = "mongo"
static hasMany = ['commodities': Commodity]
static embedded = ['commodities']
...
在save
之后执行addToCommodities
会引发以下异常。
| java.lang.NullPointerException
at org.grails.datastore.mapping.model.types.Association.getInverseSide(Association.java:71)
at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedInstance(NativeEntryEntityPersister.java:1254)
at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedInstance(NativeEntryEntityPersister.java:1215)
at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedToMany(NativeEntryEntityPersister.java:1188)
at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.persistEntity(NativeEntryEntityPersister.java:1077)
at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.persistEntity(NativeEntryEntityPersister.java:1138)
at org.grails.datastore.mapping.engine.EntityPersister.persist(EntityPersister.java:160)
at org.grails.datastore.mapping.core.AbstractSession.persist(AbstractSession.java:522)
at org.grails.datastore.gorm.GormInstanceApi.doSave(GormInstanceApi.groovy:194)
at org.grails.datastore.gorm.GormInstanceApi.save_closure5(GormInstanceApi.groovy:162)
at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:302)
at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:37)
at org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:161)
at com.apposit.terra.connect.service.MarketService.addCommodityToMarket(MarketService.groovy:248)
at com.apposit.terra.connect.service.MarketServiceSpec.Test addCommodityToMarket creates a new next month configuration if current month's configuration doesn't exist(MarketServiceSpec.groovy:164)
答案 0 :(得分:5)
将Keyword
置于@Mock
注释中解决了它。 http://grails.1312388.n4.nabble.com/Grails-MongoDb-embedded-collection-causes-nullpointerexception-td4644098.html包含更多参与讨论。
我知道这已经超级老了,但谷歌搜索引领我来到这里 面临同样的问题。确保已将@Mock(用户)添加到 你的单元测试课。就我而言,我有多个级别的嵌入式 域类,每个都必须被嘲笑。 - clarkdustin