Does it make any sense to unit-test Modules?

2,857 views
Skip to first unread message

Pablo Fernandez

unread,
Sep 9, 2009, 5:08:09 PM9/9/09
to google-guice
Hi,

I'm running my tests with a code coverage tool and for the only
classes I get 0% coverage are my Guice Modules. No big deal, but just
lead me to think...

Is there any benefit in testing Guice Modules? I've tried to test one
and that implied:

1) using Module interface instead of AbstractModule class (code
readability decreased a bit)
2) mocking the hell out of the binder (so much that I don't really
know if I'm actually testing something)

In fact after doing this, the test and the module look pretty much the
same...

isn't this just fancy code duplication? has anyone tested their own
modules? is there any outcome in doing that (aside reaching 100% code
coverage)?

Thanks

Sam Berlin

unread,
Sep 9, 2009, 5:17:35 PM9/9/09
to google...@googlegroups.com
You can indirectly test modules by using integration tests where you create an Injector using your modules and ensure that pieces of the program work.  As far as testing any logic in the modules... there ideally would be no logic in the modules, just bindings.  Logic should be separated into different modules that are installed in different circumstances.

Sam

Pablo Fernandez

unread,
Sep 9, 2009, 5:55:34 PM9/9/09
to google-guice
I actually test some modules indirectly by creating the injector in
the tests... this is for some unit tests where I could not fully
decouple the classes.

So, bottom line is: there's no point in *unit* testing modules, right?

Thanks for your reply

On Sep 9, 2:17 pm, Sam Berlin <sber...@gmail.com> wrote:
> You can indirectly test modules by using integration tests where you create
> an Injector using your modules and ensure that pieces of the program work.
> As far as testing any logic in the modules... there ideally would be no
> logic in the modules, just bindings.  Logic should be separated into
> different modules that are installed in different circumstances.
>
> Sam
>
> On Wed, Sep 9, 2009 at 5:08 PM, Pablo Fernandez
> <fernandezpabl...@gmail.com>wrote:

Dhanji R. Prasanna

unread,
Sep 9, 2009, 7:42:44 PM9/9/09
to google...@googlegroups.com
If your modules have logic you should definitely unit test them. If you just want to ensure the bindings are ok, write a small unit test that boots up the injector in Stage.TOOL. This wont instantiate anything but will still check to make sure the bindings are correct.

Dhanji.

Pablo Fernandez

unread,
Sep 9, 2009, 7:50:12 PM9/9/09
to google-guice
Hey Dhanji,

I'm checking the Stage.TOOL javadoc but got confused by this:

"We need binding meta data but not a functioning Injector. Do not
inject members of instances."

What does it do really? It just checks if the dependencies are
correct? :S

On Sep 9, 4:42 pm, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:
> If your modules have logic you should definitely unit test them. If you just
> want to ensure the bindings are ok, write a small unit test that boots up
> the injector in Stage.TOOL. This wont instantiate anything but will still
> check to make sure the bindings are correct.
> Dhanji.
>
> On Thu, Sep 10, 2009 at 7:55 AM, Pablo Fernandez <fernandezpabl...@gmail.com

Dhanji R. Prasanna

unread,
Sep 9, 2009, 8:09:18 PM9/9/09
to google...@googlegroups.com
On Thu, Sep 10, 2009 at 9:50 AM, Pablo Fernandez <fernande...@gmail.com> wrote:

Hey Dhanji,

I'm checking the Stage.TOOL javadoc but got confused by this:

"We need binding meta data but not a functioning Injector. Do not
inject members of instances."

What does it do really? It just checks if the dependencies are
correct? :S

Yes, and that the configuration is correct. One benefit of Guice is that it checks to make sure all dependencies can be satisfied as soon as your start up your injector.

Dhanji.

Reply all
Reply to author
Forward
0 new messages