jMock vs. Easymock - syntax comparison

Just a little comparison of jMock (ver 2.1.0) and EasyMock (ver. 2.2) libraries.

AFAIK you can achieve similar results with both these libraries. The question is which one makes your tests more readable ? For me the simplicity of test classes is a top priority. I believe that test classes should be VERY easy to understand.

[comment added 2008-10-19] For a comparison of features of JMock and EasyMock you should try text written by Jean Tessier "Mocking in Java: jMock vs. EasyMock".

Let's take a tiny fragment of some service into consideration. It looks like this:

public class ClientManagerImpl {
    private ClientDao clientDao;

    public boolean clientWithSamePeselExists( Long pesel ) {
        Client client = clientDao.getClientByPesel( pesel );
        return ( client != null );
    }
}

The idea behind this small snippet is to check if there is a client with the same PESEL (which is polish ID number) in the database. And here comes the mocks - we can't connect to database, but we need to pretend we do. We need to mock clientDao and see what this function does depending on return value from clientDao object.

Below are simple jUnit tests written in both libraries. I've tried to write them as simple as it's possible. They don't cover all the branches of this snippet (they don't check what happens if database returns null), but it's enough for this simple syntax comparison.

jMock

public class ClientManagerImplTest
    extends TestCase {

    private final static Long PESEL = 123213L;

    private ClientManagerImpl clientManager;

    private ClientDao clientDao;

    private Mockery mockery;

    @Override
    protected void setUp()
        throws Exception {
        super.setUp();
        mockery = new Mockery();
        clientDao = mockery.mock( ClientDao.class );
        clientManager = new ClientManagerImpl( clientDao );
    }

    public void testPeselFound() {
        mockery.checking( new Expectations(){
            {
                allowing( clientDao ).getClientByPesel( PESEL );
                will( returnValue( new Client() ) );
            }
        } );
        assertTrue( clientManager.clientWithSamePeselExists( PESEL ) );
        mockery.assertIsSatisfied();
    }
}

EasyMock

public class ClientManagerImplEasymockTest
    extends TestCase {

    private final static Long PESEL = 123213L;

    private ClientManagerImpl clientManager;

    private ClientDao clientDao;

    @Override
    protected void setUp()
        throws Exception {
        super.setUp();
        clientDao = (ClientDao) createMock( ClientDao.class );
        clientManager = new ClientManagerImpl( clientDao );
    }

    public void testPeselFound() {
        expect( clientDao.getClientByPesel( PESEL ) ).andReturn( new Client() );
        replay( clientDao );
        assertTrue( clientManager.clientWithSamePeselExists( PESEL ) );
        verify( clientDao );
    }

}

And the winner is...

EasyMock !

For me this:

expect( clientDao.getClientByPesel( PESEL ) ).andReturn( new Client() );

is much more readable than this:

mockery.checking( new Expectations(){
  {
    allowing( clientDao ).getClientByPesel( PESEL );
    will( returnValue( new Client() ) );
  }
} );

I don't say that EasyMock is superior. I only find it's syntax easier to understand. Because both frameworks allow me to do what I want to do, I'll keep on using this library.

Hello, I completely agree. I

Hello,

I completely agree. I first tried JMock and liked it, but since I saw what EasyMock provides, I won't go back to JMock. The tests are way more readable using EasyMock.

AGREEMENT

Hi, nice post. I have been wondering about this topic,so thanks for sharing. I will certainly be subscribing to your blog.

 
 
 
This used to be my blog. I moved to http://tomek.kaczanowscy.pl long time ago.

 
 
 

Please comment using