<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaBeat &#187; JUnit</title>
	<atom:link href="http://www.javabeat.net/category/junit/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javabeat.net</link>
	<description>Java Technology News</description>
	<lastBuildDate>Wed, 22 May 2013 01:42:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>JUnit 4.0 Example</title>
		<link>http://www.javabeat.net/2008/10/junit-4-0-example/</link>
		<comments>http://www.javabeat.net/2008/10/junit-4-0-example/#comments</comments>
		<pubDate>Fri, 03 Oct 2008 02:25:47 +0000</pubDate>
		<dc:creator>JavaBeat</dc:creator>
				<category><![CDATA[JUnit]]></category>
		<category><![CDATA[JUnit 4.0]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=400</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>JUnit 4.0 introduces a completely different API to the older versions. JUnit 4.0 uses Java 5.0 annotations to describe tests instead of using inheritence. It introduces more flexible initialization and cleanup, timeouts, and parameterized test cases. This post describes the new features in JUnit 4.0, and in the end, I show a basic example that [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><a id="dd_start"></a><p><strong><em>JUnit 4.0</em></strong> introduces a completely different API to the older versions. <strong><em>JUnit 4.0</em></strong> uses <strong><em>Java 5.0 annotations</em></strong> to describe tests instead of using inheritence. It introduces more flexible initialization and cleanup, timeouts, and parameterized test cases. This post describes the new features in <strong><em>JUnit 4.0</em></strong>, and in the end, I show a basic example that tests the <strong><em>java.util.Stack</em></strong> class.</p>
<ul>
<li><a href="http://www.flipkart.com/junit-action-8177225383/p/itmdyn4pgtwes3zy?pid=9788177225389&amp;affid=suthukrish" target="_blank">JUnit in Action</a></li>
<li><a href="http://www.flipkart.com/junit-pocket-guide-100-pages-8173668604/p/itmczzj4ch4htgay?pid=9788173668609&amp;affid=suthukrish" target="_blank">JUnit Pocket Guide</a></li>
</ul>
<p><strong><em>1)The tests:</em></strong> Unlike in <strong><em>JUnit 3.x</em></strong> you don&#8217;t have to extend TestCase to implement tests. A simple Java class can be used as a <strong><em>TestCase</em></strong>. The test methods have to be simply annotated with <strong><em>org.junit.Test</em></strong> annotation as shown below</p>
<pre class="brush: java; title: ; notranslate">
@Test
public void emptyTest() {
 stack = new Stack&lt;String&gt;();
 assertTrue(stack.isEmpty());
}
</pre>
<p><strong><em>2)Using Assert Methods:</em></strong> In <strong><em>JUnit 4.0</em></strong> test classes do not inherit from <strong><em>TestCase</em></strong>, as a result, the Assert methods are not available to the test classes. In order to use the Assert methods, you have to use either the prefixed syntax (Assert.assertEquals()) or, use a <strong><em>static import</em></strong>for the Assert class.</p>
<pre class="brush: java; title: ; notranslate">
import static org.junit.Assert.*;
</pre>
<p>Now the assert methods may be used directly as done with the previous versions of JUnit.</p>
<p><strong><em>3)Changes in Assert Methods:</em></strong> The new <strong><em>assertEquals</em></strong> methods use <strong><em>Autoboxing</em></strong>, and hence all the assertEquals(primitive, primitive) methods will be tested as assertEquals(Object, Object). This may lead to some interesting results. For example autoboxing will convert all numbers to the Integer class, so an Integer(10) may not be equal to Long(10). This has to be considered when writing tests for arithmetic methods. For example, the following Calc class and it&#8217;s corresponding test CalcTest will give you an error.</p>
<pre class="brush: java; title: ; notranslate">
public class Calc {
 public long add(int a, int b) {
  return a+b;
 }
}

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class CalcTest {
 @Test
 public void testAdd() {
  assertEquals(5, new Calc().add(2, 3));
 }
}
</pre>
<p>You will end up with the following error.</p>
<pre class="brush: java; title: ; notranslate">
java.lang.AssertionError: expected:&lt;5&gt; but was:&lt;5&gt;
</pre>
<p>This is due to autoboxing. By default all the integers are cast to Integer, but we were expecting long here. Hence the error. In order to overcome this problem, it is better if you type cast the first parameter in the <strong>assertEquals</strong> to the appropriate return type for the tested method as follows</p>
<pre class="brush: java; title: ; notranslate">assertEquals((long)5, new Calc().add(2, 3));
</pre>
<p>There are also a couple of methods for comparing Arrays</p>
<pre class="brush: java; title: ; notranslate">
public static void assertEquals(String message, Object[] expecteds, Object[] actuals);
public static void assertEquals(Object[] expecteds, Object[] actuals);
</pre>
<p><strong><em>4)Setup and TearDown:</em></strong> You need not have to create setup and teardown methods for setup and teardown. The <strong><em>@Before, @After and @BeforeClass, @AfterClass</em></strong> annotations are used for implementing setup and teardown operations. The <strong><em>@Before and @BeforeClass</em></strong> methods are run before running the tests. The @After and @AfterClass methods are run after the tests are run. The only difference being that the <strong><em>@Before and @After</em></strong> can be used for multiple methods in a class, but the <strong><em>@BeforeClass and @AfterClass</em></strong> can be used only once per class.</p>
<p><strong><em>5)Parameterized Tests:</em></strong> <strong><em>JUnit 4.0</em></strong>comes with another special runner: Parameterized, which allows you to run the same test with different data. For example, in the the following peice of code will imply that the tests will run four times, with the parameter &#8220;number&#8221; changed each time to the value in the array.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<pre class="brush: java; title: ; notranslate">
@RunWith(value = Parameterized.class)
public class StackTest {
 Stack&lt;Integer&gt; stack;
 private int number;

 public StackTest(int number) {
   this.number = number;
 }

 @Parameters
 public static Collection data() {
   Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
   return Arrays.asList(data);
 }
 ...
}
</pre>
<p>The requirement for parameterized tests is to<br />
Have the annotation @RunWith for the Test Class<br />
Have a public static method that returns a Collection for data. Each element of the collection must be an Array of the various paramters used for the test.<br />
You will also need a public constructor that uses the parameters</p>
<p><strong><em>6)Test Suites:</em></strong> In <strong><em>JUnit 3.8</em></strong> you had to add a <strong><em>suite()</em></strong> method to your classes, to run all tests as a suite. With <strong><em>JUnit 4.0</em></strong> you use annotations instead. To run the CalculatorTest and SquareTest you write an empty class with <strong><em>@RunWith</em></strong> and <strong><em>@Suite</em></strong>annotations.</p>
<p>&nbsp;</p>
<pre class="brush: java; title: ; notranslate">
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({StackTest.class})
public class AllTests {
}
</pre>
<p>The &#8220;Suite&#8221; class takes SuiteClasses as argument which is a list of all the classes that can be run in the suite.</p>
<p>The following is a listing of the example StackTest used in the post.</p>
<pre class="brush: java; title: ; notranslate">
package tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collection;
import java.util.EmptyStackException;
import java.util.Stack;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(value = Parameterized.class)
public class StackTest {
 Stack&lt;Integer&gt; stack;

 private int number;

 public StackTest(int number) {
   this.number = number;
 }

 @Parameters
 public static Collection data() {
   Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
   return Arrays.asList(data);
 }

 @Before
 public void noSetup() {
   stack = new Stack&lt;Integer&gt;();
 }

 @After
 public void noTearDown() {
   stack = null;
 }

 @Test
 public void pushTest() {
   stack.push(number);
   assertEquals(stack.peek(), number);

 }

 @Test
 public void popTest() {
 }

 @Test(expected = EmptyStackException.class)
 public void peekTest() {
   stack = new Stack&lt;Integer&gt;();
   stack.peek();
 }

 @Test
 public void emptyTest() {
   stack = new Stack&lt;Integer&gt;();
   assertTrue(stack.isEmpty());
 }

 @Test
 public void searchTest() {
 }
}
</pre>
<p><strong> <span style="text-decoration: underline;">JUnit Books:</span></strong></p>
<ul>
<li><a href="http://www.flipkart.com/junit-action-8177225383/p/itmdyn4pgtwes3zy?pid=9788177225389&amp;affid=suthukrish" target="_blank">JUnit in Action</a></li>
<li><a href="http://www.flipkart.com/junit-pocket-guide-100-pages-8173668604/p/itmczzj4ch4htgay?pid=9788173668609&amp;affid=suthukrish" target="_blank">JUnit Pocket Guide</a></li>
</ul>
<p><strong>If you are interested in receiving the future java articles and tips from us, please</strong> <a href="http://www.javabeat.net/subscribe/">subscribe here</a>. <span style="color: #000080;">If you have any doubts on <strong>JUnit</strong>, please post it in the comments section. If you are working on the <strong>JUnit</strong> testing environment and looking for any example code, please post it in the comments section. We will come up with the sample code to help you. If you want to share your experience on the JUnit, please write it on the comments section. </span></p>
<p>&nbsp;</p>
<ul>
<li><strong><a href="http://www.javabeat.net/2010/08/testing-support-in-spring-framework/">Testing Support in Spring Framework</a></strong></li>
</ul>
<div class='dd_outer'><div class='dd_inner'><div id='dd_ajax_float'><div class='dd_button_v'><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Fwww.javabeat.net%2F2008%2F10%2Fjunit-4-0-example%2F" send="false" show_faces="false"  layout="box_count" width="50"  ></fb:like></div><div style='clear:left'></div><div class='dd_button_v'><script type='text/javascript' src='https://apis.google.com/js/plusone.js'></script><g:plusone size='tall' href='http://www.javabeat.net/2008/10/junit-4-0-example/'></g:plusone></div><div style='clear:left'></div><div class='dd_button_v'><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.javabeat.net/2008/10/junit-4-0-example/" data-count="vertical" data-text="JUnit 4.0 Example" data-via="javabeat" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div style='clear:left'></div><div class='dd_button_extra_v'><script type="text/javascript">jQuery(document).load(function(){ stLight.options({publisher:'bab47279-62c9-46af-addc-79fd1fe8fee0'}); });</script><div class="st_email_custom"><span id='dd_email_text'>email</span></div></div><div style='clear:left'></div><div class='dd_button_extra_v'><div id='dd_print_button'><span id='dd_print_text'><a href='javascript:window:print()'>print</a></span></div></div><div style='clear:left'></div></div></div></div><script type="text/javascript">var dd_offset_from_content = 44; var dd_top_offset_from_content = 0;</script><script type="text/javascript" src="http://www.javabeat.net/wp-content/plugins/digg-digg//js/diggdigg-floating-bar.js?ver=5.3.0"></script><div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2008/10/junit-4-0-example/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
