tag:blogger.com,1999:blog-35071033606157405042024-02-20T21:43:44.794+05:30API/Unit Testing using JunitThis blog mainly focuses on the Unit or API or Functional testing done using junit framework.Sumesh Badgehttp://www.blogger.com/profile/17770819183107036741noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-3507103360615740504.post-21565082802916200552012-10-14T01:07:00.003+05:302012-10-14T01:07:53.997+05:30Handling multiple failures in single test in Junit 4<div dir="ltr" style="text-align: left;" trbidi="on">
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount="156">
</w:LatentStyles>
</xml><![endif]--><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin:0in;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
</style>
<![endif]-->
<br />
<div class="MsoNormal">
<span style="mso-bidi-font-size: 10.0pt;">In my <a href="http://apitestingwithjunit.blogspot.in/2012/07/junit-3-handling-multiple-failures-in.html">last post</a> we
have seen handling multiple assertion failures in single test using junit 3.x.
In this post we’ll see how to overcome the same problem but in junit 4 style.</span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="mso-bidi-font-size: 10.0pt;">Junit 4 addresses
the problem of handling multiple assertion failures pretty well. It has an
ErrorCollector class that collects all failures in a test case and reports them
at the end of test case. The trick lies in usage of error collector which is
also not much difficult.</span></div>
<div class="MsoNormal">
<span style="mso-bidi-font-size: 10.0pt;">All you have to do
is follow the below mentioned changes and update your base class accordingly.</span></div>
<div class="MsoNormal">
<br /></div>
<ol start="1" style="margin-top: 0in;" type="1">
<li class="MsoNormal" style="mso-list: l1 level1 lfo1; tab-stops: list .5in;"><span style="mso-bidi-font-size: 10.0pt;">Let’s assume you have a Base class which
is extended by all your test classes. In my example it’s the
TestBase.java. In the base class create an ErrorCollector object and (most
important) add </span><span style="background: silver; color: #646464; font-family: "Courier New"; font-size: 10.0pt; mso-highlight: silver;">@Rule</span><span style="color: #646464; font-family: "Courier New"; font-size: 10.0pt;"> </span><span style="mso-bidi-font-size: 10.0pt;">annotation to it.</span></li>
<li class="MsoNormal" style="mso-list: l1 level1 lfo1; tab-stops: list .5in;"><span style="mso-bidi-font-size: 10.0pt;">Now create a Boolean variable “fatal”
and add setter and getter methods for it. This will help you specify which
test cases you want to make fatal and which you don’t.</span></li>
<li class="MsoNormal" style="mso-list: l1 level1 lfo1; tab-stops: list .5in;"><span style="mso-bidi-font-size: 10.0pt;">Create an assertEquals(String, Object,
Object) method with public access. We will be using this method in all our
test classes that will extend the TestBase.java. This method will be the
decision maker on how the test execution should go ahead (whether fatal or
non-fatal).</span></li>
</ol>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="mso-bidi-font-size: 10.0pt;">Your code should
look like the one mentioned below:</span></div>
<div class="MsoNormal">
<br /></div>
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount="156">
</w:LatentStyles>
</xml><![endif]--><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin:0in;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
</style>
<![endif]-->
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">public</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> </span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">class</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> TestBase {</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><span style="color: #646464; font-family: "Courier New"; font-size: 10.0pt;">@Rule</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">public</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> ErrorCollector </span><span style="color: #0000c0; font-family: "Courier New"; font-size: 10.0pt;">collector</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> = </span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">new</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">
ErrorCollector();</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">private</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> </span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">boolean</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> </span><span style="color: #0000c0; font-family: "Courier New"; font-size: 10.0pt;">fatal</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">;</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">public</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> TestBase() {</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span></span><span style="color: #0000c0; font-family: "Courier New"; font-size: 10.0pt;">fatal</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">=</span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">true</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">;</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span>}</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">public</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> </span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">void</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">
assertEquals(String msg, Object expected, Object actual) {</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span></span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">if</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">(getFatal()) {</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 3;"> </span>Assert.<i>assertEquals</i>(msg,expected,
actual);</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span>} </span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">else</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> {</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 3;"> </span></span><span style="color: #0000c0; font-family: "Courier New"; font-size: 10.0pt;">collector</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">.checkThat(msg,
actual, CoreMatchers.<i>equalTo</i>(expected));</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span>}</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span>}</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">public</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> </span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">void</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> setFatal(</span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">boolean</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> fatalFlag) {</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span></span><span style="color: #0000c0; font-family: "Courier New"; font-size: 10.0pt;">fatal</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> = fatalFlag;</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span>}</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">public</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> </span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">boolean</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> getFatal() {</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span></span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">return</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> </span><span style="color: #0000c0; font-family: "Courier New"; font-size: 10.0pt;">fatal</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">;</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span>}</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">}</span><span style="mso-bidi-font-size: 10.0pt;"></span></div>
<span style="background: silver; color: black; font-family: "Courier New"; font-size: 10.0pt; mso-highlight: silver;"></span><br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="mso-bidi-font-size: 10.0pt;">That’s all!!!</span></div>
<div class="MsoNormal">
<span style="mso-bidi-font-size: 10.0pt;">You are done with
all the implementation part. Now, all you have to do is to use it, and that is
demonstrated below. The test class TestSample.java extends the TestBase.java to
make use of the code implemented in it.</span></div>
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount="156">
</w:LatentStyles>
</xml><![endif]--><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin:0in;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
</style>
<![endif]-->
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">public</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> </span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">class</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> TestSample </span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">extends</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> TestBase{</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><span style="color: #646464; font-family: "Courier New"; font-size: 10.0pt;">@Test</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">public</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> </span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">void</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> one() {</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span>assertEquals(</span><span style="color: #2a00ff; font-family: "Courier New"; font-size: 10.0pt;">"Comparing
1 and 2"</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">, 1, 2);</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span>assertEquals(</span><span style="color: #2a00ff; font-family: "Courier New"; font-size: 10.0pt;">"Comparing
A and B"</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">, </span><span style="color: #2a00ff; font-family: "Courier New"; font-size: 10.0pt;">"A"</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">, </span><span style="color: #2a00ff; font-family: "Courier New"; font-size: 10.0pt;">"B"</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">);</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span>assertEquals(</span><span style="color: #2a00ff; font-family: "Courier New"; font-size: 10.0pt;">"Comparing
10.2 and 10.2"</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">, 10.2, 10.2);</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span>}</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><span style="color: #646464; font-family: "Courier New"; font-size: 10.0pt;">@Test</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">public</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> </span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">void</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"> two() {</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span>setFatal(</span><b><span style="color: #7f0055; font-family: "Courier New"; font-size: 10.0pt;">false</span></b><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">);</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span>assertEquals(</span><span style="color: #2a00ff; font-family: "Courier New"; font-size: 10.0pt;">"Comparing
1 and 2"</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">, 1, 2);</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span>assertEquals(</span><span style="color: #2a00ff; font-family: "Courier New"; font-size: 10.0pt;">"Comparing
A and B"</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">, </span><span style="color: #2a00ff; font-family: "Courier New"; font-size: 10.0pt;">"A"</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">, </span><span style="color: #2a00ff; font-family: "Courier New"; font-size: 10.0pt;">"B"</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">);</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span>assertEquals(</span><span style="color: #2a00ff; font-family: "Courier New"; font-size: 10.0pt;">"Comparing
10.2 and 10.2"</span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">, 10.2, 10.2);</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span>}</span><span style="font-family: "Courier New"; font-size: 10.0pt;"></span></div>
<div class="MsoNormal">
<span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">}</span></div>
<span style="background: none repeat scroll 0% 0% rgb(255, 255, 255); color: black; font-family: "Courier New"; font-size: 10pt;"></span><br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="mso-bidi-font-size: 10.0pt;">The output will be:
Runs 2; Failures:3; Error:0</span></div>
<ol start="1" style="margin-top: 0in;" type="1">
<li class="MsoNormal" style="mso-list: l0 level1 lfo2; tab-stops: list .5in;"><span style="mso-bidi-font-size: 10.0pt;">For test case “one”:</span><br />
<span style="mso-bidi-font-size: 10.0pt;">java.lang.AssertionError: Comparing 1 and 2
expected:<1> but was:<2></span></li>
</ol>
<div class="MsoNormal" style="margin-left: .25in; text-indent: .25in;">
<br /></div>
<ol start="2" style="margin-top: 0in;" type="1">
<li class="MsoNormal" style="mso-list: l0 level1 lfo2; tab-stops: list .5in;"><span style="mso-bidi-font-size: 10.0pt;">For test case “two”: </span><br />
<span style="mso-bidi-font-size: 10.0pt;">java.lang.AssertionError: Comparing 1 and 2</span><br />
<span style="mso-bidi-font-size: 10.0pt;">Expected: <1><span style="mso-spacerun: yes;"> </span></span><br />
<span style="mso-bidi-font-size: 10.0pt;"><span style="mso-spacerun: yes;"> </span>got: <2></span></li>
</ol>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="mso-bidi-font-size: 10.0pt;">java.lang.AssertionError: Comparing A and B</span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="mso-bidi-font-size: 10.0pt;">Expected: "A"</span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="mso-bidi-font-size: 10.0pt;"><span style="mso-spacerun: yes;"> </span>got: "B"</span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<br /></div>
<div class="MsoNormal">
<span style="mso-bidi-font-size: 10.0pt;">As you can see that
for test case “one” which is by default fatal (default junit behabiour), the
test case exited after the first assertion failure.</span></div>
<div class="MsoNormal">
<span style="mso-bidi-font-size: 10.0pt;">Whereas, for test
case “two” which is explicitly marked as non-fatal by setting the fatal flag to
false, all the assertions within the test case were executed even though the
first assertion failed.</span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="mso-bidi-font-size: 10.0pt;">So, by doing few
changes in your base class of your test framework, you can add an important
feature of handling multiple assertion failures to your framework.</span></div>
<div class="MsoNormal">
<span style="mso-bidi-font-size: 10.0pt;">That’s all for this
post.</span></div>
<div class="MsoNormal">
<span style="mso-bidi-font-size: 10.0pt;">I’ll keep posting up
some interesting junit problems on this blog, as and when I encounter (and
solve) them.</span></div>
<div class="MsoNormal">
<span style="mso-bidi-font-size: 10.0pt;">Feel free to comment
if you like the post or have any good suggestions.</span></div>
</div>
Sumesh Badgehttp://www.blogger.com/profile/17770819183107036741noreply@blogger.com1tag:blogger.com,1999:blog-3507103360615740504.post-71773839127061016072012-07-22T11:11:00.000+05:302012-07-25T12:21:59.042+05:30Junit 3 - Handling multiple failures in single test<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">When
testers use junit framework for functional testing, one feature they desperately need is
to continue the test execution even when one or more assertions fail in a test
case.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">If you are
using junit 3 framework for functional testing then you can use this feature by
using “Jfunc” jar.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">By default
in junit all the tests are fatal i.e. when an assertion fails in a test case,
the code following that assertion is not executed. But Jfunc gives you a choice
to control this; you can either continue to execute all the code of your test-case
even when an assert fails, or to stop it after the first failure itself.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<h3 style="text-align: left;">
<span style="font-family: Arial; font-size: 16pt;">Changes to
your framework to use Jfunc.jar</span></h3>
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">To start
using Jfunc, all you need to do is to extend your Test Class to </span><span style="background-color: #cccccc; font-family: 'Courier New'; font-size: 10pt;">junit.extensions.jfunc.JFuncTestCase</span><span style="font-family: Arial; font-size: 10pt;"> base class instead of </span><span style="background-color: #cccccc; font-family: 'Courier New'; font-size: 10pt;">junit.framework.TestCase</span><span style="font-family: Arial; font-size: 10pt;">, and that’s all. Rest everything (constructor
initialization and suite method) remains the same as you do it in Junit
framework.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<h3 style="text-align: left;">
<span style="font-family: Arial; font-size: 16pt;">Making a
test non-fatal</span></h3>
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">Now comes the
main part; how to make a test execution non-fatal.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">This is
also very simple, all you have to do is, just set the fatal flag </span><span style="font-family: Arial; font-size: 10pt;">to false </span><span style="font-family: Arial; font-size: 10pt;">(</span><span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;">setFatal(</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">false</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">)</span></span><span style="font-family: Arial; font-size: 10pt;"><span style="background-color: #cccccc;">)</span> in your test class’s constructor and all the tests in your test class
will be non-fatal.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">An example
of using Jfunc in your test class is mentioned below.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">public</span></b><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">class</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">
TestClass </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">extends</span></b><span style="font-family: 'Courier New'; font-size: 10pt;"> JFuncTestCase { </span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">public</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">
TestClass(String name) {</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">super</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">(name);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> setFatal(</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">false</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">); </span><b><span style="font-family: 'Courier New'; font-size: 16pt;"> </span></b><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> }</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">public</span></b><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">static</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">
Test suite() {</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">return</span></b><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">new</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">
TestSuite(TestClass.</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">class</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> }</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">public</span></b><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">void</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">
test1() {</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> fail(</span><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt;">"test1: fail 1"</span><span style="font-family: 'Courier New'; font-size: 10pt;">);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> fail(</span><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt;">"test1: fail 2"</span><span style="font-family: 'Courier New'; font-size: 10pt;">);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> fail(</span><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt;">"test1: fail 3"</span><span style="font-family: 'Courier New'; font-size: 10pt;">);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> }</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">public</span></b><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">void</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">
test2() {</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> fail(</span><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt;">"test2: fail 1"</span><span style="font-family: 'Courier New'; font-size: 10pt;">);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> fail(</span><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt;">"test2: fail 2"</span><span style="font-family: 'Courier New'; font-size: 10pt;">);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> fail(</span><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt;">"test2: fail 3"</span><span style="font-family: 'Courier New'; font-size: 10pt;">);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> }</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc; font-family: 'Courier New'; font-size: 10pt;">}</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">The best part of using Jfunc’s fatal
feature is that you can set it at test case level also. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">For example, in the above code if I
want to make only test1 as non-fatal (execute all fail() methods) and I want
that test2 should be fatal (stop execution after first fail() method).<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">Then all I have to do is to remove
the </span><span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;">setFatal(</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">false</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">)</span></span><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><span style="font-family: Arial; font-size: 10pt;">from the constructor and add it at
test case level as shown below.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">public</span></b><span style="font-family: 'Courier New'; font-size: 10pt;"> TestClass(String name) {</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">super</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">(name);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="color: #3f7f5f; font-family: 'Courier New'; font-size: 10pt;">// setFatal(false);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> }</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">public</span></b><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">void</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">
test1() {</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> setFatal(</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">false</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">); </span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> fail(</span><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt;">"test1: fail 1"</span><span style="font-family: 'Courier New'; font-size: 10pt;">);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> fail(</span><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt;">"test1: fail 2"</span><span style="font-family: 'Courier New'; font-size: 10pt;">);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> fail(</span><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt;">"test1: fail 3"</span><span style="font-family: 'Courier New'; font-size: 10pt;">);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> }</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">public</span></b><span style="font-family: 'Courier New'; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt;">void</span></b><span style="font-family: 'Courier New'; font-size: 10pt;">
test2() {</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> fail(</span><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt;">"test2: fail 1"</span><span style="font-family: 'Courier New'; font-size: 10pt;">);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> fail(</span><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt;">"test2: fail 2"</span><span style="font-family: 'Courier New'; font-size: 10pt;">);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc;"><span style="font-family: 'Courier New'; font-size: 10pt;"> fail(</span><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt;">"test2: fail 3"</span><span style="font-family: 'Courier New'; font-size: 10pt;">);</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="background-color: #cccccc; font-family: 'Courier New'; font-size: 10pt;"> }</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">Now, only test1 will have a
non-fatal behavior and all other tests of </span><span style="font-family: 'Courier New'; font-size: 10pt;">TestClass.java</span><span style="font-family: Arial; font-size: 10pt;"> will be fatal.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">The last but not the least; by
default the </span><span style="font-family: 'Courier New'; font-size: 10pt;">fatal</span><span style="font-family: Arial; font-size: 10pt;"> flag
is set to true i.e. if you did not use </span><span style="font-family: 'Courier New'; font-size: 10pt;">setFatal()</span><span style="font-family: Arial; font-size: 10pt;"> method anywhere inside your test class, then, all
your test cases will be fatal (junit style).<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<h3 style="text-align: left;">
<span style="font-family: Arial; font-size: 16pt;">Conclusion:</span></h3>
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">Since the release of junit 4.x
(recent version is 4.10) with a variety of good features (see my <a href="http://apitestingwithjunit.blogspot.in/2012/05/migrating-from-junit-3x-to-junit-4x.html">last post</a> for
junit 4 features), most of the junit testers are planning or had already
migrated to the new junit 4.x framework.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">Jfunc internally uses Junit 3.x, and
does not have support for junit 4.x. So, if you have used jfunc and its fatal
feature in your framework and planning to migrate to junit 4.x, then the
biggest challenge for you is to have the Jfunc’s fatal feature in the 4.x
framework.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Arial; font-size: 10pt;">In my next post we’ll see how we can
achieve this using junit 4.10 framework.<o:p></o:p></span></div>
</div>
Sumesh Badgehttp://www.blogger.com/profile/17770819183107036741noreply@blogger.com0tag:blogger.com,1999:blog-3507103360615740504.post-72631046760488352712012-05-16T12:06:00.000+05:302012-05-16T12:06:53.990+05:30Migrating from Junit 3.x to Junit 4.x<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<br />
Hello and Welcome,</div>
<div class="MsoNormal">
This is my first blog, so thought to start with something I recently did; and it was test framework migration from junit 3.x to junit 4.x.<br />
Junit 4.x has now came up with variety of good features that gives functional or API testers flexibility to write better tests for example; you can put a timeout time for a test case, you can parametrized a test case, etc.</div>
<div class="MsoNormal">
As like other technologies, Junit 4.x is also backward
compatible with its previous versions (i.e. junit 3.x). So a code that is
written in junit 3.x can anytime be executed on junit 4.x as well. But if you
want to use junit 4.x in real sense (and not just a jar that runs 3.x code as
well) I would encourage you to migrate your junit 3.x code to junit 4.x and start using
features (powers) of junit 4.x.</div>
<div class="MsoNormal">
So, following are the steps which you need to follow while
migrating your code from junit 3.x to junit 4.x</div>
<div class="MsoListParagraphCxSpFirst" style="text-indent: -18pt;">
1.<span style="font: 7pt "Times New Roman";"> </span>Remove
all references of junit 3.x jar from the classpath and instead add junit 4.x
jar.</div>
<div class="MsoListParagraphCxSpMiddle">
As I told junit 4.x is backward compatible
with junit 3.x so after adding junit 4.x jar, your code will not throw any
compile error and hence you need to be more careful to make sure that you have
removed all references of junit 3.x.</div>
<div class="MsoListParagraphCxSpMiddle">
At any point if you find “<b><span style="font-family: "Courier New";">junit.framework.X</span></b><b><span style="font-family: "Courier New";">X</span></b><b><span style="font-family: "Courier New";">X</span></b>”
references in your test framework code then go ahead and remove it.</div>
<div class="MsoListParagraphCxSpMiddle" style="text-indent: -18pt;">
2.<span style="font: 7pt "Times New Roman";"> </span>Once
the junit 4.x jar is added, go to your test base class (a class that is
extended by all test suites in your framework). The test base class would be
extending <b><span style="font-family: "Courier New";">junit.framework.TestCase;
</span></b>remove this reference<b><span style="font-family: "Courier New";"> </span></b>(delete<b><span style="font-family: "Courier New";"> extends TestCase </span></b>part).
If you have individual test suites extending <b><span style="font-family: "Courier New";">junit.framework.TestCase </span></b>then
remove it from the individual test suites.</div>
<div class="MsoListParagraphCxSpMiddle">
The point is <b><span style="font-family: "Courier New";">junit.framework.TestCase </span></b>reference
should be removed; nothing in your framework should extend this class.</div>
<div class="MsoListParagraphCxSpMiddle" style="text-indent: -18pt;">
3.<span style="font: 7pt "Times New Roman";"> </span>In
your test suite if you have a suite method, add <span style="background: none repeat scroll 0% 0% silver; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">@BeforeClass</span><span style="color: #646464; font-family: "Courier New"; font-size: 10pt; line-height: 115%;"> </span>annotation above it. Basically, it is good idea to rename the
basic junit 3.x standard methods (like suite(), setup(), teardown(), etc) just
to be more sure that nothing is going junit 3.x way; but if you have taken care in
removing all junit 3.x imports, then its ok to have the same method names.</div>
<div class="MsoListParagraphCxSpMiddle" style="text-indent: -18pt;">
4.<span style="font: 7pt "Times New Roman";"> </span>If
you have a setup() and teardown() methods in your suite and/or test base class,
then add <span style="background: none repeat scroll 0% 0% silver; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">@Before</span><span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%;"> </span>and<span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%;"> <span style="background: none repeat scroll 0% 0% silver;">@After</span> </span>annotations to it respectively.
Again, you can rename these methods as well or keep them as is it is if you are
very sure that all junit 3.x imports are removed.</div>
<div class="MsoListParagraphCxSpMiddle">
The important thing here is that you can
add <span style="background: none repeat scroll 0% 0% silver; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">@BeforeClass</span><span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%;"> </span>to only one method (per class) but you can add<span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%;"> <span style="background: none repeat scroll 0% 0% silver;">@Before</span> </span>and<span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%;"> <span style="background: none repeat scroll 0% 0% silver;">@After</span> </span>annotations
to more than one methods.</div>
<div class="MsoListParagraphCxSpMiddle" style="text-indent: -18pt;">
5.<span style="font: 7pt "Times New Roman";"> </span>All
the asserts in your test class/suite should come from class <b><span style="font-family: "Courier New";">org.junit.Assert.
</span></b>You need to add following statement either to your base class (if
you are overriding junit asserts in your base class) or to the individual test
class/suite if you don’t have any base class in your framework.</div>
<div class="MsoListParagraphCxSpMiddle">
<b><span style="font-family: "Courier New";">import static org.junit.Assert.*;</span></b></div>
<div class="MsoListParagraphCxSpMiddle" style="text-indent: -18pt;">
6.<span style="font: 7pt "Times New Roman";"> </span>Finally,
add <span style="background: none repeat scroll 0% 0% silver; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">@Test</span>
annotation to all your test cases.</div>
<div class="MsoListParagraphCxSpMiddle">
<br /></div>
<div class="MsoListParagraphCxSpMiddle">
<br /></div>
<div class="MsoListParagraphCxSpMiddle">
If you want to add more than one suite to
your test class, then this can be done using <span style="background: none repeat scroll 0% 0% silver; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">@RunWith</span><span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%;"> </span>and<span style="font-family: "Courier New"; font-size: 10pt; line-height: 115%;"> <span style="background: none repeat scroll 0% 0% silver;">@SuiteClasses</span>
</span>annotations.</div>
<div class="MsoListParagraphCxSpMiddle">
For example: if you have a code in junit
3.x style similar to the one mentioned below;</div>
<div class="MsoListParagraphCxSpMiddle">
<br /></div>
<div class="MsoListParagraphCxSpMiddle">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">Class MyTestPackage</span></div>
<div class="MsoListParagraphCxSpMiddle">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">{</span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72pt;">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">public
static Test suite() </span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72pt;">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">{</span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 108pt;">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">TestSuite
suite = new TestSuite();</span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 108pt;">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">suite.addTest(TestOne.suite());</span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 108pt;">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">suite.addTest(TestTwo.suite());</span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 108pt;">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">return
suite;</span></div>
<div class="MsoListParagraphCxSpMiddle" style="text-indent: 36pt;">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">}</span></div>
<div class="MsoListParagraphCxSpMiddle">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">}</span><span style="color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;"></span></div>
<div class="MsoListParagraphCxSpMiddle">
<br /></div>
<div class="MsoListParagraphCxSpMiddle">
then this will be migrated to junit 4.x as </div>
<div class="MsoListParagraphCxSpMiddle">
<br /></div>
<div class="MsoListParagraphCxSpMiddle">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">@RunWith(Suite.class)</span></div>
<div class="MsoListParagraphCxSpMiddle">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">@SuiteClasses({</span></div>
<div class="MsoListParagraphCxSpMiddle">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;"> TestOne.class,</span></div>
<div class="MsoListParagraphCxSpMiddle">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;"> TestTwo.class
})</span></div>
<div class="MsoListParagraphCxSpMiddle">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">Class MyTestPackage</span></div>
<div class="MsoListParagraphCxSpMiddle">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">{</span></div>
<div class="MsoListParagraphCxSpMiddle">
<span style="background: none repeat scroll 0% 0% silver; color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;">}</span><span style="color: black; font-family: "Courier New"; font-size: 10pt; line-height: 115%;"></span></div>
<div class="MsoListParagraphCxSpMiddle">
<br /></div>
<div class="MsoListParagraphCxSpMiddle">
In most of the cases the testers migrating
to junit 4.x will have lots of test classes/suites that need to be refactored
using the above mentioned steps. Sometimes, the count can be in hundreds.</div>
<div class="MsoListParagraphCxSpLast">
So, if you have few test classes in your
framework (may be 10 or 20) you can go ahead and simply apply the above
mentioned changes to those. But if there are large numbers of test
classes/suites in your framework, it’s better to use a junit 3.x to junit 4.x
convertor tool. There are many open source tools that are available on internet
that help in automatically migrating from junit 3.x to junit 4.x.<br />
<br />
In my next post I'll be coming up with a common feature that many junit testers demand, and that is, continue test execution even when one of the asserts fail in the test case and report the failing asserts in the test execution report. We'll see that this can be achieved with both, junit 3.x as well as junit 4.x.</div>
</div>Sumesh Badgehttp://www.blogger.com/profile/17770819183107036741noreply@blogger.com1