EasyMock Class Extension 1.2.1 Readme

Documentation for release 1.2.1 (March 21 2007)
© 2003-2007 OFFIS.

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

Requirements

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

Usage

To generate Mock Objects for classes and interfaces, import
org.easymock.classextension.MockClassControl
instead of
org.easymock.MockControl.

You will then create a mock by doing something like this:

MockControl ctrl = MockClassControl.createControl(ToMock.class);
ToMock mock = (ToMock) ctrl.getMock();

From now on, you will use your mock just like you use to do with EasyMock. Note that createNiceControl and createStrictControl methods are also available.

Advanced

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 occurs because of a bad design. If it's not the case of if you can't do otherwise because of some development constraints, here's the solution.

MockControl ctrl = MockClassControl.createControl(ToMock.class, 
   new Method[] { ToMock.class.getDeclaredMethod("mockedMethod", null) } );

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

Supported JVMs

Mocks are created without any constructor being called. This is done using a dedicated library called Objenesis. It basically supports any 1.3+ JVM implementation. For more details, check their website.

If Objenesis fails to create the mock a fallback method will be used and a constructor needs to be called on the mocked class. Here's how it works:

There is one exception to the previous rule. If the class is Serializable no constructor will be called.

Limitations

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.

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.

EasyMock Class Extension Development

EasyMock CE is maintained by Henri Tremblay

Acknowledments

Joel Shellman and Chad Woolley for the initial version of the class extension on the files section of Yahoo!Groups.