﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-Baoquan Inside</title><link>http://www.blogjava.net/BaoquanInside/</link><description>研究java、j2ee和Lucene</description><language>zh-cn</language><lastBuildDate>Sat, 04 Apr 2026 02:37:31 GMT</lastBuildDate><pubDate>Sat, 04 Apr 2026 02:37:31 GMT</pubDate><ttl>60</ttl><item><title>测试驱动开发案例之自动售货机（第1集）</title><link>http://www.blogjava.net/BaoquanInside/archive/2006/02/25/32458.html</link><dc:creator>Baoquan Inside</dc:creator><author>Baoquan Inside</author><pubDate>Sat, 25 Feb 2006 15:12:00 GMT</pubDate><guid>http://www.blogjava.net/BaoquanInside/archive/2006/02/25/32458.html</guid><wfw:comment>http://www.blogjava.net/BaoquanInside/comments/32458.html</wfw:comment><comments>http://www.blogjava.net/BaoquanInside/archive/2006/02/25/32458.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.blogjava.net/BaoquanInside/comments/commentRss/32458.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/BaoquanInside/services/trackbacks/32458.html</trackback:ping><description><![CDATA[<DIV>测试驱动开发(TDD, Test Driven Development)是一种很有意思的软件开发方式，本集以较小的步伐体验TDD。<BR><BR>/**&nbsp; A program simulation&nbsp;of an automat.</DIV>
<DIV>&nbsp;* </DIV>
<DIV>
<DIV>&nbsp;*&nbsp; Director: Zuo Baoquan</DIV>
<DIV>&nbsp;*&nbsp; Contact me:</DIV>
<DIV>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp; Blog:&nbsp;&nbsp; <FONT color=#000080><A HREF="/BaoquanInside">http://www.blogjava.net/BaoquanInside</A><BR>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp; MSN: Baoquan.Zuo [at] hotmail.com<BR></FONT></DIV>
<DIV>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp; Email: &nbsp;Baoquan.Zuo [at] gmail.com</DIV>
<DIV>&nbsp;*</DIV>
<DIV>&nbsp;*&nbsp; Copyright 2006 BEC Studio</DIV></DIV>
<DIV>&nbsp;*/</DIV>
<DIV>&nbsp;</DIV>
<DIV>引言：</DIV>
<DIV>（该问题截取自《<A href="http://www.china-pub.com/computers/common/info.asp?id=16857"><FONT color=#000080>面向对象的设计与模式</FONT></A>》(Written by <A href="http://www.horstmann.com/"><FONT color=#000080>Cay Horstmann</FONT></A>) Exercise 2.18</DIV>
<DIV>&nbsp;</DIV>
<DIV>设计并实现一个模拟自动售货机的程序。通过给机器投入正确的硬币，便可以购买到相应的商品。用户从一个可用的商品列表中选择商品、投入硬币并得到商品。如果没有足够的钱币或由于此商品已销售完毕，则将硬币退回。操作员可以重新备<STRONG>货</STRONG>并将钱币取走。</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>场景#1</DIV>
<DIV>地点：同济大学西南楼</DIV>
<DIV>涉众：我，空自动售货机</DIV>
<DIV>&nbsp;</DIV>
<DIV>寝室楼里面刚搬来了一台空的自动售货机，那我们来测试一下：</DIV>
<DIV>&nbsp;</DIV>
<DIV>public class TestEmptyAutomat extends TestCase<BR>{<BR>&nbsp; public void testIsEmpty() <BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; Automat automat = new Automat();&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp; assertTrue("it should be empty.", automat.isEmpty());<BR>&nbsp; }<BR>}</DIV>
<DIV>&nbsp;</DIV>
<DIV><A href="http://www.eclipse.org/"><FONT color=#000080>Eclipse</FONT></A>提示"Automat cannot be resolved to a type"，好，看我的：</DIV>
<DIV>public class Automat<BR>{<BR>&nbsp; public Automat()&nbsp; // constructor stub<BR>&nbsp; {<BR>&nbsp; }<BR>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>再露一手：</DIV>
<DIV><STRONG>//class Automat</STRONG></DIV>
<DIV>public boolean isEmpty()</DIV>
<DIV>{</DIV>
<DIV>&nbsp; return true;</DIV>
<DIV>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>全部保存，运行测试，呵呵，绿色进度条！测试成功！</DIV>
<DIV>&nbsp;</DIV>
<DIV>好，既然我们还没有投过硬币，那么余额应该是0了～</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class TestEmptyAutomat</DIV>public void testBalance()<BR>{<BR>&nbsp;&nbsp;Automat automat = new Automat();<BR>&nbsp;&nbsp;assertEquals("the balance should be 0.", 0, automat.balance);&nbsp;<BR>}<BR>
<DIV>&nbsp;</DIV>
<DIV>继续使用我们的法宝：</DIV>
<DIV>// class Automat</DIV>
<DIV>public final int balance = 0;</DIV>
<DIV>&nbsp;</DIV>
<DIV>运行测试，yeah!</DIV>
<DIV>&nbsp;</DIV>
<DIV>看了一遍测试程序，决定优化一下：</DIV>
<DIV>public class TestEmptyAutomat extends TestCase<BR>{<BR><STRONG>&nbsp; protected void setUp() throws Exception<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; super.setUp();<BR>&nbsp;&nbsp;&nbsp; automat = new Automat();<BR>&nbsp;&nbsp;}<BR></STRONG>&nbsp;<BR>&nbsp; public void testIsEmpty() <BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; assertTrue("it should be empty.", automat.isEmpty());<BR>&nbsp;&nbsp;}<BR>&nbsp;<BR>&nbsp; public void testBalance()<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; assertEquals("the balance should be 0.", 0, automat.balance);&nbsp;<BR>&nbsp; }<BR>&nbsp;<BR><STRONG>&nbsp; private Automat automat;<BR></STRONG>}<BR></DIV>
<DIV>再运行一次测试，呵呵，又是绿色！</DIV>
<DIV>&nbsp;</DIV>
<DIV>场景#2</DIV>
<DIV>地点：同济大学西南楼</DIV>
<DIV>涉众：我，自动售货机(有一瓶百事可乐)</DIV>
<DIV>&nbsp;</DIV>
<DIV>好消息，楼长在自动售货机里面放了一瓶Pepsi。</DIV>
<DIV>&nbsp;</DIV>
<DIV>public class TestAutomatWithOnePepsi extends TestCase<BR>{<BR>&nbsp;&nbsp;protected void setUp() throws Exception<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; super.setUp();<BR>&nbsp;&nbsp; &nbsp;automat = new Automat();</DIV>
<DIV>&nbsp;&nbsp;&nbsp; pepsi = new Pepsi();&nbsp; // 一瓶百事可乐</DIV>
<DIV>&nbsp;&nbsp;&nbsp; automat.add(pepsi);&nbsp; // 楼长阿姨放的～<BR>&nbsp; }</DIV>
<DIV>&nbsp;&nbsp;</DIV>
<DIV>&nbsp; public void testEmpty()<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; assertFalse("it should not be empty.", automat.isEmpty());<BR>&nbsp;&nbsp;}<BR>&nbsp;<BR>&nbsp; public Automat automat;</DIV>
<DIV>&nbsp; public Pepsi pepsi;<BR>}</DIV>
<DIV><BR>接着创建Pepsi类</DIV>
<DIV>&nbsp;</DIV>
<DIV>public class Pepsi<BR>{<BR>&nbsp; public Pepsi() // constructor stub <BR>&nbsp; {<BR>&nbsp; &nbsp;<BR>&nbsp; }<BR>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>再给Automat添加add方法：</DIV>
<DIV>// class Automat</DIV>
<DIV>public void add(Pepsi pepsi)</DIV>
<DIV>{</DIV>
<DIV>&nbsp;</DIV>
<DIV>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>好，现在有两个TC(Test Case)了，为了运行两个测试案例，我们来创建一个Test Suite:</DIV>
<DIV>
<DIV>public class AutomatTests<BR>{<BR>&nbsp; public static Test suite()<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; TestSuite suite = new TestSuite("Test for net.mybec.automat");<BR>&nbsp;&nbsp;&nbsp; //$JUnit-BEGIN$<BR>&nbsp;&nbsp;&nbsp; suite.addTestSuite(TestAutomatWithOnePepsi.class);<BR>&nbsp;&nbsp;&nbsp; suite.addTestSuite(TestEmptyAutomat.class);<BR>&nbsp;&nbsp;&nbsp; //$JUnit-END$<BR>&nbsp;&nbsp;&nbsp; </DIV>
<DIV>&nbsp;&nbsp;&nbsp; return suite;<BR>&nbsp; }<BR>}</DIV></DIV>
<DIV>&nbsp;</DIV>
<DIV>编译成功，运行AutomatTests，红色进度条。TestEmptyAutomat绿色，TestAutomatWithOnePepsi红色。呵呵，看来要让add做点事情了。</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class Automat</DIV>
<DIV>public void add(Pepsi pepsi)</DIV>
<DIV>{</DIV>
<DIV>&nbsp; goods.add(pepsi);</DIV>
<DIV>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>// 添加一个装Pepsi的数组列表</DIV>
<DIV>private final ArrayList&lt;Pepsi&gt; goods = new ArrayList&lt;Pepsi&gt;();</DIV>
<DIV><BR>&nbsp;</DIV>
<DIV>// 修改isEmpty方法</DIV>
<DIV>public boolean isEmpty()<BR>{<BR>&nbsp; return goods.isEmpty();<BR>}</DIV>
<DIV><BR>&nbsp;</DIV>
<DIV>再次运行AutomatTests，呵呵，绿色！我们喜欢！</DIV>
<DIV>&nbsp;</DIV>
<DIV>好，我们再看看Automat的余额：</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class TestAutomatWithOnePepsi</DIV>
<DIV>public void testBalance()<BR>{<BR>&nbsp; assertEquals("the balance should be 0.", 0, automat.balance);<BR>}<BR></DIV>
<DIV>&nbsp;</DIV>
<DIV>运行一遍测试，Ok。</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>我们还没有投硬币，当然不能买百事可乐了：</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class TestAutomatWithOnePepsi</DIV>
<DIV>public void testCanBuyWithoutBalance()<BR>{<BR>&nbsp;&nbsp;assertFalse("we cannot buy pepsi without money.", automat.canBuy(pepsi));<BR>}<BR></DIV>
<DIV>// class Automat</DIV>
<DIV>public boolean canBuy(Pepsi pepsi)</DIV>
<DIV>{</DIV>
<DIV>&nbsp; return false;</DIV>
<DIV>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>我们太喜欢运行测试了，于是又忍不住运行了所有的自动测试（呵呵，实际上我们只需要点击一个运行按钮）。又是绿色～</DIV>
<DIV>&nbsp;</DIV>
<DIV>好，如果Pepsi的价格是2元，我们投1块钱试试～</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class Pepsi</DIV>
<DIV><STRONG>public static final int PRICE = 2;</STRONG></DIV>
<DIV>&nbsp;</DIV>
<DIV>// class TestAutomatWithOnePepsi</DIV>
<DIV>public void testCanBuyWithOneYuan()<BR>{</DIV>
<DIV>&nbsp; automat.put(1);<BR>&nbsp; assertFalse("we cannot buy pepsi with one yuan.", automat.canBuy(pepsi));<BR>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class Automat</DIV>
<DIV>public void put(int yuan)</DIV>
<DIV>{</DIV>
<DIV>&nbsp;</DIV>
<DIV>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>运行测试，绿色。显然1块钱买不到百事可乐。那就投2块钱吧：</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class TestAutomatWithOnePepsi</DIV>
<DIV>public void testCanBuyWithTwoYuan()<BR>{<BR>&nbsp;&nbsp;automat.put(2);<BR>&nbsp;&nbsp;assertTrue("we can not&nbsp;buy pepsi with two yuan.", automat.canBuy(pepsi));<BR>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>运行测试，红色进度条，<A href="http://www.junit.org/"><FONT color=#000080>JUnit</FONT></A>提示“we can not&nbsp;buy pepsi with two yuan.” 天啊，这不公平。</DIV>
<DIV>想起来了，Automat.put什么也没做。于是我们添了几笔：</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class Automat</DIV>
<DIV>public void put(int yuan)</DIV>
<DIV>{</DIV>
<DIV><STRONG>&nbsp; balance += yuan;</STRONG></DIV>
<DIV>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>public boolean canBuy(Pepsi pepsi)</DIV>
<DIV>{</DIV>
<DIV><STRONG>&nbsp; return balance &gt;= Pepsi.PRICE;</STRONG></DIV>
<DIV>}</DIV>
<DIV>&nbsp;</DIV>
<DIV><STRONG>public int balance = 0</STRONG>;&nbsp; // 去掉了final</DIV>
<DIV>&nbsp;</DIV>
<DIV>迫不及待地点击了运行按钮，yeah！终于能买到喜欢的Pepsi了（因为看到了绿色进度条～）。</DIV>
<DIV>&nbsp;</DIV>
<DIV>于是急忙买了一瓶Pepsi：</DIV>
<DIV>// class TestAutomatWithOnePepsi</DIV>
<DIV>public void testBuyPepsiWithTwoYuan()<BR>{<BR>&nbsp;&nbsp;automat.put(2);<BR>&nbsp;&nbsp;automat.buy(pepsi);</DIV>
<DIV>&nbsp; assertTrue("the automat should&nbsp;be empty.", automat.isEmpty());<BR>}<BR></DIV>
<DIV>// class Automat</DIV>
<DIV>public void buy(Pepsi pepsi)</DIV>
<DIV>{</DIV>
<DIV>&nbsp;</DIV>
<DIV>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>Run Tests, Failed. So, </DIV>
<DIV>// class Automat</DIV>
<DIV>public void buy(Pepsi pepsi)</DIV>
<DIV>{</DIV>
<DIV>&nbsp;&nbsp;<STRONG>goods.remove(pepsi);</STRONG></DIV>
<DIV>}</DIV>
<DIV>Run Tests again, OK.</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>
<DIV>// class TestAutomatWithOnePepsi</DIV>
<DIV>public void testBuyPepsiWithTwoYuan()<BR>{<BR>&nbsp;&nbsp;automat.put(2);<BR>&nbsp;&nbsp;automat.buy(pepsi);</DIV>
<DIV>&nbsp; assertTrue("the automat should&nbsp;be empty.", automat.isEmpty());<BR><STRONG>&nbsp;&nbsp;assertEquals("the balance should be 0.", 0, automat.balance);</STRONG></DIV>
<DIV>}<BR></DIV>
<DIV>Run Tests, failed, so</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class Automat</DIV>
<DIV>public void buy(Pepsi pepsi)</DIV>
<DIV>{</DIV>
<DIV>&nbsp; goods.remove(pepsi);</DIV>
<DIV><STRONG>&nbsp; balance -= Pepsi.PRICE;</STRONG></DIV>
<DIV>}<BR></DIV>
<DIV>Run Tests again, OK.</DIV>
<DIV>&nbsp;</DIV>
<DIV>那如果没有投币就直接买Pepsi呢？再添加一个测试：</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class TestAutomatWithOnePepsi</DIV>
<DIV>public void testBuyPepsiWithNoBalance()<BR>{<BR>&nbsp;&nbsp;try<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp; automat.buy(pepsi);<BR>&nbsp;&nbsp;&nbsp;&nbsp;fail("a NoBalanceException was expected when buying a pepsi with no balance.");<BR>&nbsp;&nbsp;} <BR>&nbsp;&nbsp;catch (NoBalanceException e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;}<BR>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class NoBalanceException </DIV>
<DIV>public class NoBalanceException extends Exception<BR>{<BR>&nbsp; public NoBalanceException(String arg0)<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; super(arg0);<BR>&nbsp; }<BR>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class TestAutomatWithOnePepsi</DIV>
<DIV>public void testBuyPepsiWithTwoYuan()<BR>{<BR>&nbsp;&nbsp;automat.put(2);<BR><STRONG>&nbsp;&nbsp;try<BR>&nbsp;&nbsp;{<BR></STRONG>&nbsp;&nbsp;&nbsp; automat.buy(pepsi);<BR><STRONG>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;catch (NoBalanceException e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp; fail("a NoBalanceException was throwed.");<BR>&nbsp;&nbsp;}&nbsp;<BR></STRONG>&nbsp; assertTrue("the automat should be empty.", automat.isEmpty());&nbsp;<BR>&nbsp;&nbsp;assertEquals("the balance should be 0.", 0, automat.balance);<BR>&nbsp;}</DIV>
<DIV><BR>&nbsp;</DIV>
<DIV>// class Automat</DIV>
<DIV>public void buy(Pepsi pepsi) throws NoBalanceException<BR>{<BR><STRONG>&nbsp;&nbsp;if (balance == 0)</STRONG></DIV>
<DIV><STRONG>&nbsp;&nbsp;&nbsp;&nbsp;throw new NoBalanceException("No balance");<BR>&nbsp;&nbsp;else if&nbsp;(balance &gt;= Pepsi.PRICE)<BR></STRONG>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;goods.remove(pepsi);<BR>&nbsp; &nbsp;&nbsp;balance -= Pepsi.PRICE;<BR>&nbsp;&nbsp;}<BR>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>运行测试，绿色！</DIV>
<DIV>接下来投1块钱买买看：</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class TestAutomatWithOnePepsi</DIV>
<DIV>public void testBuyPepsiWithOneYuan()<BR>{<BR>&nbsp;&nbsp;automat.put(1);<BR>&nbsp;&nbsp;try<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp; automat.buy(pepsi);<BR>&nbsp;&nbsp;&nbsp; fail("a LackOfBalanceException was expected when buying a pepsi without enough balance.");<BR>&nbsp;&nbsp;} <BR>&nbsp;&nbsp;catch (LackOfBalanceException e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;catch (NoBalanceException e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp; &nbsp;fail("a NoBalanceException was not expected.");<BR>&nbsp;&nbsp;}<BR>}<BR></DIV>
<DIV>接下来，为了能使编译通过，做了一下修改：</DIV>
<DIV>// class LackOfBalanceException</DIV>
<DIV>public class LackOfBalanceException extends Exception<BR>{<BR>&nbsp;&nbsp;public LackOfBalanceException(String arg0)<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; super(arg0);<BR>&nbsp; }<BR>}<BR></DIV>
<DIV>// class Automat</DIV>
<DIV>public void buy(Pepsi pepsi) throws NoBalanceException, <STRONG>LackOfBalanceException</STRONG><BR>{</DIV>
<DIV>&nbsp;&nbsp;// ...</DIV>
<DIV>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class TestAutomatWithOnePepsi</DIV>
<DIV>public void testBuyPepsiWithNoBalance()<BR>{</DIV>
<DIV>&nbsp; // ...<BR><STRONG>&nbsp;&nbsp;catch (LackOfBalanceException e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp; fail("a LackOfBalanceException was not expected.");<BR>&nbsp;&nbsp;}</STRONG><BR>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>public void testBuyPepsiWithTwoYuan()<BR>{<BR>&nbsp;&nbsp;// ...</DIV>
<DIV><STRONG>&nbsp;&nbsp;catch (LackOfBalanceException e)<BR>&nbsp;&nbsp;{<BR>&nbsp; &nbsp;&nbsp;fail("a LackOfBalanceException was not expected.");<BR>&nbsp;&nbsp;}&nbsp;<BR></STRONG>&nbsp;&nbsp;// ...<BR>}<BR><BR>好，没有错误提示了。编译，运行测试，红色进度条。</DIV>
<DIV>testBuyPepsiWithOneYuan()提示“a LackOfBalanceException was expected when buying a pepsi with no enough balance."</DIV>
<DIV>&nbsp;</DIV>
<DIV>我们修改一下Automat.buy()：</DIV>
<DIV>// class Automat</DIV>
<DIV>public void buy(Pepsi pepsi) throws NoBalanceException, LackOfBalanceException<BR>{<BR>&nbsp;&nbsp;if (balance == 0)<BR>&nbsp;&nbsp;{<BR>&nbsp; &nbsp;&nbsp;throw new NoBalanceException("No balance");<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;else if (balance &gt;= Pepsi.PRICE)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp; &nbsp;goods.remove(pepsi);<BR>&nbsp;&nbsp;&nbsp; balance -= Pepsi.PRICE;<BR>&nbsp;&nbsp;}<BR><STRONG>&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp; throw new LackOfBalanceException("Lack of Balance");<BR></STRONG>}<BR></DIV>
<DIV>测试通过了！小小庆祝一下～</DIV>
<DIV>&nbsp;</DIV>
<DIV>那如果我们投了三块钱呢？好，试试看：</DIV>
<DIV>&nbsp;</DIV>
<DIV>// class TestAutomatWithOnePepsi</DIV>
<DIV>public void testBuyPepsiWithThreeYuan()<BR>{<BR>&nbsp;&nbsp;automat.put(3);<BR>&nbsp;&nbsp;assertTrue("we can buy pepsi.", automat.canBuy(pepsi));&nbsp;&nbsp;<BR>&nbsp;&nbsp;try<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp; &nbsp;automat.buy(pepsi);<BR>&nbsp;&nbsp;&nbsp; assertTrue("the automat should be empty.", automat.isEmpty());<BR>&nbsp;&nbsp;&nbsp; assertEquals("the balance should be 1.", 1, automat.balance);</DIV>
<DIV>&nbsp;&nbsp;}</DIV>
<DIV>&nbsp;&nbsp;catch (Exception e)<BR>&nbsp;&nbsp;{<BR>&nbsp; &nbsp;&nbsp;fail("Exception was not expected.");<BR>&nbsp;&nbsp;}&nbsp;&nbsp; <BR>}<BR></DIV>
<DIV>&nbsp;</DIV>
<DIV>编译，运行测试，成功！Yeah!</DIV>
<DIV>&nbsp;</DIV>
<DIV>To be continued...</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>下一集将会更加精彩，敬请期待。</DIV></DIV><img src ="http://www.blogjava.net/BaoquanInside/aggbug/32458.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/BaoquanInside/" target="_blank">Baoquan Inside</a> 2006-02-25 23:12 <a href="http://www.blogjava.net/BaoquanInside/archive/2006/02/25/32458.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>