## 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:

- Empty constructor is first called
- If no empty constructor, first constructor of the class is called
- Default values are provided for primitive type arguments
- Nice mocks are provided for object and interface arguments
- A final class argument will be directly instantiated (not mocked)
- Mocked arguments will also need to be instantiated like the currently mocked class
- A class with only private constructors cannot be instantiated
- Code in the constructor will be executed and might crash. In this case the class is not mockable
- The ability to provide it's own arguments to instantiate the class is not supported anymore

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.