<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Five 's Weblog &#187; JUnit</title>
	<atom:link href="http://powerdream5.wordpress.com/tag/junit/feed/" rel="self" type="application/rss+xml" />
	<link>http://powerdream5.wordpress.com</link>
	<description>It is not just another blog! 伍行天下！</description>
	<lastBuildDate>Wed, 28 Nov 2007 21:21:29 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='powerdream5.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/b83f3cb0955eab114902a009525db137?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Five 's Weblog &#187; JUnit</title>
		<link>http://powerdream5.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://powerdream5.wordpress.com/osd.xml" title="Five &#8217;s Weblog" />
		<item>
		<title>An example of JMock!</title>
		<link>http://powerdream5.wordpress.com/2007/10/16/an-example-of-jmock/</link>
		<comments>http://powerdream5.wordpress.com/2007/10/16/an-example-of-jmock/#comments</comments>
		<pubDate>Wed, 17 Oct 2007 00:35:21 +0000</pubDate>
		<dc:creator>powerdream5</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JMock]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Test-Driven development]]></category>

		<guid isPermaLink="false">http://powerdream5.wordpress.com/2007/10/16/an-example-of-jmock/</guid>
		<description><![CDATA[        Test-Driven Development(TDD) has already been proved to be a best practice for software ddevelopment. And In the process of TDD, we frequently come across such situation that one method has to communicate with other classes to realize its function. Therefore, in order to test this method, we have to create the class it is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=powerdream5.wordpress.com&blog=1840509&post=54&subd=powerdream5&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://powerdream5.files.wordpress.com/2007/10/10_16_3_2007.jpg" title="10_16_3_2007.jpg"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_16_2_2007.jpg" title="10_16_2_2007.jpg"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_16_1_2007.jpg" title="10_16_1_2007.jpg"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_16_1_2007.jpg" title="10_16_1_2007.jpg"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_16_1_2007.jpg" title="10_16_1_2007.jpg"></a>        Test-Driven Development(TDD) has already been proved to be a best practice for software ddevelopment. And In the process of TDD, we frequently come across such situation that one method has to communicate with other classes to realize its function. Therefore, in order to test this method, we have to create the class it is going to communicate with firstly. However, this will deviate from the concept of unit testing. Fortunately, we have some useful tools to handle this kind of situation, such as <a target="_blank" href="http://www.jmock.org">JMock </a>and <a href="http://www.easymock.org/">EasyMock</a>, which can mock the objects the method interact with, so that we don&#8217;t need to crate a real object to finish the testing for that method.</p>
<p><a href="http://powerdream5.files.wordpress.com/2007/10/10_16_3_2007.jpg" title="10_16_3_2007.jpg"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_16_3_2007.jpg" title="10_16_3_2007.jpg"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_16_3_2007.jpg" title="10_16_3_2007.jpg"></a></p>
<p style="text-align:center;"><img src="http://powerdream5.files.wordpress.com/2007/10/10_16_3_2007.jpg" alt="10_16_3_2007.jpg" /></p>
<p>        Today, I am going to show an example of the use of JMock. Firstly, you need to download JMock and JUnit. Do not use the latesting version (junit 4.4) of JUnit. In this example, I use JUnit 4.3.1 and JMock 2.2. Then add the following packages into your classpath: <strong>junit-4.3.1.jar</strong>, <strong>jmock-2.2.-.jar</strong>, <strong>jmock-junit4-2.2.0.jar</strong>, <strong>hamcrest-core-1.1.jar</strong> and <strong>hamcrest-library-1.1.jar</strong>.<br />
        I am going to create a small application which can say different greetings to the client according to different time. For example, it outputs &#8220;Good morning, ***&#8221; in the morning, and outputs &#8220;Good evening, ***&#8221; in the evening. According to the procedure of TDD, we firstly create a test case.</p>
<p><font color="#ff0000">//import some necessary classes from the package of JUnit4<br />
</font>import org.junit.Test;<br />
import static org.junit.Assert.asserEquals;</p>
<p>public class TestJMockCase{</p>
<p>   @Test<br />
   public void testSayGreeting(){<br />
         Greeting o = new Greeting();<br />
         String out = o.SayGreeting(&#8220;wu cao&#8221;);<br />
         assertEquals(&#8220;Good afternoon, wu cao&#8221;, out);<br />
   } <br />
}</p>
<p>        Because we haven&#8217;t created the class of Greeting, we will fail in compiling this test case. In order to make it through compilation, we need to code the Greeting class. And we find the Greeting class have to communicate with the other class named GreetingTime to get the the words of greeting.</p>
<p>public class Greeting{<br />
        <font color="#ff0000">//we have to define GreetingTime as an interface<br />
        //I am goint to explain it in the later<br />
</font>        private GreetingTime gt;<br />
        public void setGreetingTime(){<br />
               this.gt = gt;<br />
        }</p>
<p>        public String sayHello(){<br />
               String greeting = gt.getGreeting();<br />
               return greeting+&#8221;, &#8220;+name;  <br />
        }<br />
} <br />
        In order to compile successfully, we also need to create the class of GreetingTime. But now, our focus is not on the GreetingTime class, so we decide to use mock object.</p>
<p>public interface GreetingTime{<br />
        public String getGreeting();<br />
}</p>
<p>       Because we are going to mock the GreetingTime class, we have to define it as an interface, which is a prerequisite for the mock object. Therefore, we have to use the setter method to inject the instance of GreetingTime to Greeting, (we cannot use the new keyword on an interface).</p>
<p>       Then, we have to make some changes to the test class we created before, because we are going to mock the GreetingTime.</p>
<p><font color="#ff0000">//import some necessary classes from the package of JUnit4<br />
</font>import org.junit.Test;<br />
import org.junit.runner.RunWith;<br />
import static org.junit.Assert.assertEquals;<br />
<font color="#ff0000">//import some necessary classes from the package of JMock2.2<br />
</font>import org.jmock.Mockery;<br />
import org.jmock.integration.junit4.JMock;<br />
import org.jmock.integration.junit4.JUnit4Mockery;<br />
import org.jmock.Expectations;<br />
@RunWith(JMock.class)<br />
public class TestJMockCaseUsingJMock{<br />
        <font color="#ff0000">//create the context for the mock object<br />
</font>        Mockery context = new JUnit4Mockery();</p>
<p>        @Test<br />
         public void testSayHello()<br />
        {<br />
             <font color="#ff0000">//Mock the GreetingTime class using the technology of reflection<br />
             //you have to define it as a final variable<br />
             //since it is going to be used in a inner class<br />
</font>             final GreetingTime gt = context.mock(GreetingTime.class);<br />
             Greeting g = new Greeting();<br />
              g.setGreetingTime(gt);</p>
<p>             <font color="#ff0000">//set the expection for the mock object</font><br />
             context.checking(new Expectations(){{<br />
                   <font color="#ff0000">//the mock object will call the getGreeting method once<br />
                  //and we assume that is returns &#8220;Good afternoon&#8221;<br />
</font>                  one(gt).getGreeting(); will(returnValue(&#8220;Good afternoon&#8221;));<br />
              }});</p>
<p>             String out = g.sayHello(&#8220;wu cao&#8221;);<br />
             <font color="#ff0000">//compare the result and expected value<br />
</font>             assertEquals(&#8220;Good afternoon, wu cao&#8221;,out);<br />
         }<br />
}</p>
<p>Now, you can open the command widnow to test.</p>
<p><a href="http://powerdream5.files.wordpress.com/2007/10/10_16_2_2007.jpg" title="10_16_2_2007.jpg"></p>
<p style="text-align:center;"><img src="http://powerdream5.files.wordpress.com/2007/10/10_16_2_2007.jpg" alt="10_16_2_2007.jpg" /></p>
<p></a></p>
<p style="text-align:center;"><img src="http://powerdream5.files.wordpress.com/2007/10/10_16_1_2007.jpg" alt="10_16_1_2007.jpg" /></p>
<p><a href="http://powerdream5.files.wordpress.com/2007/10/10_16_1_2007.jpg" title="10_16_1_2007.jpg"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_16_1_2007.jpg" title="10_16_1_2007.jpg"></a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/powerdream5.wordpress.com/54/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/powerdream5.wordpress.com/54/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/powerdream5.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/powerdream5.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/powerdream5.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/powerdream5.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/powerdream5.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/powerdream5.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/powerdream5.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/powerdream5.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/powerdream5.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/powerdream5.wordpress.com/54/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=powerdream5.wordpress.com&blog=1840509&post=54&subd=powerdream5&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://powerdream5.wordpress.com/2007/10/16/an-example-of-jmock/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a0819a06b6ff4afc4f3e0a1977e6a5a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">powerdream5</media:title>
		</media:content>

		<media:content url="http://powerdream5.files.wordpress.com/2007/10/10_16_3_2007.jpg" medium="image">
			<media:title type="html">10_16_3_2007.jpg</media:title>
		</media:content>

		<media:content url="http://powerdream5.files.wordpress.com/2007/10/10_16_2_2007.jpg" medium="image">
			<media:title type="html">10_16_2_2007.jpg</media:title>
		</media:content>

		<media:content url="http://powerdream5.files.wordpress.com/2007/10/10_16_1_2007.jpg" medium="image">
			<media:title type="html">10_16_1_2007.jpg</media:title>
		</media:content>
	</item>
	</channel>
</rss>