From: Nat P. <nat...@b1...> - 2003-06-24 08:45:10
|
You can use the 'Or' Constraint to test for a containsKey call with either the keys. If used with expectAndReturn, the mock will expect *one* call to containsKey for *either* of those values. This will take into account short circuited evaluation. However, if you don't want your test to expose details of short circuited evaluation, you can set up two matchAndReturn calls for both keys. I would recommend this option because you are stubbing the behaviour of the map and testing the behaviour of your object in response to what is in the map. The exact sequence of calls that your object makes to examine the map are not important; only what the map returns to your object and what your object does in response. E.g. Using Or: String constantClass = constantName + ".class"; String constantValue = constantName + ".value"; mockMap.expectAndReturn( "containsKey", C.or(C.eq(constantClass),c.eq(constantValue)), false ); E.g. using matchAndReturn: mockMap.matchAndReturn( "containsKey", constantClass, false ); mockMap.matchAndReturn( "containsKey", constantValue, false ); Cheers, Nat. P.S. I would recommend refactoring your if statement: private boolean definesConstant( String name ) { return getConstantsMap().containsKey( name ); } ... String constantClass = constantName + CLASS_SUFFIX; String constantValue = constantName + VALUE_SUFFIX; if ( !definesConstant(constantClass) || !definesConstant(constantValue) ) { ... blah blah blah ... } else { ... blah blah blah ... } _______________________ Dr. Nathaniel Pryce B13media Ltd. http://www.b13media.com +44 (0)7712 526 661 ----- Original Message ----- From: "Francois Beausoleil" <fb...@us...> To: <moc...@li...> Sent: Tuesday, June 24, 2003 6:14 AM Subject: [MO-java-dev] How to test double conditional implementation, which could change... Hi ! I have a test which tests for the presence of two keys in a Map. If one or both keys are missing, do A, else B. So, here's what the code looks like at the moment: if (false == getConstantsMap().containsKey(constantName + CLASS_SUFFIX) || false == getConstantsMap().containsKey(constantName + VALUE_SUFFIX)) { No, Java is able to short-circuit the evaluation of a condition if the truth of a condition can be determined early. Right now, I am setting up my dynamock so: final Mock mockMap = new Mock(Map.class); mockMap.expectAndReturn("containsKey", C.eq(constantName + ".class"), false); mockMap.expectAndReturn("containsKey", C.eq(constantName + ".value"), true); mockMap.expect("put", C.args( C.eq(constantName + ".class"), C.eq(Integer.TYPE) ) ); My test fails because Java short circuits the evaluation of the second condition. I changed to using a matchAndReturn for the second containsKey, but this is an implementation detail. What would you guys recommend ? Should I use matchAndReturn for both containsKey() setups, or should I change the implementation to use temp variables and then test these ? Thanks, François -- Francois Beausoleil Developer of Java Gui Builder http://jgb.sourceforge.net/ ------------------------------------------------------- This SF.Net email is sponsored by: INetU Attention Web Developers & Consultants: Become An INetU Hosting Partner. Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission! INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php _______________________________________________ Mockobjects-java-dev mailing list Moc...@li... https://lists.sourceforge.net/lists/listinfo/mockobjects-java-dev |