MockObjects selection: Comparison of Easymock and JMOCK
This article assumes that the reader has already understood the purpose and basic ways of MockObjects, and does not explain too much techniques such as Mocktest. Only remind: "Don't test your MockObjects."
As a evaluation result, this article can also be used as a short tutorial for Easymock and JMOCK. They are very easy to use, but unfortunate examples are too complicated and have used too much mode. After reading the examples of this article, I believe that it can be used in the project.
MockObjects commonly used in Java have Easymock and JMOCK. The Easymock has been developed earlier, and it has already been released from 1.1, while JMock has just launched 1.0 final. As a mature little brother, what competition strength is JMOCK?
This comparison is for the following aspects, please see the attachment.
1 Can I simulate the specific class (of course, the interface simulation is the basic function)
2 Is it possible to dynamically control the method name, parameter, return value
3 basic code number
4 Is it possible to simulate a specific class analog to construct
It starts now. First make a number of test files, it is very simple. To simulate an interface and a concrete class called TheInterFaceTomock and THECLASSTOMOCK, in addition, the method SampleTurn Samplethod (Parameter P) is provided; and the same name has no parameter method.
The first test is for theInterFaceTomock, providing parameterImpl and SampleReturnImpl as the expected parameters and return values.
The JMOCK code is as follows:
public class JMockUsage extends MockObjectTestCase {public void testReturnValueWithParemeter () {// controller configured Mock Mock m = new Mock (TheInterfaceToMock.class); // This tests MockObject TheInterfaceToMock mock = (TheInterfaceToMock) m.proxy (); // The expected return value Samplereturn Sr = new SampleTurnImpl (); // expect parameter parameter p = new parameterImpl (); // controller, expectation, method SampleMethod, parameter is equal to P (Equals), will return SR M.EXPECTS OnCE ()). Method ("SampleMethod") .with (EQ (P)). Will (ReturnValue (SR)); // officially executed MockObject SampleTurn Ret = Mock.SampleMEthod (New parameterImpl ()); // Determine return Value is the same assertsame (sr, return);}}
The EASYMCOK code for the same function is as follows:
public class EasyMockUsage extends TestCase {public void testReturnValueWithParameter () {// mock controller configured MockControl control = MockControl.createControl (TheInterfaceToMock.class); // this is to be tested MockObject TheInterfaceToMock mock = (TheInterfaceToMock) control.getMock (); // This is the value of the value to return SampleReturn Sr = new SampleReturnImpl (); // This is the parameter P = new parameterimpland (); // Restore to record (//). First recording the SampleMethod method Mock.SampleMethod (P); // Set the return value of the method Control.SetRetReturnValue (SR); // Switching status is reply (reply) control.replay (); // officially executed Mock Object method Obviously, the parameter value is Equals instead of Same Samplereturn Ret = Mock.SampleMETHOD (New paraMpleTHOD (NEW PARAMEMPL ()); / / Determine the return value is required for the value assertsame (SR, RET);} From the above code, the same The function, the number of rows of the two is 3 lines. The main reason is that EasyMCOK's MOCK mechanism is based on a state. The first is a recording state, record the methods and parameters, return value, etc. to be tested, then switch to reply. And JMOCK did not switch this step, and the return value directly returned by the parameter was written directly. It is indeed a word: I look forward to once, the method SampleMethod, the parameters are equal to P (Equals), will return SR. Some of these auxiliary functions, such as ReturnValue, EQ, etc., located in the parent class MockTestCase.
in conclusion:
1 If you cannot provide MockTestCase as a parent class, use EasyMock 2 If you need a bulk or dynamic generation test, please use more rule of JMOCK 3 if you like to look less, please use JMOCK 4 if you want to switch your eyes, please With mock
The specific class test is performed below, and a common point is that both CGLIB uses CGLIB as the enhancer, so the efficiency difference is almost no. Slightly modify the above test, change TheInterFaceTomock to TheclasstOMOCK. The following changes have occurred.
With JMOCK, you need to replace Import to new import, other parts of the code are completely unchanged!
Original import org.jmock.mock; import org.jmock.mockOckObjectTestCase; change to: import org.jmock.cglib.mock; import org.jmock.cglib.mockObjectTestCase;
This is a considerable consideration, ensuring the consistency of the interface. For a set of APIs, the same class has a different way of use is a nightmare. With Easymock, you need to add an Import. And modify some of some statements.
Original import org.easymock.mockcontrol; increase import org.easymock.classextension.mockclasscontrol;
// mock controller MockControl Control = MockClassControl.createControl (TheclasstOMock.class);
Other parts don't need to change. Although this change, changes have brought other benefits, that is: can support specific classes with constructors, while JMOCK does not support. This is a gospel for a large number of code that uses PicoContainer.
in conclusion:
5 If you need to construct parameters, you can only use Easymock 6 if you like to use the same API operation and don't care about constructive parameters, please use JMOCK 7 if you want to wait for the next version of JMOCK to provide constructive support, please use JMOCK
Reference comparison table:
Easymock JMOCK is a control method for controlling the number of valid times is that the custom parameter match is whether it is not required to convert whether it is a specific class simulation is the specific class can have a configuration parameter whether or not the condition code is completed in a row. Parameter rules, such as NOT No Yes Verify () is
In summary, I chose JMOCK. Think about it, Easymock uses three classes to achieve most common functions, it is very straightforward. JMOCK, if it is able to provide support for constructor inJection. regret. However, from the design, the pattern in JMOCK is used as a model, very good.
I don't have much experience on EasyMock, please indicate if something is false.
download link:
http://www.jmock.org
http://www.easymock.org
Comparison code:
http://icecloud.51.net/data/mockObjects.zip
need:
JUnit3.8.1, CGLIB2, JMOCK1.0, Easymock1.1
Copyright Notice: This article is completed by ice cloud, the author retains Chinese copyright. No commercial use is not permitted without permission. Welcome to the reprint, but please keep the article and copyright statement complete. For contact, please send an email: Icecloud (at) sina.comblog: http: //icecloud.51.net