I have just started playing with Guice, and in one experiment, I can imagine that I want to take a test. I want to override a compulsory I think I want to use all the other production level bindings to ensure that everything is properly setup and to avoid duplication, to avoid duplication.
Thought I have the following module
Public Class Production Module Value {Public Zero Configure (Binder Binder) {Binder. Bind (Interface A Class). To (ConcreteA class); Binder.bind (InterfaceB.class). ConcreteB.class; Binder.bind (InterfaceC.class). ConcreteC.class; }}
And in my testing I only want to override the interface, while keeping the efficiency of interface A and interface B, so I want something like this:
module test module = new module () {public zero configuration (binder machine) {binder} Bind (Interface C Class) To (mocky class); }}; Guice.createInjector (new production module), testmodule);
I have also tried the following with any luck:
module test module = new output module () {public zero configuration (binder) Binder) {super .configure (binder); Make binder.bind (InterfaceC.class). Expedition (MockC.class); }}; Guice.createInjector (testModule); Do anyone know if it is possible that what I want to do or is I completely barking the wrong tree ?? --- Follow-up: It appears that if I use the @implified tag on the interface and then provide binding in test case, which would have 1-1 mapping between the interface and the implementation So it works well. Apart from this, after discussing this with a colleague, it appears that we will keep the path of override an entire module below and ensure that our module is correctly defined. . It seems as if this could be the cause of a problem, where a binding is lost in a module and it needs to be moved, thus possibly breaking the test load because the binding is no longer available for override May be. P>
It may be that the answer you are looking for, but if you are writing a unit test, You probably should not use the injector and instead of hand-in-the-counter or imitation items injected.
On the other hand, if you really want to change a binding, then you Modules.override (..)
:
public class Production Module Component Module {Public Zero Configure (Binder Binder) {binder.bind (Interface A Class) .to (ConcreteA class); Binder.bind (InterfaceB.class). ConcreteB.class; Binder.bind (InterfaceC.class). ConcreteC.class; }} Public class test module tool module {public zero configuration (binder.bind }} Guice.createInjector (Module Override (New Production Module ()) with. (New Test Module ()));
See details.
But Module Recommended as javadoc for overrides (..)
, you should prepare your module in such a way that you do not need to override binding. For example, you have a different module Completed by transferring the bindings of interface c
for.
Comments
Post a Comment