Optional injection by module

37 views
Skip to first unread message

rdgoite

unread,
Sep 25, 2011, 8:08:52 PM9/25/11
to google-guice
Hello.

I have several Guice modules in my application. Some of them I use for
actual deployment and some of them I use just for testing. Some of the
test modules don't have all the dependencies because I'm limiting the
bindings to things that I want to test and leave others for mocking.
This forces me to make all my @Inject annotations optional. However, I
want to keep injections as strict as its default behavior so that I
get the errors if there are any unresolved/unconfigured components
during deployment.

Is there a way to indicate optional bindings by module and not through
@Inject(optional=true)? For example, I have a TestModule where I'm not
going to indicate a binding for, say, SomeDependency class because
I'll just mock it for testing. However, say I have ActualModule where
I'll indicate binding of SomeDependency to ActualDependency.

Thanks.

Fred Faber

unread,
Sep 25, 2011, 10:16:22 PM9/25/11
to google...@googlegroups.com
The common solution here is to use:     
    Modules.override(productionModule)
         .with(testModule);

This lets you test without needing to relax your production bindings.

Fred



--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


Alex opn

unread,
Sep 26, 2011, 6:44:53 AM9/26/11
to google...@googlegroups.com
Hello,

where would I use this? In the initializations of my tests, I suggest.

Fred Faber

unread,
Sep 26, 2011, 12:38:19 PM9/26/11
to google...@googlegroups.com
You'd use it where you would create a test-based injector:

Injector injector = Guice.createInjector(Modules.override(...));

Thomas Broyer

unread,
Sep 26, 2011, 4:47:34 PM9/26/11
to google...@googlegroups.com
And I guess the override would explicitly bind to the mocks?

Fred Faber

unread,
Sep 26, 2011, 5:35:38 PM9/26/11
to google...@googlegroups.com
Yes, exactly:


On Mon, Sep 26, 2011 at 4:47 PM, Thomas Broyer <t.br...@gmail.com> wrote:
And I guess the override would explicitly bind to the mocks?

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/xRyn9UxATzAJ.

rdgoite

unread,
Sep 28, 2011, 3:01:29 PM9/28/11
to google-guice
Thanks for this. However, I'm not sure if I'd want to get the
production module involved in the tests. Actually, in my case, there's
really not much use overriding the production module since essentially
my test modules are subsets of the actual production module. I create
mock objects using Mockito so I don't really use binding for those
with Guice. Basically, what I want is to declare an interface and then
bind it to a concrete class that I'm going to test. Any dependencies
for the actual implementation will be supplied elsewhere.
Reply all
Reply to author
Forward
0 new messages