AtUnit creates one instance of test class for the Module, another for tests

1 view
Skip to first unread message

JasonQR

unread,
Mar 4, 2011, 8:52:47 AM3/4/11
to atunit
This is a problem because you can't bind to instance variables in
#configure(). Here's a simplified example:

@RunWith(AtUnit.class)
@Container(Container.Option.GUICE)
public class InjectLocalField extends AbstractModule {

private final Object myObj = new Object();

@Override
protected void configure() {
bind(Object.class).toInstance(myObj);
}

@Test
public void testSomethingThatUsesMyObj() {
// Execute code that uses myObject

Assert.assertEquals("Expected result", myObj.toString());
}
}

The fix for this is pretty simple, just change line 48 in
GuiceContainer from:
injector.getInstance(testClass);
to:
injector.injectMembers(testClassInstance)
where testClassInstance is the instance used as a Module. There's no
reason the Module itself can't have its fields injected.
Reply all
Reply to author
Forward
0 new messages