When developing TestNG tests with Guice, a la this:
@Guice(modules=MyTestModule.class)
public class MyTestClass {
}
It's very easy to use field injection to declare the dependencies of the test class. Now, suppose we have this:
@Guice(modules=MyTestModule.class)
public class MyTestClass {
@Inject private Foo foo;
@Test
public void myTest() {
Injector injector = Guice.createInjector(new SomeOtherModule());
}
}
Because of field injection, if SomeOtherModule
doesn't provide a binding for Foo
, myTest()
will fail due to CreationException
, because it does not have the ability to create a MyTestClass
, even if we do not intend for the test to create a MyTestClass
with that injector.
Is there a way to express that intention, without changing into constructor injection of the Foo
? That is, is there a way to somehow make it not an error to create the injector, but do make it an error when trying to specifically provision a MyTestClass
, without changing how MyTestClass
is written?