Journal of JMOCK

xiaoxiao2021-03-18  215

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);

}

}

转载请注明原文地址:https://www.9cbs.com/read-129911.html

New Post(0)