I have a domain class in my Grails application that is reasonably complex. It depends on a couple of services and has some mandatory attributes, hence, its building process takes a few lines of code.
When writing unit specs (I'm using Spock), many times we have to repeat the very same code in order to have a valid instance of this domain class that can be used on each spec.
I've been able to decrease the size of this problem by creating a builder class, which takes care of filling mandatory attributes with valid data, but I still need to pass mocks of services to that builder every time, something like this:
MyComplexDomain d = new ComplexDomainBuilder()
.withFirstService(Mock(FirstService))
.withSecondService(Mock(FirstService))
.build()
The reason I need to pass all of these mocks every time is because Spock doesn't allow the creation of mocks outside a Spec class. And I don't really need those services to be nothing more than mocks in these cases.
What I'd really like to do would be something like this:
MyComplexDomain d = new ComplexDomainBuilder().build()
Where the builder classes would take care of mocking the appropriate services and assemble a valid instance of my domain class.
Is it possible to do that somehow? I mean, is there a way for my Specs to delegate the creation of mocks to some other class? I tried to pass an instance of MockingApi to the builder class, but didn't work either.
Thanks!
Aucun commentaire:
Enregistrer un commentaire