Grails + Spock:在嵌入式 hasMany 字段上执行 addTo* 时出现 NullPointerException
Grails + Spock: NullPointerException when doing addTo* on an embedded hasMany field
我有域名class:
class MarketCommoditiesConfiguration extends MarketConfiguration {
static mapWith = "mongo"
static hasMany = ['commodities': Commodity]
static embedded = ['commodities']
...
在 addToCommodities
之后执行 save
会引发以下异常。
| 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)
将 Keyword
放在 @Mock
注释中解决了它。 http://grails.1312388.n4.nabble.com/Grails-MongoDb-embedded-collection-causes-nullpointerexception-td4644098.html 包含更多涉及的讨论。
I know this is super-old, but a Google search led me here whilst
facing the same issue. Make sure that you have added @Mock(User) to
your unit test class. In my case, I had multiple levels of embedded
domain classes, and each of them had to be mocked.
- clarkdustin
我有域名class:
class MarketCommoditiesConfiguration extends MarketConfiguration {
static mapWith = "mongo"
static hasMany = ['commodities': Commodity]
static embedded = ['commodities']
...
在 addToCommodities
之后执行 save
会引发以下异常。
| 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)
将 Keyword
放在 @Mock
注释中解决了它。 http://grails.1312388.n4.nabble.com/Grails-MongoDb-embedded-collection-causes-nullpointerexception-td4644098.html 包含更多涉及的讨论。
I know this is super-old, but a Google search led me here whilst facing the same issue. Make sure that you have added @Mock(User) to your unit test class. In my case, I had multiple levels of embedded domain classes, and each of them had to be mocked. - clarkdustin