﻿<?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-自然-文章分类-test</title><link>http://www.blogjava.net/masen/category/21778.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 09 Mar 2015 13:49:51 GMT</lastBuildDate><pubDate>Mon, 09 Mar 2015 13:49:51 GMT</pubDate><ttl>60</ttl><item><title>tps</title><link>http://www.blogjava.net/masen/articles/423326.html</link><dc:creator>Masen</dc:creator><author>Masen</author><pubDate>Mon, 09 Mar 2015 13:15:00 GMT</pubDate><guid>http://www.blogjava.net/masen/articles/423326.html</guid><wfw:comment>http://www.blogjava.net/masen/comments/423326.html</wfw:comment><comments>http://www.blogjava.net/masen/articles/423326.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/masen/comments/commentRss/423326.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/masen/services/trackbacks/423326.html</trackback:ping><description><![CDATA[<div>ＱＰＳ（ＴＰＳ）：每秒钟request／事务 数量 &nbsp; <br />并发请求数： 系统同时处理的request／事务数 &nbsp; <br />响应时间： &nbsp;一般取平均响应时间<br />线程数<br /><br /><br />平均响应时间 = 总时间 / 并发请求数<br />tps = 并发数 / 平均响应时间<br /><br />线程增加 *10 , &nbsp;TPS 也 *10 ，岁线程增加而增加，当到达系统最大处理能力时，平均响应时间增加 ， TPS下降<br /><p style="padding: 0px; margin: 1.2em 0px; line-height: 1.5em; color: #002200; font-family: georgia; font-size: 15.3333330154419px;">并发数、QPS、平均响应时间三者之间关系</p><p style="padding: 0px; margin: 1.2em 0px; line-height: 1.5em; color: #002200; font-family: georgia; font-size: 15.3333330154419px;"><img title="系统吞吐量评估方法" src="http://www.ha97.com/wp-content/uploads/image/2012/08/073720Lwr.jpg" alt="系统吞吐量评估方法" width="500" height="314" style="padding: 3px; margin: 5px 0px; border: 1px solid #ced4ca;" /></p></div><img src ="http://www.blogjava.net/masen/aggbug/423326.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/masen/" target="_blank">Masen</a> 2015-03-09 21:15 <a href="http://www.blogjava.net/masen/articles/423326.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Junit4 六点</title><link>http://www.blogjava.net/masen/articles/112018.html</link><dc:creator>Masen</dc:creator><author>Masen</author><pubDate>Thu, 19 Apr 2007 10:48:00 GMT</pubDate><guid>http://www.blogjava.net/masen/articles/112018.html</guid><wfw:comment>http://www.blogjava.net/masen/comments/112018.html</wfw:comment><comments>http://www.blogjava.net/masen/articles/112018.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/masen/comments/commentRss/112018.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/masen/services/trackbacks/112018.html</trackback:ping><description><![CDATA[一、&nbsp;&nbsp;&nbsp;&nbsp; &lt;!--[endif]--&gt;高级Fixture
<p style="FONT-SIZE: 12pt" align=left>上一篇文章中我们介绍了两个Fixture标注，分别是@Before和@After，我们来看看他们是否适合完成如下功能：有一个类是负责对大文件（超过500兆）进行读写，他的每一个方法都是对文件进行操作。换句话说，在调用每一个方法之前，我们都要打开一个大文件并读入文件内容，这绝对是一个非常耗费时间的操作。如果我们使用@Before和@After，那么每次测试都要读取一次文件，效率及其低下。这里我们所希望的是在所有测试一开始读一次文件，所有测试结束之后释放文件，而不是每次测试都读文件。JUnit的作者显然也考虑到了这个问题，它给出了@BeforeClass 和 @AfterClass两个Fixture来帮我们实现这个功能。从名字上就可以看出，用这两个Fixture标注的函数，只在测试用例初始化时执行@BeforeClass方法，当所有测试执行完毕之后，执行@AfterClass进行收尾工作。在这里要注意一下，每个测试类只能有一个方法被标注为@BeforeClass 或 @AfterClass，并且该方法必须是Public和Static的。</p>
<p style="FONT-SIZE: 12pt" align=left>&lt;!--[if !supportLists]--&gt;二、&nbsp;&nbsp;&nbsp;&nbsp; &lt;!--[endif]--&gt;限时测试。</p>
<p style="FONT-SIZE: 12pt" align=left>还记得我在初级篇中给出的例子吗，那个求平方根的函数有Bug，是个死循环：</p>
<p style="FONT-SIZE: 12pt" align=left></p>
<div style="FONT-SIZE: 12pt">
<div style="FONT-SIZE: 12pt"><img id=_34_90_Open_Image onclick="this.style.display='none'; document.getElementById('_34_90_Open_Text').style.display='none'; document.getElementById('_34_90_Closed_Image').style.display='inline'; document.getElementById('_34_90_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=_34_90_Closed_Image onclick="this.style.display='none'; document.getElementById('_34_90_Closed_Text').style.display='none'; document.getElementById('_34_90_Open_Image').style.display='inline'; document.getElementById('_34_90_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;squareRoot(int&nbsp;n)&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(;&nbsp;;)&nbsp;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Bug&nbsp;:&nbsp;死循环<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top></div>
</div>
<p style="FONT-SIZE: 12pt" align=left></p>
<p style="FONT-SIZE: 12pt" align=left>如果测试的时候遇到死循环，你的脸上绝对不会露出笑容。因此，对于那些逻辑很复杂，循环嵌套比较深的程序，很有可能出现死循环，因此一定要采取一些预防措施。限时测试是一个很好的解决方案。我们给这些测试函数设定一个执行时间，超过了这个时间，他们就会被系统强行终止，并且系统还会向你汇报该函数结束的原因是因为超时，这样你就可以发现这些Bug了。要实现这一功能，只需要给@Test标注加一个参数即可，代码如下：</p>
<pre></pre>
<div style="FONT-SIZE: 12pt">
<div style="FONT-SIZE: 12pt"><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;@Test(timeout&nbsp;=&nbsp;1000)<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img id=_56_151_Open_Image onclick="this.style.display='none'; document.getElementById('_56_151_Open_Text').style.display='none'; document.getElementById('_56_151_Closed_Image').style.display='inline'; document.getElementById('_56_151_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=_56_151_Closed_Image onclick="this.style.display='none'; document.getElementById('_56_151_Closed_Text').style.display='none'; document.getElementById('_56_151_Open_Image').style.display='inline'; document.getElementById('_56_151_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;squareRoot()&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calculator.squareRoot(4);<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assertEquals(2,&nbsp;calculator.getResult());<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top></div>
</div>
<pre></pre>
<p style="FONT-SIZE: 12pt" align=left>Timeout参数表明了你要设定的时间，单位为毫秒，因此1000就代表1秒。</p>
<p style="FONT-SIZE: 12pt" align=left>&lt;!--[if !supportLists]--&gt;三、&nbsp;&nbsp;&nbsp;&nbsp; &lt;!--[endif]--&gt;测试异常</p>
<p style="FONT-SIZE: 12pt" align=left>JAVA中的异常处理也是一个重点，因此你经常会编写一些需要抛出异常的函数。那么，如果你觉得一个函数应该抛出异常，但是它没抛出，这算不算Bug呢？这当然是Bug，并JUnit也考虑到了这一点，来帮助我们找到这种Bug。例如，我们写的计算器类有除法功能，如果除数是一个0，那么必然要抛出&#8220;除0异常&#8221;。因此，我们很有必要对这些进行测试。代码如下：</p>
<pre></pre>
<div style="FONT-SIZE: 12pt">
<div style="FONT-SIZE: 12pt"><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;@Test(expected&nbsp;=&nbsp;ArithmeticException.class)<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img id=_76_105_Open_Image onclick="this.style.display='none'; document.getElementById('_76_105_Open_Text').style.display='none'; document.getElementById('_76_105_Closed_Image').style.display='inline'; document.getElementById('_76_105_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=_76_105_Closed_Image onclick="this.style.display='none'; document.getElementById('_76_105_Closed_Text').style.display='none'; document.getElementById('_76_105_Open_Image').style.display='inline'; document.getElementById('_76_105_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;public&nbsp;void&nbsp;divideByZero()&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>calculator.divide(0);&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top></div>
</div>
<pre></pre>
<pre>如上述代码所示，我们需要使用@Test标注的expected属性，将我们要检验的异常传递给他，这样JUnit框架就能自动帮我们检测是否抛出了我们指定的异常。</pre>
<p style="FONT-SIZE: 12pt" align=left>&lt;!--[if !supportLists]--&gt;&lt;!--[endif]--&gt;四、&nbsp;&nbsp;&nbsp;&nbsp; Runner (运行器)</p>
<p style="FONT-SIZE: 12pt" align=left>大家有没有想过这个问题，当你把测试代码提交给JUnit框架后，框架如何来运行你的代码呢？答案就是——Runner。在JUnit中有很多个Runner，他们负责调用你的测试代码，每一个Runner都有各自的特殊功能，你要根据需要选择不同的Runner来运行你的测试代码。可能你会觉得奇怪，前面我们写了那么多测试，并没有明确指定一个Runner啊？这是因为JUnit中有一个默认Runner，如果你没有指定，那么系统自动使用默认Runner来运行你的代码。换句话说，下面两段代码含义是完全一样的：</p>
<pre></pre>
<div style="FONT-SIZE: 12pt">
<div style="FONT-SIZE: 12pt"><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;org.junit.internal.runners.TestClassRunner;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;org.junit.runner.RunWith;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>//使用了系统默认的TestClassRunner，与下面代码完全一样<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img id=_154_162_Open_Image onclick="this.style.display='none'; document.getElementById('_154_162_Open_Text').style.display='none'; document.getElementById('_154_162_Closed_Image').style.display='inline'; document.getElementById('_154_162_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=_154_162_Closed_Image onclick="this.style.display='none'; document.getElementById('_154_162_Closed_Text').style.display='none'; document.getElementById('_154_162_Open_Image').style.display='inline'; document.getElementById('_154_162_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align=top>public&nbsp;class&nbsp;CalculatorTest&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>...<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>@RunWith(TestClassRunner.class)<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img id=_232_240_Open_Image onclick="this.style.display='none'; document.getElementById('_232_240_Open_Text').style.display='none'; document.getElementById('_232_240_Closed_Image').style.display='inline'; document.getElementById('_232_240_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=_232_240_Closed_Image onclick="this.style.display='none'; document.getElementById('_232_240_Closed_Text').style.display='none'; document.getElementById('_232_240_Open_Image').style.display='inline'; document.getElementById('_232_240_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align=top>public&nbsp;class&nbsp;CalculatorTest&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>...<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top></div>
</div>
<pre></pre>
<p style="FONT-SIZE: 12pt" align=left>从上述例子可以看出，要想指定一个Runner，需要使用@RunWith标注，并且把你所指定的Runner作为参数传递给它。另外一个要注意的是，@RunWith是用来修饰类的，而不是用来修饰函数的。只要对一个类指定了Runner，那么这个类中的所有函数都被这个Runner来调用。最后，不要忘了包含相应的Package哦，上面的例子对这一点写的很清楚了。接下来，我会向你们展示其他Runner的特有功能。</p>
<p style="FONT-SIZE: 12pt" align=left>&lt;!--[if !supportLists]--&gt;五、&nbsp;&nbsp;&nbsp;&nbsp; &lt;!--[endif]--&gt;参数化测试。</p>
<p style="FONT-SIZE: 12pt" align=left>你可能遇到过这样的函数，它的参数有许多特殊值，或者说他的参数分为很多个区域。比如，一个对考试分数进行评价的函数，返回值分别为&#8220;优秀，良好，一般，及格，不及格&#8221;，因此你在编写测试的时候，至少要写5个测试，把这5中情况都包含了，这确实是一件很麻烦的事情。我们还使用我们先前的例子，测试一下&#8220;计算一个数的平方&#8221;这个函数，暂且分三类：正数、0、负数。测试代码如下：</p>
<pre></pre>
<div style="FONT-SIZE: 12pt">
<div style="FONT-SIZE: 12pt"><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;org.junit.AfterClass;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;org.junit.Before;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;org.junit.BeforeClass;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;org.junit.Test;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;static&nbsp;org.junit.Assert.*;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img id=_175_721_Open_Image onclick="this.style.display='none'; document.getElementById('_175_721_Open_Text').style.display='none'; document.getElementById('_175_721_Closed_Image').style.display='inline'; document.getElementById('_175_721_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=_175_721_Closed_Image onclick="this.style.display='none'; document.getElementById('_175_721_Closed_Text').style.display='none'; document.getElementById('_175_721_Open_Image').style.display='inline'; document.getElementById('_175_721_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align=top>public&nbsp;class&nbsp;AdvancedTest&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>private&nbsp;static&nbsp;Calculator&nbsp;calculator&nbsp;＝&nbsp;new&nbsp;Calculator();<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;@Before<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img id=_285_321_Open_Image onclick="this.style.display='none'; document.getElementById('_285_321_Open_Text').style.display='none'; document.getElementById('_285_321_Closed_Image').style.display='inline'; document.getElementById('_285_321_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=_285_321_Closed_Image onclick="this.style.display='none'; document.getElementById('_285_321_Closed_Text').style.display='none'; document.getElementById('_285_321_Open_Image').style.display='inline'; document.getElementById('_285_321_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align=top>public&nbsp;void&nbsp;clearCalculator()&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calculator.clear();<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;@Test<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img id=_364_452_Open_Image onclick="this.style.display='none'; document.getElementById('_364_452_Open_Text').style.display='none'; document.getElementById('_364_452_Closed_Image').style.display='inline'; document.getElementById('_364_452_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=_364_452_Closed_Image onclick="this.style.display='none'; document.getElementById('_364_452_Closed_Text').style.display='none'; document.getElementById('_364_452_Open_Image').style.display='inline'; document.getElementById('_364_452_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;square1()&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calculator.square(2);<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assertEquals(4,&nbsp;calculator.getResult());<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;@Test<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img id=_495_583_Open_Image onclick="this.style.display='none'; document.getElementById('_495_583_Open_Text').style.display='none'; document.getElementById('_495_583_Closed_Image').style.display='inline'; document.getElementById('_495_583_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=_495_583_Closed_Image onclick="this.style.display='none'; document.getElementById('_495_583_Closed_Text').style.display='none'; document.getElementById('_495_583_Open_Image').style.display='inline'; document.getElementById('_495_583_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;square2()&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calculator.square(0);<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assertEquals(0,&nbsp;calculator.getResult());<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;@Test<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img id=_626_715_Open_Image onclick="this.style.display='none'; document.getElementById('_626_715_Open_Text').style.display='none'; document.getElementById('_626_715_Closed_Image').style.display='inline'; document.getElementById('_626_715_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=_626_715_Closed_Image onclick="this.style.display='none'; document.getElementById('_626_715_Closed_Text').style.display='none'; document.getElementById('_626_715_Open_Image').style.display='inline'; document.getElementById('_626_715_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;square3()&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calculator.square(-3);<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assertEquals(9,&nbsp;calculator.getResult());<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top></div>
</div>
<pre></pre>
<p style="FONT-SIZE: 12pt" align=left>为了简化类似的测试，JUnit4提出了&#8220;参数化测试&#8221;的概念，只写一个测试函数，把这若干种情况作为参数传递进去，一次性的完成测试。代码如下：</p>
<pre></pre>
<div style="FONT-SIZE: 12pt">
<div style="FONT-SIZE: 12pt"><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;static&nbsp;org.junit.Assert.assertEquals;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;org.junit.Test;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;org.junit.runner.RunWith;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;org.junit.runners.Parameterized;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;org.junit.runners.Parameterized.Parameters;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;java.util.Arrays;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;java.util.Collection;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>@RunWith(Parameterized.class)<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img id=_314_912_Open_Image onclick="this.style.display='none'; document.getElementById('_314_912_Open_Text').style.display='none'; document.getElementById('_314_912_Closed_Image').style.display='inline'; document.getElementById('_314_912_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=_314_912_Closed_Image onclick="this.style.display='none'; document.getElementById('_314_912_Closed_Text').style.display='none'; document.getElementById('_314_912_Open_Image').style.display='inline'; document.getElementById('_314_912_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align=top>public&nbsp;class&nbsp;SquareTest&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;Calculator&nbsp;calculator&nbsp;=&nbsp;new&nbsp;Calculator();<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;int&nbsp;param;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;int&nbsp;result;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;@Parameters<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img id=_487_629_Open_Image onclick="this.style.display='none'; document.getElementById('_487_629_Open_Text').style.display='none'; document.getElementById('_487_629_Closed_Image').style.display='inline'; document.getElementById('_487_629_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=_487_629_Closed_Image onclick="this.style.display='none'; document.getElementById('_487_629_Closed_Text').style.display='none'; document.getElementById('_487_629_Open_Image').style.display='inline'; document.getElementById('_487_629_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;Collection&nbsp;data()&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img id=_533_620_Open_Image onclick="this.style.display='none'; document.getElementById('_533_620_Open_Text').style.display='none'; document.getElementById('_533_620_Closed_Image').style.display='inline'; document.getElementById('_533_620_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=_533_620_Closed_Image onclick="this.style.display='none'; document.getElementById('_533_620_Closed_Text').style.display='none'; document.getElementById('_533_620_Open_Image').style.display='inline'; document.getElementById('_533_620_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;Arrays.asList(new&nbsp;Object[][]...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img id=_552_557_Open_Image onclick="this.style.display='none'; document.getElementById('_552_557_Open_Text').style.display='none'; document.getElementById('_552_557_Closed_Image').style.display='inline'; document.getElementById('_552_557_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=_552_557_Closed_Image onclick="this.style.display='none'; document.getElementById('_552_557_Closed_Text').style.display='none'; document.getElementById('_552_557_Open_Image').style.display='inline'; document.getElementById('_552_557_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...{2,&nbsp;4},<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img id=_577_582_Open_Image onclick="this.style.display='none'; document.getElementById('_577_582_Open_Text').style.display='none'; document.getElementById('_577_582_Closed_Image').style.display='inline'; document.getElementById('_577_582_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=_577_582_Closed_Image onclick="this.style.display='none'; document.getElementById('_577_582_Closed_Text').style.display='none'; document.getElementById('_577_582_Open_Image').style.display='inline'; document.getElementById('_577_582_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...{0,&nbsp;0},<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img id=_602_608_Open_Image onclick="this.style.display='none'; document.getElementById('_602_608_Open_Text').style.display='none'; document.getElementById('_602_608_Closed_Image').style.display='inline'; document.getElementById('_602_608_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=_602_608_Closed_Image onclick="this.style.display='none'; document.getElementById('_602_608_Closed_Text').style.display='none'; document.getElementById('_602_608_Open_Image').style.display='inline'; document.getElementById('_602_608_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...{－3,&nbsp;9},<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>//构造函数，对变量进行初始化<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img id=_700_767_Open_Image onclick="this.style.display='none'; document.getElementById('_700_767_Open_Text').style.display='none'; document.getElementById('_700_767_Closed_Image').style.display='inline'; document.getElementById('_700_767_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=_700_767_Closed_Image onclick="this.style.display='none'; document.getElementById('_700_767_Closed_Text').style.display='none'; document.getElementById('_700_767_Open_Image').style.display='inline'; document.getElementById('_700_767_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;SquareTest(int&nbsp;param,&nbsp;int&nbsp;result)&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.param&nbsp;=&nbsp;param;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.result&nbsp;=&nbsp;result;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;@Test<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img id=_809_906_Open_Image onclick="this.style.display='none'; document.getElementById('_809_906_Open_Text').style.display='none'; document.getElementById('_809_906_Closed_Image').style.display='inline'; document.getElementById('_809_906_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=_809_906_Closed_Image onclick="this.style.display='none'; document.getElementById('_809_906_Closed_Text').style.display='none'; document.getElementById('_809_906_Open_Image').style.display='inline'; document.getElementById('_809_906_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;square()&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calculator.square(param);<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assertEquals(result,&nbsp;calculator.getResult());<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top></div>
</div>
<pre></pre>
<pre>下面我们对上述代码进行分析。首先，你要为这种测试专门生成一个新的类，而不能与其他测试共用同一个类，此例中我们定义了一个SquareTest类。然后，<br>你要为这个类指定一个Runner，而不能使用默认的Runner了，因为特殊的功能要用特殊的Runner嘛。@RunWith(Parameterized.class)这条语句就是为这个类指<br>定了一个ParameterizedRunner。第二步，定义一个待测试的类，并且定义两个变量，一个用于存放参数，一个用于存放期待的结果。接下来，定义测试数据的集合，<br>也就是上述的data()方法，该方法可以任意命名，但是必须使用@Parameters标注进行修饰。这个方法的框架就不予解释了，大家只需要注意其中的数据，是一个二<br>维数组，数据两两一组，每组中的这两个数据，一个是参数，一个是你预期的结果。比如我们的第一组{2, 4}，2就是参数，4就是预期的结果。这两个数据的顺序无所<br>谓，谁前谁后都可以。之后是构造函数，其功能就是对先前定义的两个参数进行初始化。在这里你可要注意一下参数的顺序了，要和上面的数据集合的顺序保持一致。<br>如果前面的顺序是{参数，期待的结果}，那么你构造函数的顺序也要是&#8220;构造函数(参数， 期待的结果)&#8221;，反之亦然。最后就是写一个简单的测试例了，和前面介绍<br>过的写法完全一样，在此就不多说。</pre>
<p style="FONT-SIZE: 12pt" align=left>&lt;!--[if !supportLists]--&gt;六、&nbsp;&nbsp;&nbsp;&nbsp; &lt;!--[endif]--&gt;打包测试。</p>
<p style="FONT-SIZE: 12pt" align=left>通过前面的介绍我们可以感觉到，在一个项目中，只写一个测试类是不可能的，我们会写出很多很多个测试类。可是这些测试类必须一个一个的执行，也是比较麻烦的事情。鉴于此，JUnit为我们提供了打包测试的功能，将所有需要运行的测试类集中起来，一次性的运行完毕，大大的方便了我们的测试工作。具体代码如下：</p>
<p style="FONT-SIZE: 12pt" align=left></p>
<div style="FONT-SIZE: 12pt">
<div style="FONT-SIZE: 12pt"><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;org.junit.runner.RunWith;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>import&nbsp;org.junit.runners.Suite;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>@RunWith(Suite.class)<br><img id=_108_173_Open_Image onclick="this.style.display='none'; document.getElementById('_108_173_Open_Text').style.display='none'; document.getElementById('_108_173_Closed_Image').style.display='inline'; document.getElementById('_108_173_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=_108_173_Closed_Image onclick="this.style.display='none'; document.getElementById('_108_173_Closed_Text').style.display='none'; document.getElementById('_108_173_Open_Image').style.display='inline'; document.getElementById('_108_173_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align=top>@Suite.SuiteClasses(...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CalculatorTest.class,<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SquareTest.class<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})<br><img id=_208_210_Open_Image onclick="this.style.display='none'; document.getElementById('_208_210_Open_Text').style.display='none'; document.getElementById('_208_210_Closed_Image').style.display='inline'; document.getElementById('_208_210_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=_208_210_Closed_Image onclick="this.style.display='none'; document.getElementById('_208_210_Closed_Text').style.display='none'; document.getElementById('_208_210_Open_Image').style.display='inline'; document.getElementById('_208_210_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align=top>public&nbsp;class&nbsp;AllCalculatorTests&nbsp;...{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top></div>
</div>
<p style="FONT-SIZE: 12pt">&nbsp;</p>
<p style="FONT-SIZE: 12pt" align=left>大家可以看到，这个功能也需要使用一个特殊的Runner，因此我们需要向@RunWith标注传递一个参数Suite.class。同时，我们还需要另外一个标注@Suite.SuiteClasses，来表明这个类是一个打包测试类。我们把需要打包的类作为参数传递给该标注就可以了。有了这两个标注之后，就已经完整的表达了所有的含义，因此下面的类已经无关紧要，随便起一个类名，内容全部为空既可。</p>
<img src ="http://www.blogjava.net/masen/aggbug/112018.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/masen/" target="_blank">Masen</a> 2007-04-19 18:48 <a href="http://www.blogjava.net/masen/articles/112018.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>