java - Providing an object for a field in constructing an object in Guice -
suppose have class z, injects class a:
class z { @inject public z(.., arg, ..) { .. } } suppose class has constructor:
@inject public a(b arg0, c arg1, d arg2) { ... } i want injection work normally, except in special cases, want provide 1 of arguments. e.g., constuct using cobject of c class. note constructed using z.
i want because writing functional tests z, want provide different kinds of fakes depending on test. 1 test file contain 1 kind of fake b, or c, or d.
i recommend using modules.override here, has documentation use functional testing. use sparingly, things can messy , hard-to-follow otherwise, this:
@before public void createinjector() { this.injector = guice.createinjector( modules.override(new yourzabcdmodule()).with(new abstractmodule() { @override public void configure() { bind(b.class).to(fakeb.class); } })); } or, in modules.override documentation, use smaller modules more-granular combinations:
@before public void createinjector() { this.injector = guice.createinjector( new zamodule(), new fakebmodule(), new cmodule(), new dmodule()); }
Comments
Post a Comment