We write a lot of unit tests every day in our development. Many unit tests are relatively independent, such as an income tax calculation module, however, more unit tests depend on other components or services. Therefore, we need easymock to seamlessly connect these components. Mock means nothing that does not exist.
Easymock has just released version 2.2, which uses many JDK 1.5-based features.
Here is an example:
Assume that the following interface is defined:
Public interface isimpsonservice {
Iepisode getepisode (INT number );
}
Public interface iepisode {
Int getnumber ();
String gettitle ();
Inputstream getdataasstream ();
}
The implementation of this interface is
public class ClientSimpsonService implements ISimpsonService { private ISimpsonService remoteSimpsonService; public ClientSimpsonService(ISimpsonService remoteSimpsonService) { this.remoteSimpsonService = remoteSimpsonService; } public IEpisode getEpisode(int episodeNumber) { return null; }}
The Unit Tests for this Service are as follows:
public class ClientSimpsonServiceTest extends TestCase { public void testClientSimpsonService() { try { new ClientSimpsonService(null); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected } }
We can see that there is no remotesimpsonservice, so we only pass a null value to clientsimpsonservice,
In this way, the test cannot be completed. Therefore, we replace the unwritten remotesimpsonservice with mock.
import static org.easymock.EasyMock.createMock;import junit.framework.TestCase;public class ClientSimpsonServiceTest extends TestCase { private ISimpsonService remoteSimpsonServiceMock; protected void setUp() throws Exception { super.setUp(); remoteSimpsonServiceMock = createMock(ISimpsonService.class); } public void testClientSimpsonService() { try { new ClientSimpsonService(null); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected } new ClientSimpsonService(remoteSimpsonServiceMock); }}
In the above simhei code, we createdIsimpsonservice. ClassIt owns andIsimpsonserviceSameOf
Interface.
In the same way, we can define the iepisode object returned by isimpsonservice.
import static org.easymock.EasyMock.createMock;import static org.easymock.EasyMock.expect;import static org.easymock.EasyMock.replay;import static org.easymock.EasyMock.verify;import junit.framework.TestCase;public class ClientSimpsonServiceTest extends TestCase { private IEpisode episode17Mock; private ISimpsonService remoteSimpsonServiceMock; protected void setUp() throws Exception { super.setUp(); episode17Mock = createMock(IEpisode.class); remoteSimpsonServiceMock = createMock(ISimpsonService.class); }... public void testGetEpisode() throws Exception { expect(remoteSimpsonServiceMock.getEpisode(17)) .andReturn(episode17Mock); replay(remoteSimpsonServiceMock); ISimpsonService clientSimpsonService = new ClientSimpsonService(remoteSimpsonServiceMock); IEpisode result = clientSimpsonService.getEpisode(17); verify(remoteSimpsonServiceMock); assertEquals(episode17Mock, result); }
For more information about easymock, see
http://today.java.net/pub/a/today/2006/06/20/getting-started-with-easymock-2.html