本文是在jmock的网站上发现的,很有实际意义,因为一直用easymock,试了一下jmock,觉得很别扭,方法名以字符串的方式自己输入,容易写错,而且还要继承它自己的基类,不爽。
所以本文的程序样例用easymock重写了。
在下面的例子中,Guard持有一个Alarm的引用,在必要的时候进行报警。
<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard
[email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start source code -->
publicinterfaceAlarm{ publicvoidring(); } | <!-- end source code -->
<!-- = END of automatically generated HTML code = --><!-- ======================================================== --><!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard
[email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start source code -->
publicclassGuard{ privateAlarmalarm;
publicGuard(Alarmalarm){ this.alarm=alarm; }
publicvoidgetBored(){ startRingingTheAlarm(); }
privatevoidstartRingingTheAlarm(){ RunnableringAlarmTask=newRunnable(){ publicvoidrun(){ alarm.ring(); } }; ThreadringAlarmThread=newThread(ringAlarmTask); ringAlarmThread.start(); } } | <!-- end source code -->
Guard.getBored()çæµè¯ä»£ç å¦ä¸ï¼
<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard
[email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start source code -->
publicvoidtestGuardDoesNotRingTheAlarmWhenHeGetsBored(){ Alarmalarm=EasyMock.createMock(Alarm.class); Guardguard=newGuard(alarm); guard.getBored(); } | <!-- end source code -->
å¨æ¤ä¾ä¸ï¼é¢æçå¼å¸¸å¹¶æ²¡æåçï¼æµè¯éè¿äºãè¿æ¯å 为alarmæåºçå¼å¸¸æ¯å¨ringAlarm线ç¨ä¸ï¼èä¸æ¯å¨æµè¯ä¸»çº¿ç¨ä¸ãæ¤é®é¢çæ ¹æºæ¯è¯å¾ä½¿ç¨mock objectæ¥è¿è¡éææµè¯ãç¨mock objectæ¥è¿è¡åå
æµè¯æ¯å¸æå°æµè¯çåå
ä¸ç³»ç»å
¶ä»åå
ç¸é离ãç¶èï¼çº¿ç¨ä»å
¶ç¹æ§æ¥è¯´ï¼æ¯å±äºéææµè¯çèç´ã并åååæ¥é½è¦æ¶åå°å
¨å±èå´ï¼çº¿ç¨çå建ä¹ç¨å°äºæä½ç³»ç»åºå±çç¹æ§ã
ä¸ç§è§£å³æ¹æ¡æ¯å°è¦æ§è¡ä»»å¡ç对象ä¸ä»»å¡çç»èç¸é离ï¼å¨å®ä»¬ä¹é´å¼å
¥ä¸ä¸ªæ¥å£ãè¿æ ·ï¼å¯ä»¥ç¨mock objectæ¥æµè¯è¦æ§è¡ä»»å¡ç对象ï¼å¨éææµè¯ä¸æµè¯ä»»å¡çæ§è¡ã
<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard
[email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start source code -->
publicinterfaceTaskRunner{ publicvoidstart(Runnabletask); } | <!-- end source code -->
æµè¯æ¹æ¡å¦ä¸ï¼
<!-- = END of automatically generated HTML code = --><!-- ======================================================== --><!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard
[email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start source code -->
publicvoidtestGuardDoesNotRingTheAlarmWhenHeGetsBored(){ Alarmalarm=EasyMock.createMock(Alarm.class); TaskRunnertaskRunner=newImmediateTaskRunner(); Guardguard=newGuard(alarm,taskRunner); guard.getBored(); } | <!-- end source code -->
<!-- = END of automatically generated HTML code = --><!-- ======================================================== --> å¨TaskRunnerçå®ç°ä¸ï¼å¦ææ¯å¯ç¨ä¸ä¸ªæ°çº¿ç¨æ¥æ§è¡ä»»å¡ï¼é£ä¹ååå°äºé®é¢çå¼å§ï¼æµè¯è¿æ¯ä¸è½å¾å°å¸æçå¼å¸¸ãæ们éè¦å°ä»»å¡çæ§è¡æ¾å¨TaskRunnerç¸åç线ç¨ä¸ãæç®åçæ¹æ³å°±æ¯ç«å³æ§è¡è¯¥ä»»å¡ï¼èä¸æ¯å¯çº¿ç¨æ¥æ§è¡ãå¨åå
æµè¯ä¸ï¼å¯ä»¥ç´æ¥å®ç°TaskRunneræ¥å£ï¼å¾å°å¦ä¸çä»»å¡æ§è¡å¨ã
<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard
[email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start source code -->
publicclassImmediateTaskRunnerimplementsTaskRunner{ publicvoidstart(Runnabletask){ task.run(); } } | <!-- end source code -->
<!-- = END of automatically generated HTML code = --><!-- ======================================================== -->
Guard代ç æ´æ°å¦ä¸ï¼
<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard
[email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start source code -->
publicclassGuard{ privateAlarmalarm;
privateTaskRunnertaskRunner;
publicGuard(Alarmalarm,TaskRunnertaskRunner){ this.alarm=alarm; this.taskRunner=taskRunner; }
publicvoidgetBored(){ startRingingTheAlarm(); }
privatevoidstartRingingTheAlarm(){ RunnableringAlarmTask=newRunnable(){ publicvoidrun(){ alarm.ring(); } }; taskRunner.start(ringAlarmTask); } } | <!-- end source code -->
å¨å®é
项ç®ä¸ä½¿ç¨çTaskRunner
<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard
[email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start source code -->
publicclassConcurrentTaskRunnerimplementsTaskRunner{ publicvoidstart(Runnabletask){ (newThread(task)).start(); } } | <!-- end source code -->
<!-- = END of automatically generated HTML code = --><!-- ======================================================== --> å¦ä¸ç§æ¹æ¡æ¯å¨Guard.getBored()æ§è¡ç»æåï¼å¨æµè¯æå¨ç线ç¨ä¸æ§è¡ä»»å¡ãå¦æGuardä¸çtry/finally æ©çäºä»»å¡å¼èµ·çæµè¯é误ï¼åºç¨æ¤æ¹æ¡åç¹å«éåã
å®ç°çTaskRunnerå¦ä¸ï¼<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--> <style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard
[email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start source code -->
publicclassDelayedTaskRunnerimplementsTaskRunner{ privateList<Runnable>delayedTasks=newArrayList<Runnable>();
publicvoidstart(Runnabletask){ delayedTasks.add(task); }
publicvoidrunTasks(){ for(Iterator<Runnable>i=delayedTasks.iterator();i.hasNext();){ i.next().run(); i.remove(); } } } | <!-- end source code -->
<!-- = END of automatically generated HTML code = --><!-- ======================================================== -->对åºçæµè¯ä»£ç 为ï¼<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--> <style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard
[email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start source code -->
publicvoidtestGuardDoesNotRingTheAlarmWhenHeGetsBored(){ Alarmalarm=EasyMock.createMock(Alarm.class); DelayedTaskRunnertaskRunner=newDelayedTaskRunner(); Guardguard=newGuard(alarm,taskRunner); guard.getBored(); taskRunner.runTasks(); } | <!-- end source code -->
<!-- = END of automatically generated HTML code = --><!-- ======================================================== --> å°å¯¹è±¡ä¸è¿è¡å¤çº¿ç¨ä»»å¡çæºå¶æååºæ¥ï¼ä¸ä»
æ¹ä¾¿åå
æµè¯ï¼èä¸è¿è½ä½¿å¾ç¨åºä¹é´çè¦åæ´æ¾ï¼æ©å±æ§æ´å¥½ãæ¯å¦å¯ä»¥æ¯«ä¸è´¹åçå°ç°å¨ç并åä»»å¡å¤çå¨æ¿æ¢æ线ç¨æ± ã