EasyMock Class Extension 2.4 Readme

Documentation for release 2.4 (August 30 2008)
© 2003-2008 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.4, 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.


Partial mocking

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, 
   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. One exception: abstract methods are conveniently mocked by default.

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

Remark: EasyMock provides a default behavior for Object's methods (equals, hashCode, toString). However, for a partial mock, if these methods are not mocked explicitly, they will have their normal behavior instead of EasyMock default's one.

Self testing

It is possible to create a mock by calling one of its constructor. This can be handy when a class method needs to be tested but the class other methods, mocked. For that you call a createMock() method with a ConstructorArgs. See the ConstructorCalledMockTest for an example.

Replace default instantiator

For some reason (usually an unsupported JVM), it is possible that EasyMock CE isn't able to create a mock in your environment. Under the hood, class instantiation is implemented with a factory pattern. In case of failure, you can replace the default instantiator with:

You set this new instantiator using ClassInstantiatorFactory.setInstantiator(). You can set back the default one with setDefaultInstantiator().

Important: The instantiator is kept statically so it will stick between your unit tests. Make sure you reset it if needed.


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 to be 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.

Private methods cannot be mocked. If called, their normal code will be executed. Remember this can occur during partial mocking.

Class instantiation is performed using Objenesis. Supported JVMs are listed here.

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 is currently maintained by Henri Tremblay.

It was initially developed by Joel Shellman, Chad Woolley and Henri Tremblay on the files section of Yahoo!Groups.

Release Notes

New in version 2.4:

New in version 2.3:

New in version 2.2.2:

New in version 2.2.1:

New in version 2.1:

New in version 2.0: