views:

324

answers:

1

I am trying to use Guice for a test framework based on TestNG. This frameworks analyzes the test class for dependencies and provides them eliminating the need to build them in tests.

Guice is all about injection and I think is a good fit for the framework. But the question is how do I define bindings after I have created the injector? This is needed because tests may override bindings to substitute default implementations with mocks.

Besides that, I want to guess the implementation at runtime in some cases based on class names conventions. Sounds like Just-in-type binding feature. But how do I provide my own just-in-time binding provider?

+2  A: 

That kind of dynamic behaviour isn't supported out-of-the-box, but you can achieve a lot with module rewriting. Take a look at Guiceberry, which already implements mock-substitution for JUnit tests. (And consider submitting a TestNG patch to them, they'd love that!)

Jesse Wilson
I also guess that I can find and instantiate the desired class by myself (most of them have parameterless constructors) and then have the injector inject it's members
artemb