Because trying to develop TDD, you must write TestCase, of course, it is impossible to use Mock Object. In order to learn to write Mock Object, write information on a ferry and a dog, and found two Mock Library projects, one is Easymock, one is JMOCK. No time to look, find a comparative article "MockObjects selection: Easymock and JMOCK comparison" http://www.landiy.net/ttzy/study/jsp/jspBase/20040825193929.html finally with the author, selected JMOCK. The shortcoming of JMOCK is that the specific classes can be constructed, but I use Spring, not Pico, so the requirements are not high, while other functions or JMOCK is all, or decide to use it. The most basic methods are as follows: import org.jmock.mock; import org.jmock.mockObjecttecasepublic class jmockusage extends MockObjectTestCase {
Public void testReturnValuewithParemeter () {
//
structure
Mock
Controller
Mock M = New Mock (TheInterfaceTomock.class);
//
This is to test
MockObject
TheinterfaceTomock Mock = (theInterfaceTomock) m.Proxy ();
//
Looking forward to the return value
Samplereturn SR = new SampleReturnImpl ();
//
Expected parameters
Parameter P = New parameterImpl ();
//
Controller, look forward to once, 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
Samplereturn Ret = Mock.SampleMethod (New parameterImpl ());
//
Determine the return value is the same
Assertsame (SR, RET);
}
}