???????????????????????????????α??????????????漲????????÷????????????????塣??????????????????????????????????????????磬??????????Foo??
????//Foo.java     public class Foo {       public void dummy() throw ParseException {         ...       }       public String bar(int i) {         ...       }       public boolean isSame(String[] strs) {         ...       }       public void add(StringBuffer sb?? String s) {         ...       }     }α???????????????????????????壺
????//get mock control     MockControl control = MockControl.createControl(Foo.class);     //get mock object     Foo foo = (Foo)control.getMock();     //begin behavior definition     //specify which method invocation's behavior     //to be defined.     foo.bar(10);     //define the behavior -- return "ok" when the     //argument is 10     control.setReturnValue("ok");     ...     //end behavior definition     control.replay();     ...MockControl?г???50??????????????巽??????????????·???
????o        setReturnValue()
??????Щ?????????????????????????÷?????????????????????7????????????????????`setReturnValue()????????setReturnValue(int i)??setReturnValue(float f)??setReturnValue(Object obj)????????????Щ???????????????????????????????????????????????????AssertionFailedError????
??????????????????м???????????????????????????????
????MockControl control = ...       Foo foo = (Foo)control.getMock();       ...       foo.bar(10);       //define the behavior -- return "ok" when the       //argument is 10. And this method is expected       //to be called just once.       setReturnValue("ok"?? 1);       ... ????????ζ?????bar(10)??????????????Ρ???????????Χ???????????
????...       foo.bar(10);       //define the behavior -- return "ok" when the       //argument is 10. And this method is expected       //to be called at least once and at most 3       //times.       setReturnValue("ok"?? 1?? 3);       ...????bar(10)?????????????????ζ?3?Ρ??????????Range???????????Щ?????Χ??
????...       foo.bar(10);       //define the behavior -- return "ok" when the       //argument is 10. And this method is expected       //to be called at least once.       setReturnValue("ok"?? Range.ONE_OR_MORE);       ...Range.ONE_OR_MORE???????????Range?????????ζ???????????????????Ρ????setReturnValue()????ж????????????????setReturnValue("Hello")??Range.ONE_OR_MORE??????????????????????????Range?????Range.ONE????Σ???Range.ZERO_OR_MORE????????????????????
??????????????????????÷???????????setDefaultReturnValue()?????????淽?????????????????????????????????Range.ONE_OR_MORE??????????????????????
????...       foo.bar(10);       //define the behavior -- return "ok" when calling       //bar(int) despite the argument value.       setDefaultReturnValue("ok");       ...o        setThrowable
????setThrowable(Throwable throwable)?????????巽???????????????????????????throwable??????????????壬??AssertionFailedError??????????????????????????????????????μ??
????...       try {         foo.dummy();       } catch (Exception e) {         //skip       }       //define the behavior -- throw ParseException       //when call dummy(). And this method is expected       //to be called exactly once.       control.setThrowable(new ParseException(""?? 0)?? 1);       ...o        setVoidCallable()
????setVoidCallable()????????з??????????????????????????????????????????μ??
????...       try {         foo.dummy();       } catch (Exception e) {         //skip       }       //define the behavior -- no return value       //when calling dummy(). And this method is expected       //to be called at least once.       control.setVoidCallable();       ...o        Set ArgumentsMatcher
?????????????MockControl????α????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????setMatcher()???????ArgumentsMatcher?????????á?
????public interface ArgumentsMatcher {         public boolean matches(Object[] expected??                                Object[] actual);       }ArgumentsMatcher?????matches()????????????????????????????????饗????????????????????NULL?????????????????????顣??????????????檔
????...       foo.isSame(null);       //set the argument match rule -- always match       //no matter what parameter is given       control.setMatcher(MockControl.ALWAYS_MATCHER);       //define the behavior -- return true when call       //isSame(). And this method is expected       //to be called at least once.       control.setReturnValue(true?? 1);       ...MockControl??????????????ArgumentsMatcher?????MockControl.ALWAYS_MATCHER?????????????????????????????MockControl.EQUALS_MATCHER???????????????????????equals()??????MockControl.ARRAY_MATCHER??MockControl.EQUALS_MATCHER??????£??????????????Arrays.equals()??????????????????????????ArgumentsMatcher??
?????????????ArgumentsMatcher???????????????????巽?????????????????
????...       //just to demonstrate the function       //of out parameter value definition       foo.add(new String[]{null?? null});       //set the argument match rule -- always       //match no matter what parameter given.       //Also defined the value of out param.       control.setMatcher(new ArgumentsMatcher() {         public boolean matches(Object[] expected??                                Object[] actual) {            ((StringBuffer)actual[0])                               .append(actual[1]);            return true;         }       });       //define the behavior of add().       //This method is expected to be called at       //least once.       control.setVoidCallable(true?? 1);       ...setDefaultMatcher()????MockControl????ArgumentsMatcher????????????????ArgumentsMatcher??????ArgumentsMatcher????á??????????????κη????????????????????á??????????AssertionFailedError????
????//get mock control       MockControl control = ...;       //get mock object       Foo foo = (Foo)control.getMock();       //set default ArgumentsMatcher       control.setDefaultMatcher(                      MockControl.ALWAYS_MATCHER);       //begin behavior definition       foo.bar(10);       control.setReturnValue("ok");       ...?????????setDefaultMatcher()??MockControl.ARRAY_MATCHER??????ArgumentsMatcher??
???????????
??????????????????????????Mocquer?÷????????????????????FTPConnector??
????package org.jingle.mocquer.sample; import java.io.IOException; import java.net.SocketException; import org.apache.commons.net.ftp.FTPClient; public class FTPConnector {     //ftp server host name     String hostName;     //ftp server port number     int port;     //user name     String user;     //password     String pass;     public FTPConnector(String hostName??                         int port??                         String user??                         String pass) {         this.hostName = hostName;         this.port = port;         this.user = user;         this.pass = pass;     }     /**      * Connect to the ftp server.      * The max retry times is 3.      * @return true if suclearcase/" target="_blank" >cceed      */     public boolean connect() {         boolean ret = false;         FTPClient ftp = getFTPClient();         int times = 1;         while ((times <= 3) && !ret) {             try {                 ftp.connect(hostName?? port);                 ret = ftp.login(user?? pass);             } catch (SocketException e) {             } catch (IOException e) {             } finally {                 times++;             }         }         return ret;     }     /**      * get the FTPClient instance      * It seems that this method is a nonsense      * at first glance. Actually?? this method      * is very important for unit test using      * mock technology.      * @return FTPClient instance      */     protected FTPClient getFTPClient() {         return new FTPClient();     } }connect()????????????FTP???????????????????????????????????Ρ????????????????檔????????????????org.apache.commons.net.FTPClient?????????????????????????????????????????????????getFTPClient()???????????????????α??????????????????????????????????
???????JUnit???????FTPConnectorTest??????????connect()????????????????????????????????????????????У?????FTP???????????????????????????Mocquer?????FTPClient??
????package org.jingle.mocquer.sample; import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.jingle.mocquer.MockControl; import junit.framework.TestCase; public class FTPConnectorTest extends TestCase {     /*      * @see TestCase#setUp()      */     protected void setUp() throws Exception {         super.setUp();     }     /*      * @see TestCase#tearDown()      */     protected void tearDown() throws Exception {         super.tearDown();     }     /**      * test FTPConnector.connect()      */     public final void testConnect() {         //get strict mock control         MockControl control =              MockControl.createStrictControl(                                 FTPClient.class);         //get mock object         //why final? try to remove it         final FTPClient ftp =                     (FTPClient)control.getMock();         //Test point 1         //begin behavior definition         try {             //specify the method invocation             ftp.connect("202.96.69.8"?? 7010);             //specify the behavior             //throw IOException when call             //connect() with parameters             //"202.96.69.8" and 7010. This method             //should be called exactly three times             control.setThrowable(                             new IOException()?? 3);             //change to working state             control.replay();         } catch (Exception e) {             fail("Unexpected exception: " + e);         }         //prepare the instance         //the overridden method is the bridge to         //introduce the mock object.         FTPConnector inst = new FTPConnector(                                   "202.96.69.8"??                                   7010??                                   "user"??                                   "pass") {             protected FTPClient getFTPClient() {                 //do you understand why declare                 //the ftp variable as final now?                 return ftp;             }         };         //in this case?? the connect() should         //return false         assertFalse(inst.connect());         //change to checking state         control.verify();         //Test point 2         try {             //return to preparing state first             control.reset();             //behavior definition             ftp.connect("202.96.69.8"?? 7010);             control.setThrowable(                            new IOException()?? 2);             ftp.connect("202.96.69.8"?? 7010);             control.setVoidCallable(1);             ftp.login("user"?? "pass");             control.setReturnValue(true?? 1);             control.replay();         } catch (Exception e) {             fail("Unexpected exception: " + e);         }         //in this case?? the connect() should         //return true         assertTrue(inst.connect());         //verify again         control.verify();     } }???????????????MockObject??α????????????final???η????????????????????????????????в??????????
?????????????????а????????????????????????FTPClient.connect()??????????????FTPClient.connect()??????
????try {     ftp.connect("202.96.69.8"?? 7010);     control.setThrowable(new IOException()?? 3);     control.replay(); } catch (Exception e) {     fail("Unexpected exception: " + e); }MockControl?????α????connect()???????????202.96.96.8????????????7010?????????????IOException????????????????????????Ρ???????????replay()???α???????????????????try/catch???????FTPClient.connect()????壬??????????????IOException????
????FTPConnector inst = new FTPConnector("202.96.69.8"??                                      7010??                                      "user"??                                      "pass") {     protected FTPClient getFTPClient() {         return ftp;     } };???????????????д??getFTPClient()??????FTPConnector???????????????????α?????????????????
????assertFalse(inst.connect());
?????????????connect()??÷?????
????control.verify();
?????????α??????????
?????????????????????FTPClient.connect()?????????????????λ????????FTPClient.login()???????????????ζ??FTPConnector.connect()?????檔
???????????????????????????????У?????????MockObject???????reset()?????????????
???????
???????????????????????????????з??????????JUnit????м????????????????????????????EasyMock????????α????????????????????α??????DunamisЭ???£?Mocquer?????EasyMock?????????????????α??????????????????Mocquer?????????е???á?