Mathieu has posted 10 posts at DZone. View Full User Profile

Unit Testing with Spring, Guice, JMock, Easymock, Mockito...

02.04.2009
| 12902 views |
  • submit to reddit
Location: 
http://code.mycila.com/

When 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:

 

Mathieu.

0
Published at DZone with permission of its author, Mathieu Carbou.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)