I’m having a strange problem with unit test in grails, when I try to save a mocked object it throw a NullPointerException
java.lang.NullPointerException
at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.persistEntity(NativeEntryEntityPersister.java:798)
at org.grails.datastore.mapping.engine.EntityPersister.persist(EntityPersister.java:130)
at org.grails.datastore.mapping.core.AbstractSession.persist(AbstractSession.java:477)
at org.grails.datastore.gorm.GormInstanceApi.doSave(GormInstanceApi.groovy:166)
at org.grails.datastore.gorm.GormInstanceApi$_save_closure4.doCall(GormInstanceApi.groovy:143)
at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:301)
at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:34)
at org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:142)
at it.rascio.ConverterServiceTests.testConvert(ConverterServiceTests.groovy:64)
what I have the test like:
@TestFor(ConverterService)
@TestMixin(GrailsUnitTestMixin)
@Mock([MyObj1, MyObj2, LittleObject])
class ConverterServiceTests{
def littleObject;
void setUp(){
littleObject = new LittleObject(prop1: 'hello', prop2: 'world')
}
void testConvert(){
MyObj1 obj = new MyObj1()
obj.prop = 'my property'
obj.littleObject = littleObject
obj = obj.save(flush:true, failOnError: true)
MyObj2 b = new MyObj2()
b.obj1Id = obj.id;
assert service.convert(b) == null;
}
The exception is thrown when I call the .save method.
I’ve seen that if I remove the littleObject it works.
Can someone have an idea of what is this exception?
If your class
MyObj1contains some embedded properties of other types or has any other cascade saves you should add these classes to@Mockannotation list.