Hi Rémy,
your example looks quite strange to me as you are using the Injector somewhat manually. Maybe you want to illustrate the example but maybe you also got wrong how to use the Injector effectively.
In case of your LoveMachine I would have expected this (code like without DI)
class LoveMachine {
int answer;
LoveMachine(int answer) { this.answer = answer; }
int getAnswer() { return answer; }
}
Than binding it like
protected void declare() {
bind(LoveMachine.class).toConstructor();
bind(int.class).to(42);
}
Also remember that
int is a fairly common class so you might want to use this bind instead of the untargeted one above
injectingInto(LoveMchine.class).bind(int.class).to(42);
In case of a test that should use another value/instance for that int I think the best is to create a
TestRootBundle on top of your normal root.
class TestRootBundle extends BootstrapperBundle {
bootstrap() {
install(RootBundle.class);
uninstall(UsualModuleX.class);
install(TestModuleX.class);
//...
}
}
This approach requires that the part you want to "override" is isolated in a Bundle or Module so that you can un-install it and install a Bundle or Module that does the same setup for test.
An alternative to this is to do a more specific binding but that doesn't seam very clean in the long run.
Beside this most general way of composing a configuration differently you can use
With any of those you use the same RootBundle in both test and usual setup but chose another edition/feature, option or configuration.
Picking a specific Module based on scenario (as you describe it) is what edition/features and options or another root bundle on top are used for.
You should not create if-else logic within any of your modules! While this seams easy to do it will mess you up in the long run (except you have a toy sized project).
Instead hard-code or compose a Globals object (with Options and Edition) and bootstrap the one tree of Bundles and Modules you use for everything.
The chosen option/edition or feature set will take care of including the right composition of bindings.
Cheers
Jan