in Bob's and Kevin's presentation, they emphasize the possibility to
do unit testing of classes that use Guice by using the constructors
directly, thus skipping Guice.
However, I reckon that Guice's injection mechanisms are in general
very well suited for being used for unit testing. My approach would be
to define one Module that is to be used in the actual environment. And
one that is to be used for unit testing, thus binding to mock-ups,
etc..
Are there circumstances under which the first approach would be
favorable? I am just a little surprised that you advertise not using
Guice in a case that I reckon is perfectly suited for Guice.
hoping for comments,
Christoph
> However, I reckon that Guice's injection mechanisms are in general
> very well suited for being used for unit testing. My approach would be
> to define one Module that is to be used in the actual environment. And
> one that is to be used for unit testing, thus binding to mock-ups,
> etc..
>
I do this when doing more functional tests that will in fact hit the
database, or call another service or hit the web or whatever they might
do. In this case I create a setter (since JUnit has constructor
requirements) and set it to @Inject and then make sure that everything
is in the test module (which can be setup in a @Before method with JUnit
4) or that they have @ImplementedBy guice annotations.
-bp
> I actually use both approaches....
Me too.
If I only want to test class A, which depends on interfaces B and C,
it's probably easier and cleaner to not use Guice. Just create mocks
for B and C, and pass them into the real A.
If I want to test how the trio of A, B, and C work as a whole -- a
more interesting test, oftentimes -- then I can still use this
approach. Create a RealC using a MockD and MockE, Create a RealB
using the RealC, MockE, and MockF, etc.
But this can get out of hand. So another alternative is to bang out a
little Module that sets up the environment you want, make an Injector,
and getInstance(A.class) from that. Very convenient!
Does this help?
K
@Ignore
public class BaseTest {
private Module[] modules;
public void setModules(Module... modules) {
this.modules = modules;
}
@Before
public void setupGuice() {
Injector i = Guice.createInjector(modules);
i.injectMembers(this);
}
}
public class MyTest extends BaseTest {
private FooService fooService;
public MyTest() {
setModules(new FooModule());
}
@Inject
public void setFooService(FooService fooService) {
this.fooService = fooService;
}
@Test
public void testSomething() {}
}
-bp