I have finally created my first module in our big scale application.
It's clean, readable and cool :)
My next step was to test it (in the following modules I will try to
'test first').
So I created a Test class.
Created a TestModule that extends AbstractModule and installs my own
module (I did it like that, as my module is dependent on modules that
injected separately).
In the test I called to the injector and verified that instances are
not null, in Singleton I verifies sameInstance.
Is there a best approach to test the module?
Here's part of the test:
@SuppressWarnings("unchecked")
@Test
public void shouldConstructObjects() {
final Properties properties = new Properties();
properties.setProperty("MpsQueryFilter", "1=1");
// more properties here...
final MpsOperations mpsOperations =
mock(MpsOperations.class);// created in a different module
final IDataCollector dataCollector =
mock(IDataCollector.class); // created in a different module
final RecordsCounterOperations recordsCounterOperations =
mock(RecordsCounterOperations.class); // created in a different module
Module testModule = new AbstractModule() {
@Override
protected void configure() {
Names.bindProperties(binder(), properties);
bind(MpsOperations.class).toInstance(mpsOperations);//
created in a different module
bind(new TypeLiteral<IDataCollector<CoreMpsData>>()
{}).toInstance(dataCollector);// created in a different module
bind(RecordsCounterOperations.class).toInstance(recordsCounterOperations);//
created in a different module
install(new MpsLogicModule()); // This is the actual
module to be tested
}
};
Injector injector = Guice.createInjector(testModule);
MpsProcessor mpsProcessor1 =
injector.getInstance(MpsProcessor.class);
MpsProcessor mpsProcessor2 =
injector.getInstance(MpsProcessor.class);
assertNotNull(mpsProcessor1);
assertSame(mpsProcessor1, mpsProcessor2);//checking singleton
ManagerStrategy managerStrategy1 =
injector.getInstance(ManagerStrategy.class);
ManagerStrategy managerStrategy2 =
injector.getInstance(ManagerStrategy.class);
assertNotNull(managerStrategy1);
assertSame(managerStrategy1, managerStrategy2);
.... // more asserts here...
}
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.