Documentation for release 2.2.2 (April 12 2007)
© 2003-2007 OFFIS, Henri Tremblay.
The EasyMock Class Extension allows to generate Mock Objects for classes.
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.
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:
DefaultClassInstantiator
which works well with Serializable classes
and otherwise tries to guess the best constructor and parameters to use.IClassInstantiator
.
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.
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.