|
Mocking classes with Easymock
[
vmassol
]
Joel Shellman has coded an extension (easymock-patch-1.0.jar) of EasyMock which allows to mock class (and not only interfaces). To use it:
Here's an example. First let's start by the class we wish to mock:
Here's now the class we wish to test (it uses the Calculator class):
Now, here's our test of
Note: I don't know how to instantiate a constructor taking primitive types. Don't know if it is supported. That's nice. However, there are a few drawbacks I can see:
So it's not a silver bullet but it will certainly help in cases where you have no control over the sources (like when using third-party libraries. >>Note: I don't know how to instantiate a Yes it's supported. if you have this: use Constructor c = Xpto.class.getConstructor(new Class[]{Integer.TYPE});
Other way is to use jakarta's BeanUtils package. I think they have some util classes to handle that in a simple way. have fun. --Jorge Sousa, March 25, 2004 01:17 PM
I think the easymock-patch-1.0.jar is very good. --Yukitaka Kobayashi, October 15, 2004 07:38 AM
Am not able to download easymock-patch-1.0.jar.. could anyone please tell me where to download it from? --ajeesh, February 17, 2006 04:11 AM
ajeesh, what you want to look at is Mocquer. Mocquer is a similar mock tool as EasyMock. With the help of Dunamis Project, it extends the function of EasyMock to support mock object creation for both class and interface. --Ronny N, August 30, 2006 09:53 AM
easymock does not support for classes. --pankaj khare, January 9, 2008 10:24 AM
i am not understanding for this example because --pankaj khare, January 25, 2008 07:47 AM
Post a comment
|