EasyMock Class Extension 2.2 Readme

Documentation for release 2.2 (April 17 2006)
© 2003-2006 OFFIS, Henri Tremblay.

The EasyMock Class Extension allows to generate Mock Objects for classes.


The EasyMock Class Extension requires Java 5 or above, EasyMock 2.1, cglib 2.1 (2.0.x also works but cannot mock a class without visible constructor), asm (use the version that matches your cglib version, you can also use cglib-nodep-2.1.jar which includes asm) and JUnit 4 to run the tests.


To generate Mock Objects for classes and interfaces, use
import static org.easymock.classextension.EasyMock.*;
instead of
import static org.easymock.EasyMock.*;
You will then create a mock just like with EasyMock by doing something like this:
ToMock mock = createMock(ToMock.class);
From now on, you will use your mock just like you use to do with EasyMock. Note that createNiceMock, createStrictMock, createStrictControl, createControl and createNiceControl methods are also available. The create...Control() methods return org.easymock.classextension.IMocksControl, an extension of EasyMock's IMocksControl.


Sometimes you may need to mock only some methods of a class and keep the normal behavior of others. This usually happens when you want to test a method that calls some others in the same class. So you want to keep the normal behavior of the tested method and mock the others.

In this case, the first thing to do is to consider a refactoring since most of the time this problem caused by a bad design. If it's not the case or if you can't do otherwise because of some development constraints, here's the solution.

ToMock mock = createMock(ToMock.class, 
   new Method[] { ToMock.class.getMethod("mockedMethod", null) } );

In this case only the methods passed to createMock will be mocked (mockedMethod() in the example). The others will still behave as they used to.

The class extension's IMocksControl also provides a createMock() method to create partial mocks.


EasyMock Class Extension provides a built-in behavior for equals(), toString() and hashCode(). It means that you cannot record your own behavior for these methods. It is coherent with what EasyMock do. This limitation is considered as a feature that prevents you from having to care about these methods.

Final methods cannot be mocked. If called, their normal code will be executed.

Currently, only Sun JVM 5.0 is really well supported. By that I mean that you can mock any kind of classes. No constructor will be called to create the mock.

In the Advanced section, it was explained how to do partial mocking. One important thing is that private methods are never mocked. So if your method under test is calling some private methods, you will need to test them as well since you cannot mock them.


The EasyMock Class Extension has been developed by Joel Shellman, Henri Tremblay, and Chad Woolley on the files section of Yahoo!Groups. It is maintained and improved by Henri Tremblay.