mercredi 6 mai 2015

Testing afterInsert in Grails

Simply, I have the following scenario:

class OwnedRights {
    static belongsTo = [comp: Competition]

    @Transactional
    def afterInsert() {
        // Erroring out here.
        Event.findAllByComp(comp).each { event -> 
            // Do something with event.
        }
    }
}

When I attempt to save in my unit test with something like the following:

def ownedRights = new OwnedRights(params).save(flush: true, failOnError: true)

I am seeing the following stacktrace:

java.lang.NullPointerException at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130) at org.codehaus.groovy.grails.orm.support.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:85) at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:209) at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:194)

This lead to me to this Jira Issue which indicated that I was not using the @Mock annotation, however in my test, I am mocking all used domain classes:

@Mock([OwnedRights, Sport, Competition, Event])

Is this a known problem with hibernate events containing GORM logic?

Attempts to Solve

I have attempted to override the afterInsert() and beforeDelete methods by using the metaClass:

OwnedRights.metaClass.afterInsert = null;
OwnedRights.metaClass.beforeDelete = null;

and

OwnedRights.metaClass.afterInsert = {};
OwnedRights.metaClass.beforeDelete = {};

Both of which had no effect on the outcome. If I comment out the afterInsert event, the save works perfectly.

Aucun commentaire:

Enregistrer un commentaire