Use easymock in Unit Testing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.