lundi 2 mars 2015

Unit test abstract class with injected service

My application has a set of subclasses that all extend a certain base class.


BaseClass.groovy



abstract class Base {

def beforeInsert() {
userCreated = springSecurityService.currentUser
}

/* other stuff */

}


ConcreteClass.groovy



class Concrete extends Base {

/* stuff, doesn't matter */

}


And I am writing a test that must instantiate several Concretes:


AsdfServiceSpec.groovy



def "under x circumstances, check for all instances that meet y criteria"() {

setup: "create 3 concrete classes"
(1..3).each { new ConcreteClass(a: 'yes').save(validate: false) }

/* and then the actual test ... */

}


The problem arises when I save the instances, because of that springSecurityService up in BaseClass. I can't find a way to stub it out for the unit test!



  • I can't @Mock an abstract class, which is needed to use defineBeans.

  • Base.springSecurityService raises an NPE.

  • Base.metaClass.springSecurityService and Base.metaClass.static.springSecurityService compile but don't work.

  • And apparently you can't override events in Grails, so I can't just bypass the beforeInsert, which would otherwise be fine.


Anybody know how to unit test an abstract class with an injected service?


Aucun commentaire:

Enregistrer un commentaire