﻿<?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-aldream-文章分类-JUnit</title><link>http://www.blogjava.net/aldreamlau/category/27257.html</link><description>me</description><language>zh-cn</language><lastBuildDate>Tue, 13 Nov 2007 09:11:29 GMT</lastBuildDate><pubDate>Tue, 13 Nov 2007 09:11:29 GMT</pubDate><ttl>60</ttl><item><title>eclipse Junit测试向导</title><link>http://www.blogjava.net/aldreamlau/articles/159974.html</link><dc:creator>aldream</dc:creator><author>aldream</author><pubDate>Mon, 12 Nov 2007 07:10:00 GMT</pubDate><guid>http://www.blogjava.net/aldreamlau/articles/159974.html</guid><wfw:comment>http://www.blogjava.net/aldreamlau/comments/159974.html</wfw:comment><comments>http://www.blogjava.net/aldreamlau/articles/159974.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/aldreamlau/comments/commentRss/159974.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/aldreamlau/services/trackbacks/159974.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;">
原文出处：<a href="http://www.laliluna.de/assets/tutorials/junit-testing-en.pdf">http://www.laliluna.de/assets/tutorials/junit-testing-en.pdf</a></span>
<p>&nbsp;</p>
<p>JUnit is a simple Java testing framework to
write tests for you Java application. This tutorial gives you an
overview of the features of JUnit and shows a little example how you
can write tests for your Java application.</p>
<h1>General</h1>
<p><strong>Author</strong>:
</p>
<p>Sascha
Wolski</p>
<p>Sebastian
Hennebrueder</p>
<p><a href="http://www.laliluna.de/tutorials.html">http://www.laliluna.de/tutorials.html</a>
? Tutorials for Struts, EJB, xdoclet and eclipse.</p>
<p><br />
</p>
<p><strong>Date</strong>:
</p>
<p>April,
12 2005</p>
<p><br />
</p>
<p><strong>Software:</strong></p>
<p>Eclipse
3.x</p>
<p>Junit
2.x</p>
<p><br />
</p>
<p><strong>Source
code:</strong></p>
<p><a href="http://www.laliluna.de/assets/tutorials/junit-testing-source.zip">http://www.laliluna.de/assets/tutorials/junit-testing-source.zip</a></p>
<p><strong>PDF
Version</strong></p>
<p><a href="http://www.laliluna.de/assets/tutorials/junit-testing-en.pdf">http://www.laliluna.de/assets/tutorials/junit-testing-en.pdf</a></p>
<h1>What is JUnit</h1>
<p>JUnit is a simple open source Java testing
framework used to write and run repeatable automated tests. It is an
instance of the xUnit architecture for unit testing framework.
Eclipse supports creating test cases and running test suites, so it
is easy to use for your Java applications.</p>
<p>JUnit features include:</p>
<ul>
    <li>
    <p>Assertions for testing expected results</p>
    </li>
    <li>
    <p>Test fixtures for sharing common test
    data</p>
    </li>
    <li>
    <p>Test suites for easily organizing and
    running tests</p>
    </li>
    <li>
    <p>Graphical and textual test runners</p>
    </li>
</ul>
<h1>What is a test case</h1>
<p>A test case is a class which holds a number
of test methods. For example if you want to test some  methods of a
class <em>Book</em> you create a class <em>BookTest</em> which extends
the JUnit <em>TestCase</em> class and place your test methods in there.</p>
<h1>How you write and run a simple test</h1>
<ol>
    <li>
    <p>Create
    a subclass of TestCase:</p>
    </li>
</ol>
<p><br />
</p>
<pre>public class BookTest extends TestCase{ <br />
//..<br />
}</pre>
<p>
<br />
</p>
<ol start="2">
    <li>
    <p>Write
    a test method to assert expected results on the object under test:</p>
    </li>
</ol>
<p>Note: The naming convention for a test
method is testXXX()</p>
<pre>public void testCollection() {<br />
Collection collection = new ArrayList();<br />
assertTrue(collection.isEmpty());<br />
} </pre>
<p>
<br />
</p>
<ol start="3">
    <li>
    <p>Write
    a <em>suite()</em> method that uses reflection to dynamically create a
    test suite containing all the <em>testXXX()</em> methods:</p>
    </li>
</ol>
<p><br />
</p>
<pre>public static Test suite(){<br />
return new TestSuite(BookTest.class);<br />
}</pre>
<p>
<br />
<br />
</p>
<ol start="4">
    <li>
    <p>Activate the JUnit view in Eclipse
    (<em>Window &gt; Show View &gt; Other.. &gt; Java &gt; JUnit</em>).</p>
    </li>
</ol>
<h1><img name="Graphic1" src="http://www.laliluna.de/assets/images/tutorials/junit-testing/junit-view.gif" alt="" align="left" border="0" height="356" width="402" /><br clear="left" />
<br />
<br />
</h1>
<p>You
find the <em>JUnit</em> tab near the <em>Package Explorer</em> tab. You
can change the position of the tab by drag and drop it.</p>
<p><img name="Graphic2" src="http://www.laliluna.de/assets/images/tutorials/junit-testing/junit-view2.gif" alt="" align="left" border="0" height="360" width="336" /><br clear="left" />
<br />
<br />
</p>
<ol start="5">
    <li>
    <p style="page-break-before: always;" class="ttext-western">
    Right click on the subclass of
    <em>TestCase</em> and choose <em>Run &gt; JUnit Test</em> to run the
    test.</p>
    </li>
</ol>
<h1>Using a test fixture</h1>
<p>A test fixture is useful if you have two or
more tests for a common set of objects. Using a test fixture avoids
duplicating the test code necessary to initialize and cleanup those
common objects for each test.</p>
<p>To create a test fixture, define a <em>setUp()</em>
method that initializes common object and a <em>tearDown()</em> method
to cleanup those objects. The JUnit framework automatically invokes
the <em>setUp()</em> method before a each test is run and the
<em>tearDown()</em> method after each test is run.</p>
<p>The following test uses a test fixture:</p>
<pre>public class BookTest2 extends TestCase {<br />
<br />
private Collection collection;<br />
<br />
protected void setUp() {<br />
collection = new ArrayList();<br />
}<br />
<br />
protected void tearDown() {<br />
collection.clear(); <br />
}<br />
<br />
public void testEmptyCollection(){<br />
assertTrue(collection.isEmpty());<br />
}<br />
}</pre>
<h1>
Dynamic and static way of running single tests</h1>
<p>JUnit supports two ways (static and dynamic)
of running single tests.
</p>
<p>In static way you override the <em>runTest()</em>
method inherited form TestCase class and call the desired test case.
A convenient way to do this is with an anonymous inner class.
</p>
<p>Note: Each test must be given a name, so
you can identify it if it fails.
</p>
<pre>TestCase test = new BookTest("equals test") {<br />
public void runTest() {<br />
testEquals();<br />
}<br />
};</pre>
<p>
<br />
<br />
</p>
<p>The dynamic way to create a test case to be
run uses reflection to implement <em>runTest</em>. It assumes the name
of the test is the name of the test case method to invoke. It
dynamically finds and invokes the test method. The dynamic way is
more compact to write but it is less static type safe. An error in
the name of the test case goes unnoticed until you run it and get a
<em>NoSuchMethodException</em>. We leave the choice of which to use up
to you.</p>
<pre>TestCast test = new BookTest("testEquals");</pre>
<h1>
What is a TestSuite</h1>
<p>If you have two tests and you'll run them
together you could run the tests one at a time yourself, but you
would quickly grow tired of that. Instead, JUnit provides an object
<em>TestSuite</em> which runs any number of test cases together. The
suite method is like a main method that is specialized to run tests.
</p>
<p>Create a suite and add each test case you
want to execute:</p>
<pre>public static void suite(){<br />
TestSuite suite = new TestSuite();<br />
suite.addTest(new BookTest("testEquals"));<br />
suite.addTest(new BookTest("testBookAdd"));<br />
return suite;<br />
}</pre>
<p>
<br />
<br />
</p>
<p>Since JUnit 2.0 there is an even simpler way
to create a test suite, which holds all testXXX() methods. You only
pass the class with the tests to a TestSuite and it extracts the test
methods automatically.</p>
<p>Note: If you use this way to create a
TestSuite all test methods will be added. If you do not want all test
methods in the TestSuite use the normal way to create it.</p>
<p>Example:</p>
<pre>public static void suite(){<br />
return new TestSuite(BookTest.class);<br />
}</pre>
<h1>
A little example</h1>
<p>Create a new Java project named
JUnitExample.
</p>
<p>Add a package
<em>de.laliluna.tutorial.junitexample</em> where you place the example
classes and a package <em>test.laliluna.tutorial.junitexample</em>
where you place your test classes.
</p>
<h2>The class Book</h2>
<p>Create a new class <em>Book</em> in the
package <em>de.laliluna.tutorial.junitexample</em>.</p>
<p>Add two properties <em>title</em> of type
<em>String</em> and <em>price</em> of type <em>double</em>.</p>
<p>Add a constructor to set the two properties.</p>
<p>Provide a getter- and setter-method for each
of them.</p>
<p>Add a method trunk for a method
<em>equals(Object object)</em> which checks if the object is an
instance of the class Book and the values of the object are equal.
The method return a boolean value.</p>
<p>Note: Do not write the logic of the
<em>equals(..)</em> method, we do it after finish creating the test
method.</p>
<p>The following source code shows the class
Book.</p>
<pre>public class Book {<br />
<br />
private String title;<br />
private double price;<br />
<br />
/**<br />
* Constructor <br />
* <br />
* @param title<br />
* @param price<br />
*/<br />
public Book(String title,<br />
double price) {<br />
this.title = title;<br />
this.price = price;<br />
}<br />
<br />
/**<br />
* Check if an object is an instance of book<br />
* and the values of title and price are equal<br />
* then return true, otherwise return false<br />
*/<br />
public boolean equals(Object object) {<br />
<br />
return false;<br />
}<br />
<br />
public double getPrice() {<br />
return price;<br />
}<br />
<br />
public void setPrice(double price) {<br />
this.price = price;<br />
}<br />
<br />
public String getTitle() {<br />
return title;<br />
}<br />
<br />
public void setTitle(String title) {<br />
this.title = title;<br />
}<br />
}</pre>
<h2>
The test case BookTest</h2>
<p>Create a new test case <em>BookTest</em> in
the package <em>test.laliluna.tutorial.junitexample</em><span style="font-style: normal;">
Right click on the package and choose </span><em>New &gt; JUnit Test
Case</em><span style="font-style: normal;">. </span>
</p>
<p style="font-style: normal;" class="ttext-western">In the wizard
choose the methods stubs <em>setUp()</em>, <em>tearDown()</em> and
<em>constructor()</em>.</p>
<p><img name="Graphic3" src="http://www.laliluna.de/assets/images/tutorials/junit-testing/new-test-case.gif" alt="" align="left" border="0" height="365" width="464" /><br clear="left" />
<br />
<br />
</p>
<p><br />
<br />
</p>
<p>The
following source code shows the class BookTest</p>
<pre>public class BookTest extends TestCase {<br />
<br />
/**<br />
* setUp() method that initializes common objects<br />
*/<br />
protected void setUp() throws Exception {<br />
super.setUp();<br />
}<br />
<br />
/**<br />
* tearDown() method that cleanup the common objects<br />
*/<br />
protected void tearDown() throws Exception {<br />
super.tearDown();<br />
}<br />
<br />
/**<br />
* Constructor for BookTest.<br />
* @param name<br />
*/<br />
public BookTest(String name) {<br />
super(name);<br />
}<br />
<br />
}</pre>
<p>
<br />
<br />
</p>
<p>Now we want to write a test for the
<em>equals(..)</em> method of the class <em>Book</em>. We provide three
private properties, book1, book2 and book3 of type <em>Book. </em>
</p>
<pre>private Book book1;<br />
private Book book2;<br />
private Book book3;</pre>
<p>
<br />
<br />
</p>
<p>Within the <em>setUp()</em> method we
initializes the three properties with some values. Property book1 and
book3 are the same.</p>
<pre>protected void setUp() throws Exception {<br />
super.setUp();<br />
book1 = new Book("ES", 12.99);<br />
book2 = new Book("The Gate", 11.99);<br />
book3 = new Book("ES", 12.99);<br />
}</pre>
<p>
<br />
<br />
</p>
<p>Within the tearDown() method we cleanup the
properties:</p>
<pre>protected void tearDown() throws Exception {<br />
super.tearDown();<br />
book1 = null;<br />
book2 = null;<br />
book3 = null;<br />
}</pre>
<p>
<br />
<br />
</p>
<p>Now, add a test method <em>testEquals()</em>
to the test case. Within the method we use the <em>assertFalse()</em>
method of the JUnit framework to test if the return-value of the
equals(..) method is false, because book1 and book2 are not the same.
If the return-value is false the logic of the equals() method is
correct, otherwise there is a logical problem while comparing the
objects. We want to test if the method compares the objects correctly
by using the assertTrue() method. Book1 and Book3 are the same,
because both are an instance of the class Book and have the same
values.</p>
<p>The following source code shows the
testEquals() method:</p>
<pre>public void testEquals(){<br />
assertFalse(book2.equals(book1));<br />
assertTrue(book1.equals(book1));<br />
}</pre>
<p>
<br />
<br />
</p>
<h2>Writing the logic of the equals() method</h2>
<p>We have finished the test and now we can add
the logic to the <em>equals()</em> method stub. Open the class Book and
add the logic to the <em>equals()</em> method. First we check if the
object given by the method is an instance of <em>Book</em>. Then
compare the properties <em>title</em> and <em>price</em><span style="font-style: normal;">,
if they are equal return true. </span>
</p>
<pre>public boolean equals(Object object) {<br />
if (object instanceof Book) {<br />
Book book = (Book) object;<br />
return getTitle().equals(book.getTitle())<br />
&amp;&amp; getPrice() == book.getPrice();<br />
}<br />
return false;<br />
}</pre>
<h2>
<br />
Create the suite() method</h2>
<p>In order to run the test method <em>testEquals()</em>
add a method <em>suite()</em> to the class <em>BookTest</em>.
</p>
<p>Note: You can also create a separate class
where you add the <em>suite()</em>&nbsp;method.</p>
<p>Within the method create a new instance of
<em>TestSuite</em> and use the method <em>addTest(..)</em> to add a test.
Here we use the dynamically way to add a test to a TestSuite.</p>
<p>The method looks like the follows:</p>
<pre>public static Test suite(){<br />
TestSuite suite = new TestSuite();<br />
suite.addTest(new BookTest("testEquals"));<br />
return suite;<br />
}</pre>
<p>
<br />
<br />
</p>
<h2>Run the test</h2>
<p>After finishing all test methods we want to
run the JUnit test case. Right mouse button on the class BookTest and
choose <em>Run As &gt; JUnit Test</em>.
</p>
<p>On the JUnit view (Menu Windows -&gt; show
view) of Eclipse you can see how many runs, errors and failures
occurred.</p>
<br />
<img src ="http://www.blogjava.net/aldreamlau/aggbug/159974.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/aldreamlau/" target="_blank">aldream</a> 2007-11-12 15:10 <a href="http://www.blogjava.net/aldreamlau/articles/159974.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个Junit例子(转自caterpillar.onlyfun.net)</title><link>http://www.blogjava.net/aldreamlau/articles/159653.html</link><dc:creator>aldream</dc:creator><author>aldream</author><pubDate>Sat, 10 Nov 2007 16:55:00 GMT</pubDate><guid>http://www.blogjava.net/aldreamlau/articles/159653.html</guid><wfw:comment>http://www.blogjava.net/aldreamlau/comments/159653.html</wfw:comment><comments>http://www.blogjava.net/aldreamlau/articles/159653.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/aldreamlau/comments/commentRss/159653.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/aldreamlau/services/trackbacks/159653.html</trackback:ping><description><![CDATA[使用JUnit時，您主要都是透過繼承TestCase類別來撰寫測試案例，預設上您可以使用testXXX() 名稱來撰寫單元測試。<br />
<br />
在測試一個單元方法時，有時您會需要給它一些物件作為運行時的資料，例如您撰寫下面這個測試案例：<br />
<br />
MaxMinTest.java<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);">&nbsp;1</span>&nbsp;<span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);">&nbsp;onlyfun.caterpillar.test;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;2</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;3</span>&nbsp;<span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;onlyfun.caterpillar.MaxMinTool;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;4</span>&nbsp;<span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;junit.framework.TestCase;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;5</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;6</span>&nbsp;<span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;MaxMinTest&nbsp;</span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">&nbsp;TestCase&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;7</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;testMax()&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;8</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]&nbsp;arr&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;{</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">};<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;9</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assertEquals(</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,&nbsp;MaxMinTool.getMax(arr));<br />
</span><span style="color: rgb(0, 128, 128);">10</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">11</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">12</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;testMin()&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">13</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]&nbsp;arr&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;{</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">};<br />
</span><span style="color: rgb(0, 128, 128);">14</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assertEquals(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,&nbsp;MaxMinTool.getMin(arr));<br />
</span><span style="color: rgb(0, 128, 128);">15</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">16</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">17</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;main(String[]&nbsp;args)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">18</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;junit.swingui.TestRunner.run(MaxMinTest.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);<br />
</span><span style="color: rgb(0, 128, 128);">19</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">20</span>&nbsp;<span style="color: rgb(0, 0, 0);">}</span></div>
<br />
您將設計的MaxMinTool包括靜態方法getMax()與getMin()，當您給它一個整數陣列，它們將個別傳回陣列中的最大值與最小值，顯然
的，您所準備的陣列重複出現在兩個單元測試之中，重複的程式碼在設計中可以減少就儘量減少，在這兩個單元測試中，整數陣列的準備是單元方法所需要的資源，
我們稱之為fixture，也就是一個測試時所需要的資源集合。<br />
<br />
fixture必須與上下文（Context）無關，也就是與程式執行前後無關，這樣才符合單元測試的意涵，為此，通常將所需的fixture撰寫在單元方法之中，如此在單元測試開始時創建fixture，並於結束後銷毀fixture。<br />
<br />
然而對於重複出現在各個單元測試中的fixture，您可以集中加以管理，您可以在繼承TestCase之後，重新定義<span style="font-weight: bold;">setUp()</span>與<span style="font-weight: bold;">tearDown()</span>方法，將數個單元測試所需要的fixture在setUp()中創建，並在tearDown()中銷毀，例如：<br />
<br />
MaxMinTest.java<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);">&nbsp;1</span>&nbsp;<span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);">&nbsp;onlyfun.caterpillar.test;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;2</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;3</span>&nbsp;<span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;onlyfun.caterpillar.MaxMinTool;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;4</span>&nbsp;<span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;junit.framework.TestCase;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;5</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;6</span>&nbsp;<span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;MaxMinTest&nbsp;</span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">&nbsp;TestCase&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;7</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]&nbsp;arr;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;8</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;9</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;setUp()&nbsp;</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);">&nbsp;Exception&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">10</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">.setUp();<br />
</span><span style="color: rgb(0, 128, 128);">11</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]{</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">};<br />
</span><span style="color: rgb(0, 128, 128);">12</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">13</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">14</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;tearDown()&nbsp;</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);">&nbsp;Exception&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">15</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">.tearDown();<br />
</span><span style="color: rgb(0, 128, 128);">16</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">;<br />
</span><span style="color: rgb(0, 128, 128);">17</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">18</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">19</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;testMax()&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">20</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assertEquals(</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,&nbsp;MaxMinTool.getMax(arr));<br />
</span><span style="color: rgb(0, 128, 128);">21</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">22</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">23</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;testMin()&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">24</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assertEquals(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,&nbsp;MaxMinTool.getMin(arr));<br />
</span><span style="color: rgb(0, 128, 128);">25</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">26</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">27</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;main(String[]&nbsp;args)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">28</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;junit.swingui.TestRunner.run(MaxMinTest.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);<br />
</span><span style="color: rgb(0, 128, 128);">29</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">30</span>&nbsp;<span style="color: rgb(0, 0, 0);">}</span></div>
<br />
setUp()方法會在每一個單元測試testXXX()方法開始前被呼叫，因而整數陣列會被建立，而tearDown()會在每一個單元測試
testXXX()方法結束後被呼叫，因而整數陣列參考名稱將會參考至null，如此一來，您可以將fixture的管理集中在
setUp()與tearDown()方法之後。<br />
<br />
最後按照測試案例的內容，您完成MaxMinTool類別：
<br />
<br />
MaxMinTool.java<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);">&nbsp;1</span>&nbsp;<span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);">&nbsp;onlyfun.caterpillar;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;2</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;3</span>&nbsp;<span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;MaxMinTool&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;4</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;getMax(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]&nbsp;arr)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;5</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;max&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;Integer.MIN_VALUE;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;6</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;7</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;arr.length;&nbsp;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;8</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(arr[i]&nbsp;</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;max)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;9</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;arr[i];<br />
</span><span style="color: rgb(0, 128, 128);">10</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">11</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">12</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;max;<br />
</span><span style="color: rgb(0, 128, 128);">13</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">14</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">15</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;getMin(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]&nbsp;arr)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">16</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;min&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;Integer.MAX_VALUE;<br />
</span><span style="color: rgb(0, 128, 128);">17</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">18</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;arr.length;&nbsp;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">19</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(arr[i]&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;min)<br />
</span><span style="color: rgb(0, 128, 128);">20</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;arr[i];<br />
</span><span style="color: rgb(0, 128, 128);">21</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">22</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">23</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;min;<br />
</span><span style="color: rgb(0, 128, 128);">24</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">25</span>&nbsp;<span style="color: rgb(0, 0, 0);">}</span></div>
<br />
Swing介面的TestRunner在測試失敗時會顯示紅色的棒子，而在測試成功後會顯示綠色的棒子，而 <span style="font-weight: bold;">"Keep the bar green to keep the code clean."</span> 正是JUnit的名言，也是測試的最終目的。<br />
<img src ="http://www.blogjava.net/aldreamlau/aggbug/159653.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/aldreamlau/" target="_blank">aldream</a> 2007-11-11 00:55 <a href="http://www.blogjava.net/aldreamlau/articles/159653.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>