I have a complex set of bindings that include many private bindings to solve the robot legs problem.
Because of Guice's limited ability to report intelligible biding errors, I'm wondering what effective tools or techniques, if any, besides reading Guice's runtime exceptions are available to troubleshoot runtime binding errors.
Stepping through configuration code isn't helpful, because the configuration happens at boot time rather than at object instantiation time, where errors usually occur.
The Guice graph plugin would likely be useful if it worked--my experiments with it have resulted in incorrect graphs.
A binding is an object that corresponds to an entry in the Guice map. You add new entries into the Guice map by creating bindings.
Guice figures out how to give you an Emailer based on the type. If it's a simple object, it'll instantiate it and pass it in. If it has dependencies, it will resolve those dependencies, pass them into it's constructor, then pass the resulting object into your object.
Interface Injector. public interface Injector. Builds the graphs of objects that make up your application. The injector tracks the dependencies for each type and uses bindings to inject them. This is the core of Guice, although you rarely interact with it directly.
The Guice module helps you to inject Guice managed components into your play application. The injection points are defined by the upcoming @javax. inject. Inject annotation, which is bundled with play at the moment.
I found the following two tips useful for debugging from this answer:
Binder.skipSources() is useful if you write generic binding helper methods and Guice only reports the line number of the generic helper method, but you (most likely) actually want the line number of the caller one level up the stack instead.
I'm developing for Android, so the build time can be quite slow from the time I modify my bindings until I see the results of my changes on the device or simulator. So I've developed unit tests that will verify the Guice bindings directly on the host PC. Even if you're not developing for Android, it can be helpful to write Guice binding unit tests as follows. Right now, mine look something like this (here in Scala--Java would look similar)
class ProviderTest {
var injector : Injector = null
@Before
def setUp() {
injector = Guice.createInjector(
new BindModule1(),
new BindModule2(),
new BindGlobals()
)
}
@After
def tearDown() {
}
@Test def InjectedClass1WasBound() {
val provider = injector.getProvider(classOf[InjectedClass1])
}
@Test def InjectedClass2WasBound() {
val provider = injector.getProvider(classOf[InjectedClass2])
}
}
I write tests starting from the most deeply-bound class. I.e., if C is injected into B, which is injected into A, I'll start testing at C. If unit testing C's binding fails, I'll start commenting out the injected fields in C until I get the binding to succeed. Then I move my way up the injection hierarchy repeating this process.
Of course if you follow test-driven development, and make sure to include full-coverage Guice-binding tests in your suite, you'll detect these errors as soon as you break a binding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With