From: Francois B. <fb...@us...> - 2003-09-12 02:22:46
|
Hello Veny, Here is a bit of information for you: expectAndReturn() Expect a call to the named method using the specified argument or arguments, and return the specified value. It is possible to also throw an Exception instead by using expectAndThrow(). As you have guessed, expectAndReturn() will fail the verification if the method has not been called. matchAndReturn() Stub out a method call. This would be useful in the case where you will be calling a method, but do not care if the method is called once, twice or no time at all. ConstraintMatcher It is possible to specify constraints on the parameters that are received. Let's say you are mocking a List. Lists have a method named Object get(int index). It is possible to ask the expectAndReturn method to assert that the method is called with an appropriate parameter. An example will help: Mock mockList =3D new Mock(List.class); List list =3D (List)mockList.proxy(); mockList.expectAndReturn("get", C.eq(3), "returned object"); // This is where the domain methods call upon the mock classUnderTest.method(list); mockList.verify(); Given the above, the following implementation will fail: public void method(List list) { int index =3D 4; this.object =3D list.get(index); } It will fail because the expectAndReturn() method expected a single parameter, and it must have been equal to 3 (C.eq stands for ConstraintMatcher Shortcuts.equals(3)). Given the above too, this implementation will fail the verify() stage: public void method(List list) { } The Mock expected the get() method to be called with 3 as an argument, but did not receive it. Finally, if you overload your methods, the same principles as Java apply: interface User { addBilling(double x); addBilling(long x); } Mock mockUser =3D new Mock(User.class); User user =3D (User)mockUser.proxy(); user.expect("addBilling", C.eq(new Double(13.2d))); user.expect("addBilling", C.eq(new Long(17l))); classUnderTest.do(user); public void do(User user) { user.addBilling(17); // Passes. Java calls addBilling(long) user.addBilling(13.2f); // Passes. Java calls addBilling(double) } As for your last question, if the method that you expect a call upon accepts a single parameter, and you want to assert equality, Mock implementations allow a shortcut. Instead of this: user.expect("addBilling", C.eq(new Double(13.2d))); You can do this instead: user.expect("addBilling", new Double(13.2d)); Notice that the C.eq() was removed. We are expecting equality, and this will assert it just fine. For documentation, you can try http://www.mockobjects.org/. This will bring you to the MockObjects Wiki. There is information on the wiki about the framework. Not everything is documented, but a good bit of it is available. Have a nice day ! Fran=E7ois On Fri, 12 Sep 2003 09:39:18 +0800, "veny" <ve...@p2...> said: > Hi all, >=20 > I am new to mockobject and looking through the DynaMock class. > Could somebody give me some idea, what does the following methods in > com.mockobjects.dynamic.Mock class do? >=20 > expectAndReturn(java.lang.String methodName, ConstraintMatcher args, > java.lang.Object result) > expectAndReturn(java.lang.String methodName, java.lang.Object > singleEqualArg, boolean result) >=20 > I understand the above methods is setting the return value for the named > method. But some questions: > - How does ConstraintMatcher works? > - For the second method, what is the use of parameter singleEqualArg? > - These methods are name expectAndReturn, does it implies that if the > named > method is not called, it will throw an error? - How bout if the codes > have > methods overloading, how does expectAndReturn know which method i am > referring to? >=20 > matchAndReturn(java.lang.String methodName, java.lang.Object result) > How is this different from expectAndReturn? >=20 > The javadoc has rather limited explanation, any recommendation to sites > that > have sample or more detailed explanation? >=20 > Thanks lots. >=20 > Veny Developer of Java Gui Builder http://jgb.sourceforge.net/ |