I was not aware of override and it looks exactly like what I need. Thanks for the pointer, Fred!
-- Cédric On Thu, Sep 6, 2012 at 10:18 AM, Fred Faber <[email protected]> wrote: > Have you tried Modules.override? > > Modules.override(new ProdModule()).with(new TestModule()); > > Fred > > On Thu, Sep 6, 2012 at 1:14 PM, Cédric Beust ♔ <[email protected]> wrote: > >> Hi everyone, >> >> I am considering using separate test modules for testing but I'm a bit >> bothered by the fact that Guice won't let me bind the same class to >> multiple targets. For example, assume I currently have a main module that >> binds two classes: >> >> MainModule: >> bind(A.class).to(a); >> bind(B.class).to(b); >> >> I want to test the A class, so I create an "aMock" version in TestModule: >> >> TestModule: >> bind(A.class).to(aMock); >> >> Then I try to run my tests with both MainModule and TestModule, and of >> course, Guice complains that A.class is bound twice. So now I find myself >> having to introduce a third module that contains the common bindings: >> >> CommonModule: >> bind(B.class).to(b); >> >> And I run: >> >> Production: >> CommonModule, MainModule >> >> Tests: >> CommonModule, TestModule >> >> I understand that this architecture is not necessarily bad but it's a bit >> tedious that whenever I want to test a certain class, I need to 1) remove >> it from CommonModule 2) move it to MainModule and 3) create a mock in >> TestModule. >> >> Ideally, I'd like to tell Guice to allow shadowing bindings so I can just >> place TestModule at the top of the list of modules and Guice will ignore >> duplicate bindings that it will find in MainModule. This way, I never need >> to create CommonModule. With this feature, I now have: >> >> Production: >> MainModule >> >> Test: >> TestModule, MainModule >> >> Benefits: one less module to create and less bookkeeping moving bindings >> in and out of modules. >> >> Thoughts? >> >> -- >> Cédric >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "google-guice" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/google-guice?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "google-guice" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en. > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
