﻿<?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-辰o(^o^)o的专栏[除非注释原创，其它文章基本来源于网络]-文章分类-Test-Driven Development</title><link>http://www.blogjava.net/jackybu/category/2315.html</link><description>&lt;a href="http://www.fastonlineusers.com"&gt;&lt;b&gt;&lt;font color=red&gt;共有&lt;script src=http://fastonlineusers.com/online.php?d=jackybu.blogjava.net&gt;&lt;/script&gt;人在同时阅读此Blog&lt;/font&gt;&lt;/b&gt;&lt;/a&gt;</description><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 07:48:05 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 07:48:05 GMT</pubDate><ttl>60</ttl><item><title>TDD(3) --转自http://www.blogjava.net/yandazhi</title><link>http://www.blogjava.net/jackybu/articles/8399.html</link><dc:creator>辰</dc:creator><author>辰</author><pubDate>Mon, 25 Jul 2005 04:28:00 GMT</pubDate><guid>http://www.blogjava.net/jackybu/articles/8399.html</guid><wfw:comment>http://www.blogjava.net/jackybu/comments/8399.html</wfw:comment><comments>http://www.blogjava.net/jackybu/articles/8399.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jackybu/comments/commentRss/8399.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jackybu/services/trackbacks/8399.html</trackback:ping><description><![CDATA[<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD vAlign=top>
<H2 class=docChapterTitle>意图编程</H2>
<P class=docChapterTitle><SPAN class=docEmphasis>programming by intention</SPAN></P>
<P class=docChapterTitle>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD vAlign=top><A id=ch03lev1sec1 name=ch03lev1sec1></A>
<H3 class=docSection1Title>名字</H3>
<P class=docSection1Title><SPAN class=docEmphStrong><SPAN class=docEmphStrong>Use nouns or noun phrases for class names<BR><BR></P>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Movie&nbsp;{<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;MovieRatingComparator&nbsp;implements&nbsp;Comparator&nbsp;{<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;XMLMovieListReader&nbsp;implements&nbsp;MovieListReader&nbsp;{<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">}<BR><BR></SPAN></DIV></DIV>
<P class=docSection1Title><SPAN class=docEmphStrong>Use either adjectives or generic nouns and noun phrases for interfaces<BR><BR></P>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">interface</SPAN><SPAN style="COLOR: #000000">&nbsp;Serializable&nbsp;{<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">interface</SPAN><SPAN style="COLOR: #000000">&nbsp;MovieListWriter&nbsp;{<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;write(MovieList&nbsp;movieList)&nbsp;throws&nbsp;IOException;<BR>}<BR><BR></SPAN></DIV></DIV>
<P class=docSection1Title><SPAN class=docEmphStrong>Use verbs and verb phrases for method names</SPAN></SPAN></SPAN></SPAN></P></TD></TR></TBODY></TABLE><BR></P></TD></TR></TBODY></TABLE>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;calculateAverageRating()&nbsp;{<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;totalRating&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;calculateTotalRating();<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;totalRating&nbsp;</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">&nbsp;ratings.size();<BR>}<BR><BR></SPAN></DIV></DIV><SPAN class=docEmphStrong><BR>Use accepted conventions for accessors and mutators<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;Category&nbsp;getCategory()&nbsp;{<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;category;<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;setCategory(Category&nbsp;aCategory)&nbsp;{<BR>&nbsp;&nbsp;category&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;aCategory;<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;boolean&nbsp;isOfCategory(Category&nbsp;aCategory)&nbsp;{<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;category.equals(aCategory);<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;boolean&nbsp;isRated()&nbsp;{<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">!</SPAN><SPAN style="COLOR: #000000">ratings.isEmpty();<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;boolean&nbsp;hasRating()&nbsp;{<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">!</SPAN><SPAN style="COLOR: #000000">ratings.isEmpty();<BR>}<BR><BR></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;size()&nbsp;{<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;movies.size();<BR>}<BR><BR></SPAN></DIV></DIV></SPAN><BR><SPAN class=docEmphStrong>Don't put redundant information in method names<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;add(Movie&nbsp;movieToAdd)&nbsp;throws&nbsp;DuplicateMovieException&nbsp;{<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.contains(movieToAdd))&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">throw</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;DuplicateMovieException(movieToAdd.getName());<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;movies.add(movieToAdd);<BR>}<BR><BR></SPAN></DIV></DIV></SPAN>
<P class=docList>There are always exceptions. Sometimes it is just clearer, and reads better, if you have type information in the method name.<BR><BR></P>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;addRating(Rating&nbsp;ratingToAdd)&nbsp;{<BR>&nbsp;&nbsp;ratings.add(ratingToAdd);<BR>}<BR><BR></SPAN></DIV></DIV><SPAN class=docEmphStrong>Use nouns and noun phrases for variable names</SPAN>.<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Movie&nbsp;{<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;String&nbsp;name&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">""</SPAN><SPAN style="COLOR: #000000">;<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;Category&nbsp;category&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;Category.UNCATEGORIZED;<BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;List&nbsp;ratings&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">;<BR><BR>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">}<BR><BR></SPAN></DIV></DIV><BR>
<H3 class=docSection1Title>如何意图编程</H3>
<P class=docSection1Title>包括使用隐喻，测试优先，重构，作出假定，让编译器告诉你下一步做什么 这几种技巧。<BR><BR>公用词汇表 帮助你理解这个领域。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 帮助你取名字。<BR><BR>
<TABLE width="95%">
<TBODY>
<TR>
<TD class=v2 align=left><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD vAlign=top><A id=ch03lev1sec5 name=ch03lev1sec5></A>
<H3 class=docSection1Title>"NO COMMENT"</H3>
<P class=docSection1Title><SPAN class=docEmphStrong>Incomplete code</SPAN> <BR></P><PRE>// TODO: The tree should be balanced after doing the insertion.
</PRE><PRE>// CODE DEBT: the looping structure is a bit convoluted, could use
// some method extraction.
</PRE>
<P class=docSection1Title><BR><SPAN class=docEmphStrong>Refactoring doesn't make it clear enough</SPAN> <BR></P><PRE>// NEEDS WORK: I tried extract method, but it's still awkward.
// Maybe refactoring to a Strategy would clean it up?
</PRE>
<P class=docSection1Title><BR><SPAN class=docEmphStrong>Use of an unusual algorithm</SPAN> <BR></P><PRE>// I used an AVL Tree algorithm here to keep the tree balanced.
</PRE>
<P class=docSection1Title><BR><SPAN class=docEmphStrong>Use of a published algorithm</SPAN> <BR></P><PRE>// This AVL alorithm was based on Brad Appleton's implementation at
// http://www.enteract.com/~bradapp/ftp/src/libs/C++/AvlTrees.html
</PRE>
<P class=docSection1Title><BR><SPAN class=docEmphStrong>Performance tuning</SPAN> <BR></P><PRE>// A circular queue is used here for performance reasons: to avoid
// having to move elements around.
</PRE>
<P class=docSection1Title><BR><SPAN class=docEmphStrong>Class comment</SPAN> <BR></P><PRE>/**
 * This class represents a single movie title. It is responsible for
 * maintaining its own ratings, reviews, etc.
 */
</PRE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></P><img src ="http://www.blogjava.net/jackybu/aggbug/8399.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jackybu/" target="_blank">辰</a> 2005-07-25 12:28 <a href="http://www.blogjava.net/jackybu/articles/8399.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>TDD(1)--转自http://www.blogjava.net/yandazhi</title><link>http://www.blogjava.net/jackybu/articles/8397.html</link><dc:creator>辰</dc:creator><author>辰</author><pubDate>Mon, 25 Jul 2005 04:26:00 GMT</pubDate><guid>http://www.blogjava.net/jackybu/articles/8397.html</guid><wfw:comment>http://www.blogjava.net/jackybu/comments/8397.html</wfw:comment><comments>http://www.blogjava.net/jackybu/articles/8397.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jackybu/comments/commentRss/8397.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jackybu/services/trackbacks/8397.html</trackback:ping><description><![CDATA[<P><STRONG><FONT size=3>TDD是这样一种设计风格</FONT></STRONG></P>
<H4 class=docSection2Title>Maintain an exhaustive suite of Programmer Tests</H4>
<P class=docSection2Title>维护一套程序员测试的框架</P>
<H4 class=docSection2Title>No code goes into production unless it has associated tests</H4>
<P class=docSection2Title>除非它已结合测试，产品不写入任何代码</P>
<H4 class=docSection2Title>Write the tests first</H4>
<P class=docSection2Title>先写测试</P>
<H4 class=docSection2Title>Tests determine what code you need to write</H4>
<P class=docSection2Title>测试决定你需要写什么代码</P>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG id=Codehighlighter1_28_146_Open_Image onclick="this.style.display='none'; Codehighlighter1_28_146_Open_Text.style.display='none'; Codehighlighter1_28_146_Closed_Image.style.display='inline'; Codehighlighter1_28_146_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_28_146_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_28_146_Closed_Text.style.display='none'; Codehighlighter1_28_146_Open_Image.style.display='inline'; Codehighlighter1_28_146_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;testEmptyList()&nbsp;</SPAN><SPAN id=Codehighlighter1_28_146_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_28_146_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;MovieList&nbsp;emptyList&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">newMovieList();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;assertEquals(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Empty&nbsp;list&nbsp;should&nbsp;have&nbsp;size&nbsp;of&nbsp;0</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,&nbsp;emptyList.size());<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV><BR>要通过上面的测试，你必须创建一个类MovieList，和一个方法size();<BR><BR>（eclipse的<FONT size=2>快速修复功能能帮你搞定哦。看来先写测试还是很方便的*^^*）<BR><BR>
<H3 class=docSection1Title>让计算机来告诉你</H3>
<P class=docSection1Title>你需要增加类或者方法，编译器会告诉你。（eclipse会向你抱怨有cannot be resolved 的）</P>下面看看代码的演进<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG id=Codehighlighter1_25_96_Open_Image onclick="this.style.display='none'; Codehighlighter1_25_96_Open_Text.style.display='none'; Codehighlighter1_25_96_Closed_Image.style.display='inline'; Codehighlighter1_25_96_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_25_96_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_25_96_Closed_Text.style.display='none'; Codehighlighter1_25_96_Open_Image.style.display='inline'; Codehighlighter1_25_96_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;testRating()&nbsp;</SPAN><SPAN id=Codehighlighter1_25_96_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_25_96_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;assertEquals(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Bad&nbsp;average&nbsp;rating.</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">4</SPAN><SPAN style="COLOR: #000000">,starWars.getAverageRating());<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG id=Codehighlighter1_25_146_Open_Image onclick="this.style.display='none'; Codehighlighter1_25_146_Open_Text.style.display='none'; Codehighlighter1_25_146_Closed_Image.style.display='inline'; Codehighlighter1_25_146_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_25_146_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_25_146_Closed_Text.style.display='none'; Codehighlighter1_25_146_Open_Image.style.display='inline'; Codehighlighter1_25_146_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;testRating()&nbsp;</SPAN><SPAN id=Codehighlighter1_25_146_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_25_146_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;starWars.addRating(</SPAN><SPAN style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;starWars.addRating(</SPAN><SPAN style="COLOR: #000000">5</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;assertEquals(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Bad&nbsp;average&nbsp;rating.</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">4</SPAN><SPAN style="COLOR: #000000">,starWars.getAverageRating());<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV></FONT>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG id=Codehighlighter1_25_189_Open_Image onclick="this.style.display='none'; Codehighlighter1_25_189_Open_Text.style.display='none'; Codehighlighter1_25_189_Closed_Image.style.display='inline'; Codehighlighter1_25_189_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_25_189_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_25_189_Closed_Text.style.display='none'; Codehighlighter1_25_189_Open_Image.style.display='inline'; Codehighlighter1_25_189_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;testRating()&nbsp;</SPAN><SPAN id=Codehighlighter1_25_189_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_25_189_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;Movie&nbsp;starWars&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Movie(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Star&nbsp;Wars</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;starWars.addRating(</SPAN><SPAN style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;starWars.addRating(</SPAN><SPAN style="COLOR: #000000">5</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;assertEquals(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Bad&nbsp;average&nbsp;rating.</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">4</SPAN><SPAN style="COLOR: #000000">,starWars.getAverageRating());<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV><BR>非常有意思，和我们平时写代码的顺序相反<BR><BR>下面看看getAverageRating()；<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG id=Codehighlighter1_30_44_Open_Image onclick="this.style.display='none'; Codehighlighter1_30_44_Open_Text.style.display='none'; Codehighlighter1_30_44_Closed_Image.style.display='inline'; Codehighlighter1_30_44_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_30_44_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_30_44_Closed_Text.style.display='none'; Codehighlighter1_30_44_Open_Image.style.display='inline'; Codehighlighter1_30_44_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;getAverageRating()&nbsp;</SPAN><SPAN id=Codehighlighter1_30_44_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_30_44_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">4</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG id=Codehighlighter1_30_54_Open_Image onclick="this.style.display='none'; Codehighlighter1_30_54_Open_Text.style.display='none'; Codehighlighter1_30_54_Closed_Image.style.display='inline'; Codehighlighter1_30_54_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_30_54_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_30_54_Closed_Text.style.display='none'; Codehighlighter1_30_54_Open_Image.style.display='inline'; Codehighlighter1_30_54_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;getAverageRating()&nbsp;</SPAN><SPAN id=Codehighlighter1_30_54_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_30_54_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">5</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;totalRating&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_68_98_Open_Image style="DISPLAY: inline" onclick="this.style.display='none'; Codehighlighter1_68_98_Open_Text.style.display='none'; Codehighlighter1_68_98_Closed_Image.style.display='inline'; Codehighlighter1_68_98_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_68_98_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_68_98_Closed_Text.style.display='none'; Codehighlighter1_68_98_Open_Image.style.display='inline'; Codehighlighter1_68_98_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;addRating(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;newRating)&nbsp;</SPAN><SPAN id=Codehighlighter1_68_98_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_68_98_Open_Text style="DISPLAY: inline"><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;totalRating&nbsp;</SPAN><SPAN style="COLOR: #000000">+=</SPAN><SPAN style="COLOR: #000000">&nbsp;newRating;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_171_199_Open_Image onclick="this.style.display='none'; Codehighlighter1_171_199_Open_Text.style.display='none'; Codehighlighter1_171_199_Closed_Image.style.display='inline'; Codehighlighter1_171_199_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_171_199_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_171_199_Closed_Text.style.display='none'; Codehighlighter1_171_199_Open_Image.style.display='inline'; Codehighlighter1_171_199_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;getAverageRating()&nbsp;</SPAN><SPAN id=Codehighlighter1_171_199_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_171_199_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;totalRating&nbsp;</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;numberOfRatings&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_71_122_Open_Image onclick="this.style.display='none'; Codehighlighter1_71_122_Open_Text.style.display='none'; Codehighlighter1_71_122_Closed_Image.style.display='inline'; Codehighlighter1_71_122_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_71_122_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_71_122_Closed_Text.style.display='none'; Codehighlighter1_71_122_Open_Image.style.display='inline'; Codehighlighter1_71_122_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;addRating(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;newRating)&nbsp;</SPAN><SPAN id=Codehighlighter1_71_122_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_71_122_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;totalRating&nbsp;</SPAN><SPAN style="COLOR: #000000">+=</SPAN><SPAN style="COLOR: #000000">&nbsp;newRating;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;numberOfRatings</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_155_197_Open_Image onclick="this.style.display='none'; Codehighlighter1_155_197_Open_Text.style.display='none'; Codehighlighter1_155_197_Closed_Image.style.display='inline'; Codehighlighter1_155_197_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_155_197_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_155_197_Closed_Text.style.display='none'; Codehighlighter1_155_197_Open_Image.style.display='inline'; Codehighlighter1_155_197_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;getAverageRating()&nbsp;</SPAN><SPAN id=Codehighlighter1_155_197_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_155_197_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;totalRating&nbsp;</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">&nbsp;numberOfRatings;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV><BR>实际上，每次变化之后都重新编译和运行这个测试。<BR><BR>
<H2 class=docSidebarTitle>Agile Modeling and TDD</H2>
<P class=docSidebarTitle><BR>采用XP（极限编程）的项目都采用了TDD的辅助手段。建模（modeling）是XP当中很重要的的一个部分。XP开发者使用用户叙述（user stories） ,用户叙述是清晰的敏捷模型。<BR><BR>创建敏捷模型能够帮助我们TDD工作，这是因为，他能揭示我们需要的测试。一个敏捷模型草图的背后总是隐含着这样的思考“我怎样来测试他”，这将导致一个新的测试案例。<BR></P><img src ="http://www.blogjava.net/jackybu/aggbug/8397.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jackybu/" target="_blank">辰</a> 2005-07-25 12:26 <a href="http://www.blogjava.net/jackybu/articles/8397.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>TDD(2) --转自http://www.blogjava.net/yandazhi</title><link>http://www.blogjava.net/jackybu/articles/8398.html</link><dc:creator>辰</dc:creator><author>辰</author><pubDate>Mon, 25 Jul 2005 04:26:00 GMT</pubDate><guid>http://www.blogjava.net/jackybu/articles/8398.html</guid><wfw:comment>http://www.blogjava.net/jackybu/comments/8398.html</wfw:comment><comments>http://www.blogjava.net/jackybu/articles/8398.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jackybu/comments/commentRss/8398.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jackybu/services/trackbacks/8398.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 重构以两种方式和TDD密切相关。在以尽可能简单的手段通过测试之后，（在这个过程中违反了任何编码规则）我们就进行重构清理。大部分是除去我们为了通过测试而带来的重复。如果我们是按TDD行事，那么我们就有了适当的测试的安全网，这使得我们有信心进行重构。&nbsp;&nbsp;&nbsp;何时重构？1&nbsp; 重复的时候public&nbsp;boolean&nbsp;save()&n...&nbsp;&nbsp;<a href='http://www.blogjava.net/jackybu/articles/8398.html'>阅读全文</a><img src ="http://www.blogjava.net/jackybu/aggbug/8398.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jackybu/" target="_blank">辰</a> 2005-07-25 12:26 <a href="http://www.blogjava.net/jackybu/articles/8398.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>TDD faq</title><link>http://www.blogjava.net/jackybu/articles/8396.html</link><dc:creator>辰</dc:creator><author>辰</author><pubDate>Mon, 25 Jul 2005 04:23:00 GMT</pubDate><guid>http://www.blogjava.net/jackybu/articles/8396.html</guid><wfw:comment>http://www.blogjava.net/jackybu/comments/8396.html</wfw:comment><comments>http://www.blogjava.net/jackybu/articles/8396.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jackybu/comments/commentRss/8396.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jackybu/services/trackbacks/8396.html</trackback:ping><description><![CDATA[<P>在TDD中煎熬了已有一阵子了,所谓吃得苦中苦,方为人上人.回首这段旅程,需要总结的东西很多,我只理理曾经出现在脑海中的疑问,并提供本人的对该问题的理解.以后随时补充.</P>
<P><STRONG>☆写测试的时间比写代码的时间还多?</STRONG></P>
<P>在有些情况下的确如此,但是不要太担心.为什么呢? 根据我的体会:</P>
<P>1.有了测试,你会少写很多本来不需要(初看起来应该是有用)的代码</P>
<P>2.写测试的过程就是在解决问题的过程,因此你会比较容易,尽早地明白你到底应该做什么.这样在写代码时,就能节省时间</P>
<P>3.对重构帮助很大.有了测试,你才能放心大胆的进行重构.</P>
<P>4.长远来看,因为TDD会促进你写出好的代码,并且你会经常的重构,因此会降低维护代价</P>
<P><STRONG>☆需要为每个方法编写测试吗?</STRONG></P>
<P>当然不需要.我们所写的测试必须是针对接口方法的.一般认为处理业务逻辑的方法,以及领域模型对象的关键行为是必须进行测试. 其它的一些方法需要自己把握,当然这需要经验.</P>
<P>我现在只是一个新手,没有啥经验.我判断某个方法是否需要测试,依据有两条:</P>
<P>1.是否满足我上面列出的必须测试条件</P>
<P>2.是否值的测试,这一条主要是心理因素,例如对某个方法感觉心里没底,那就先编写测试.</P>
<P><STRONG>☆TDD是一种测试新技术吗?</STRONG></P>
<P>当然不是,TDD根本就不是一项测试技术.它是一种新的开发方式,只是借助测试而已.<BR><BR></P>
<P><STRONG>☆项目一开始没有采用TDD,在项目中期再引入TDD,可行吗?</STRONG></P>
<P>一般来说不推荐在项目中期再引入TDD,这是由于TDD内在特性决定的.</P>
<P>1.TDD是一种新的开发方法,在开发过程中就需要你转变思想,需要在实践不断完善自己,而且它本身就具有一个较陡学习的坡度,这一点在很多文章中都提到过.因此在项目中期引入TDD,会立即拖延项目进展,对项目本身帮助也不会太大.</P>
<P>2.TDD在你开始写测试时,会驱动你对问题进行思考,然后持续进行功能增强和重构.在项目中期,如果你编写一个测试,这时你需要项目早期的一个组件,但是这个组件并没有满足你的测试(因为根本就没有测试).现在因为该组件有问题,测试通不过.如果这时你再为该组件编写单元测试,就失去了测试驱动开发的优势了,此时TDD的效果就大打折扣了.</P>
<P>当然,在没有项目压力的情况下,引入TDD是没有任何问题的.不过我还是推荐在项目开始就引入TDD是最佳选择.</P>
<P><STRONG>☆为也存在的组件补充单元测试值得吗?</STRONG></P>
<P>在上一问题的第二点原因中已经提到过,感觉不值得.在这种情况下,用一般的方法测试一下即可,比如java的main()方法.</P>
<P><STRONG>☆TDD编写的测试案例是比较复杂的吗?</STRONG></P>
<P>在TDD中,测试是一步一步演化的,需要你一直保持简单设计的理念,因此,一般测试案例是比较清晰的.如果发现你的测试非常复杂,应该是你没有抓住问题的重点或者没有掌握正确,有效的方法.</P><img src ="http://www.blogjava.net/jackybu/aggbug/8396.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jackybu/" target="_blank">辰</a> 2005-07-25 12:23 <a href="http://www.blogjava.net/jackybu/articles/8396.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Test-Driven Development</title><link>http://www.blogjava.net/jackybu/articles/8395.html</link><dc:creator>辰</dc:creator><author>辰</author><pubDate>Mon, 25 Jul 2005 04:22:00 GMT</pubDate><guid>http://www.blogjava.net/jackybu/articles/8395.html</guid><wfw:comment>http://www.blogjava.net/jackybu/comments/8395.html</wfw:comment><comments>http://www.blogjava.net/jackybu/articles/8395.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jackybu/comments/commentRss/8395.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jackybu/services/trackbacks/8395.html</trackback:ping><description><![CDATA[<DIV id=header>
<H1 class=title>
<DIV><IMG height=300 src="http://www.xprogramming.com/images/circles.jpg" width=500></DIV></H1></DIV>
<DIV class=entity>
<DIV><A name=test>&nbsp; 
<H3><FONT color=#0000ff><EM>Test-Driven Development</EM></FONT></H3>
<P></A><A href="http://www.xprogramming.com/xpmag/whatisxp.htm#test" target=_SELF>http://www.xprogramming.com/xpmag/whatisxp.htm#test</A><A name=test></P></A>
<P><FONT color=#0000ff><EM>Extreme Programming is obsessed with feedback, and in software development, good feedback requires good testing. Top XP teams practice "test-driven development", working in very short cycles of adding a test, then making it work. Almost effortlessly, teams produce code with nearly 100 percent test coverage, which is a great step forward in most shops. (If your programmers are already doing even more sophisticated testing, more power to you. Keep it up, it can only help!)</EM></FONT></P>
<P><FONT color=#0000ff><EM>It isn't enough to write tests: you have to run them. Here, too, Extreme Programming is extreme. These "programmer tests", or "unit tests" are all collected together, and every time any programmer releases any code to the repository (and pairs typically release twice a day or more), every single one of the programmer tests must run correctly. One hundred percent, all the time! This means that programmers get immediate feedback on how they're doing. Additionally, these tests provide invaluable support as the software design is improved.</EM></FONT></P></DIV>
<DIV>在上面的图中，列出的的是XP的12个团队实践。Test-Driven Development是其中之一。</DIV>
<DIV>Kent Beck 的著作TDD(Test Driven Development) 中详细讲述了测试驱动开发。</DIV>
<DIV>在XP中测试是一种设计，不是简单的为了测试你的功能是否实现。测试先行一个目的</DIV>
<DIV>是可以在代码编写之前，保证测试已经写好；其实这些测试的另一个目的就是设计接口，</DIV>
<DIV>保证接口在以后的修改过程中能够满足测试（设计）。<EM><FONT color=#ff0000>当你有意或无意修改接口的时候，你必须同时</FONT></EM></DIV>
<DIV><FONT color=#ff0000><EM>修改测试 </EM></FONT><STRONG><FONT color=#0000ff>要改动接口的时候，也应该是先修改测试，再修改代码。而不是修改代码等着报错再修改</FONT></STRONG></DIV>
<DIV><FONT color=#000000><STRONG><FONT color=#0000ff>测试(设计）</FONT></STRONG>。（测试必须100%通过才能进行下一个功能），这样测试也是在提醒你，接口已经改变。</FONT></DIV>
<DIV>实际上，设计已经改变。当你修改单元测试的时候，可能就会遭到抱怨。为什么？你修改了设计</DIV>
<DIV>，别人的代码是依赖你修改前的测试（设计），那么新的测试（设计）中的变动可能要影响到</DIV>
<DIV>别人已有的代码。</DIV>
<DIV>&nbsp;</DIV>
<DIV>当你使用TDD的时候一定要说明是测试驱动开发还是测试驱动设计。这两者是有区别的。测试驱动</DIV>
<DIV>开发，是通过测试定义所要开发的功能的接口，然后实现功能的开发过程。对于测试驱动设计，在XP中似乎</DIV>
<DIV>已经消失了，而是被测试驱动开发所取代。另外在XP中有用于描述设计的，SimpleDesign ，Design Improvement.</DIV>
<DIV>在XP中测试是一种设计，设计之后才有开发，这也就是为什么要测试先行了。</DIV>
<DIV>在XP中测试是一种文档，用于描述设计。</DIV></DIV><img src ="http://www.blogjava.net/jackybu/aggbug/8395.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jackybu/" target="_blank">辰</a> 2005-07-25 12:22 <a href="http://www.blogjava.net/jackybu/articles/8395.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>