Testing my module

139 views
Skip to first unread message

egolan

unread,
Jan 29, 2012, 4:59:34 PM1/29/12
to google-guice
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...
}

Sam Berlin

unread,
Jan 29, 2012, 6:41:27 PM1/29/12
to google...@googlegroups.com
What's your ultimate goal in testing the module?

Modules should have no logic in them, and ideally should require no specific tests.  All a Module does is setup your object graph, so the best "test" of your app's whole Module is with integration tests of your app.  If you want to ensure a subcomponent's Module is wiring things up correctly, then again I'd suggest the best thing is an integration test of that subcomponent.

sam


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


Eyal Golan

unread,
Jan 30, 2012, 2:15:21 AM1/30/12
to google...@googlegroups.com
Hi,
We do have integration tests that we will use the module (I am now in the process of migrating DI by hand to DI by Guice).
None of my modules have logic because as you mentioned they are just for wiring things up.

When I test the module I verify that I didn't forget to put Singleton in the correct classes.
I also verify that providers return different instance etc.

And something a little bit trivial is code coverage.
As long as I didn't hook the module to the integration tests, I got less coverage with the tests.

Also, as I only started using Guice, I felt that testing it made me more confident.

Thanks,

Eyal


Eyal Golan
egol...@gmail.com

Visit: http://jvdrums.sourceforge.net/
LinkedIn: http://www.linkedin.com/in/egolan74
Skype: egolan74

P  Save a tree. Please don't print this e-mail unless it's really necessary
Reply all
Reply to author
Forward
0 new messages