Spock + Mock + Guice

928 views
Skip to first unread message

Jeff Sussna

unread,
Jun 19, 2012, 3:44:27 PM6/19/12
to spockfr...@googlegroups.com
I want to test class A that depends on class B. I want to mock class B. Class A acquires instances of B via Guice. There is a spock-guice module but I can't find any documentation so have no idea what it does or how it works. I've wrapped myself around the axle trying to figure out how to tell Guice to use B mocks instead of real B's for my unit tests. Any suggestions would be appreciated.

Peter Niederwieser

unread,
Jun 19, 2012, 4:06:00 PM6/19/12
to spockfr...@googlegroups.com
spock-guice starts one or more Guice modules before executing a spec class, and shuts them down afterwards. Services can be injected into specs with the usual Guice annotations. To see how it works, have a look at the specs for spock-guice, for example https://github.com/spockframework/spock/blob/groovy-1.8/spock-guice/src/test/groovy/org/spockframework/guice/InjectionExamples.groovy

For unit tests you shouldn't use Guice at all. Just instantiate A and pass it a mock for B. As soon as you are using Guice, your tests become integration tests.

I'm not very familiar with Guice, but if you need a test variant of B, you might be able to express this as a variation of the Guice module that contains B. It's a Guice related question, not a Spock related one. Can't tell offhand how you would register a Spock mock as a Guice service. In any case, it feels like the wrong tool for the job; better create a test implementation by hand.

Cheers,
Peter

On Jun 19, 2012, at 9:44 PM, Jeff Sussna wrote:

I want to test class A that depends on class B. I want to mock class B. Class A acquires instances of B via Guice. There is a spock-guice module but I can't find any documentation so have no idea what it does or how it works. I've wrapped myself around the axle trying to figure out how to tell Guice to use B mocks instead of real B's for my unit tests. Any suggestions would be appreciated.

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To view this discussion on the web visit https://groups.google.com/d/msg/spockframework/-/pBuxDtIwRzwJ.
To post to this group, send email to spockfr...@googlegroups.com.
To unsubscribe from this group, send email to spockframewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.

Jeff Sussna

unread,
Jun 19, 2012, 5:29:28 PM6/19/12
to Spock Framework - User


On Jun 19, 3:06 pm, Peter Niederwieser <pnied...@gmail.com> wrote:
> spock-guice starts one or more Guice modules before executing a spec class, and shuts them down afterwards. Services can be injected into specs with the usual Guice annotations. To see how it works, have a look at the specs for spock-guice, for examplehttps://github.com/spockframework/spock/blob/groovy-1.8/spock-guice/s...
>

I like it!

> For unit tests you shouldn't use Guice at all. Just instantiate A and pass it a mock for B. As soon as you are using Guice, your tests become integration tests.

In this case B is being injected into a private field rather than via
the constructor. Perhaps that's a design flaw.
Reply all
Reply to author
Forward
0 new messages