Unit Testing with Spring, Guice, JMock, Easymock, Mockito...
Join the DZone community and get the full member experience.
Join For FreeWhen you write unit tests, as the project grows to end up by using many technologies such as Jmock (or another mock framework like Mockito), DbUnit, Selenium, your favorite IOC, ... Integration of these technologies requires you to write pleinty of time the same code to setup a context, prepare your test, inject, ...
Mycila Testing Framework is an integration library that take care of all this code so that the developer can focuse on his tests. It currently has plugins for Guice 1 & 2, Spring, JMock, EasyMock, Mockito, AtUnit and we plan to add support for DbUnit, JGroups, JMS, JMX, Selenium, and others ...
Existing plugins are based on annotations and enables you to
- quickly create mocks,
- create bindings and bean definitions
- add them to the application context (or injector for Guice), supporting overriding of existing beans,
- inject services, ...
Mycila is based on a plugin architecture. Writing new plugins is trivial: anyone can contribute and write its own plugin and use it directly just by putting a jar on the claspath. The library supports any testing framework thnaks to its design (tested on TestNG and Junit). It works by enhancing your test from the plugins discovered on the classpath. As a result, you end up with a concise test.
Here is an example of unit test, using Guice 2 and Mockito:
@GuiceContext(ClusterModule.class)
public final class ClusterNodeListenerTest extends AbstractMycilaTestNGTest {
@Inject
ClusterFactory factory;
@Mock
ClusterNodeListener listener;
@Mock
@Bind(scope = Singleton.class)
ClusterService service;
@Test
public void test_ClusterNodeListener() throws Exception {
ClusterState agent1 = factory.createClusterState("states");
agent1.addListener(listener);
ClusterState agent2 = factory.createClusterState("testatoo-states");
Thread.sleep(4000);
agent2.close();
Thread.sleep(4000);
agent1.close();
verify(listener, times(1)).onJoin(any(UUID.class));
verify(listener, times(1)).onLeave(any(UUID.class));
}
}
The project is located at http://code.mycila.com/wiki/MycilaTesting.
Feel free to post your comment, issues, contributions :)
Also have a look at other useful projects we develop:
- Maven License Plugin: A Maven 2 plugin to manage your license headers in source files: http://code.google.com/p/maven-license-plugin/
- XML Tool: Create, parse and navigate in XML document through a very intuitive and simple API using Fluent Interface: http://code.google.com/p/xmltool/
- Mycila Plugin Framework: A simple but powerful plugin framework to add extensiblity into your application: http://code.mycila.com/wiki/MycilaPlugin
- Maven Deployer GUI: A Graphic tool to help deploying Maven Artifact: http://code.mycila.com/wiki/MavenDeployer
Mathieu.
Opinions expressed by DZone contributors are their own.
Comments