<?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>EclipseSource Blog &#187; testing</title>
	<atom:link href="http://eclipsesource.com/blogs/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://eclipsesource.com/blogs</link>
	<description>Eclipse Equinox OSGi</description>
	<lastBuildDate>Fri, 03 Feb 2012 17:54:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Effective Mockito Part 5</title>
		<link>http://eclipsesource.com/blogs/2011/11/16/effective-mockito-part-5/</link>
		<comments>http://eclipsesource.com/blogs/2011/11/16/effective-mockito-part-5/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 11:29:06 +0000</pubDate>
		<dc:creator>Holger Staudacher</dc:creator>
				<category><![CDATA[craftsmanship]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[syndicate]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[Mocking]]></category>
		<category><![CDATA[Mockito]]></category>
		<category><![CDATA[Software craftsmanship]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=6721</guid>
		<description><![CDATA[With this effective Mockito Post I want to share a really simple pattern with you. We call this pattern &#8220;check answers&#8221; and we use it whenever we work with Mockito Answers. The code resulting from creating Mockito Answers generally looks ugly. But, as good programmers we care about test quality, right? Let&#8217;s see how we [...]]]></description>
			<content:encoded><![CDATA[<p>With this effective <a href="http://code.google.com/p/mockito/">Mockito</a> Post I want to share a really simple pattern with you. We call this pattern &#8220;check answers&#8221; and we use it whenever we work with <a href="http://docs.mockito.googlecode.com/hg/latest/org/mockito/stubbing/Answer.html">Mockito Answers</a>. The code resulting from creating <a href="http://code.google.com/p/mockito/">Mockito</a> Answers generally looks ugly. But, as good programmers we care about test quality, right? <img src='http://eclipsesource.com/blogs/wp-includes/images/smilies/icon_smile.gif' alt="icon smile Effective Mockito Part 5" class='wp-smiley' title="Effective Mockito Part 5" /> </p>
<p>Let&#8217;s see how we can make better looking answers using the &#8220;check answer&#8221; pattern. <a href="http://docs.mockito.googlecode.com/hg/latest/org/mockito/stubbing/Answer.html">Mockito&#8217;s Answers</a> are great when you need to verify that a method was called with specific parameters, or you need to execute an operation that also needs to be mocked. I&#8217;m sure you have seen situations where Answers were very useful. Let&#8217;s dive into a simple example. Our fancy program consists of three classes which are listed in the snippet below.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyRuntimeState <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> state<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setState<span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">boolean</span> state <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">state</span> <span style="color: #339933;">=</span> state<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isTrue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> state<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyDelegate <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span> MyRuntimeState state <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// do some fancy operation</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyObject <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> MyDelegate delegate<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> MyObject<span style="color: #009900;">&#40;</span> MyDelegate delegate <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">delegate</span> <span style="color: #339933;">=</span> delegate<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> operate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    MyRuntimeState state <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyRuntimeState<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    state.<span style="color: #006633;">setState</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this is the important fact we want to test</span>
    delegate.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span> state <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see we have the Types <code>MyObject</code>, <code>MyDelegate</code> and <code>MyRuntimeState</code>. <code>MyObject</code> needs a <code>MyDelegate</code> in its constructor. In the operate method, <code>MyObject</code> calls <code>MyDelegate</code>&#8216;s <code>doSomething</code> method with a newly created <code>MyRuntimeState</code> object. Let&#8217;s assume that for some reason our fake system needs a <code>MyRuntimeState</code> with the state &#8216;<code>true</code>&#8216; to work (be creative &#8211; it&#8217;s just an example <img src='http://eclipsesource.com/blogs/wp-includes/images/smilies/icon_wink.gif' alt="icon wink Effective Mockito Part 5" class='wp-smiley' title="Effective Mockito Part 5" /> ).  So, <code>MyObject</code> needs to set the state of the <code>MyRuntimeState</code> object to true. This is exactly the case where Answers are useful to verify that the state is true within the <code>doSomething</code> invocation (we can probably also do this with verify in this example, but there are harder operations out there where verify can&#8217;t be used).  Let&#8217;s look at the test for this mechanism.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RunWith<span style="color: #009900;">&#40;</span> MockitoJUnitRunner.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyObjectTest <span style="color: #009900;">&#123;</span>
&nbsp;
  @Mock
  <span style="color: #000000; font-weight: bold;">private</span> MyDelegate delegate<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> MyObject myObject<span style="color: #339933;">;</span>
&nbsp;
  @Before
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    myObject <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyObject<span style="color: #009900;">&#40;</span> delegate <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSendsRightState<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    doAnswer<span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Answer<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      @Override
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> answer<span style="color: #009900;">&#40;</span> InvocationOnMock invocation <span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
        MyRuntimeState state <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> MyRuntimeState <span style="color: #009900;">&#41;</span>invocation.<span style="color: #006633;">getArguments</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        assertTrue<span style="color: #009900;">&#40;</span> state.<span style="color: #006633;">isTrue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span> delegate <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span> any<span style="color: #009900;">&#40;</span> MyRuntimeState.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    myObject.<span style="color: #006633;">operate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You see that it&#8217;s a fully working test that tests exactly what we want, but it&#8217;s also a smell. I dislike several things in the <code>testSendsRightState</code> method. The first thing is the method chaining over several lines. The second thing is that we have an indentation level of three which leads to unreadable code. The third thing is that we can&#8217;t see at a glance what will be tested. The fourth thing I don&#8217;t like is that the assert statement comes before the actual operate call at least in the writing of the code. The last thing is that this method is way too long.</p>
<p>Let&#8217;s try to get rid of most of the smells by simply doing one refactoring. You may say that this is obvious, and yes it is;). We need to extract the creation of the Answer object into a separate method. I name these methods <code>createCheck***Answer</code> (*** stands for a named check) because the answers that will be created are invoking the actual asserts. The result can look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSendsRightState<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  Answer<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> checkStateAnswer <span style="color: #339933;">=</span> createCheckStateAnswer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  doAnswer<span style="color: #009900;">&#40;</span> checkStateAnswer <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span> delegate <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span> any<span style="color: #009900;">&#40;</span> MyRuntimeState.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  myObject.<span style="color: #006633;">operate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> Answer<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> createCheckStateAnswer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Answer<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> answer<span style="color: #009900;">&#40;</span> InvocationOnMock invocation <span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
      MyRuntimeState state <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> MyRuntimeState <span style="color: #009900;">&#41;</span>invocation.<span style="color: #006633;">getArguments</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      assertTrue<span style="color: #009900;">&#40;</span> state.<span style="color: #006633;">isTrue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This simple refactoring has a huge impact on our test method. The test method has only a length of three lines of code now. The indentation level is one. The call chain fits into one line. Even the <code>createCheckStateAnswer</code> is more readable because it&#8217;s not surrounded by chained calls. One drawback still resists this solution. That is, that the assert is now called within another method. To me, this is a very low price to pay compared to the problems in the first test. And, the best thing about this solution is that when you only read the test method you see at first glance that an answer called checkStateAnswer was created that obviously does something like check a state. Other programmers should be able to understand most of the test method without reading the <code>createCheckStateAnswer</code> method.</p>
<p>I know that this solution is very simple and for you, may be obvious. But when I started with <a href="http://docs.mockito.googlecode.com/hg/latest/org/mockito/stubbing/Answer.html">Answers</a> I didn&#8217;t do this for reasons only the Programming Gods know now. If you have any other things related to Answers in your repertoire please share them with us in a comment.</p>
<p><a href="http://twitter.com/hstaudacher"><img src="http://download.eclipsesource.com/~hstaudacher/followme.png" title="Follow @hstaudacher" width="191" height="58" border="0" alt="followme Effective Mockito Part 5" /></a></p>
<p><strong><em>Read the other Effective Mockito posts:</em></strong></p>
<ul>
<li><a href="http://eclipsesource.com/blogs/2011/09/19/effective-mockito-part-1/">Effective Mockito Part 1</a></li>
<li><a href="http://eclipsesource.com/blogs/2011/09/29/effective-mockito-part-2/">Effective Mockito Part 2</a></li>
<li><a href="http://eclipsesource.com/blogs/2011/10/13/effective-mockito-part-3/">Effective Mockito Part 3</a></li>
<li><a href="http://eclipsesource.com/blogs/2011/10/17/effective-mockito-part-4/">Effective Mockito Part 4</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://eclipsesource.com/blogs/2011/11/16/effective-mockito-part-5/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Introducing restfuse &#8211; a JUnit Extension to test REST APIs</title>
		<link>http://eclipsesource.com/blogs/2011/11/14/introducing-restfuse-a-junit-extension-to-test-rest-apis/</link>
		<comments>http://eclipsesource.com/blogs/2011/11/14/introducing-restfuse-a-junit-extension-to-test-rest-apis/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 08:01:11 +0000</pubDate>
		<dc:creator>Holger Staudacher</dc:creator>
				<category><![CDATA[craftsmanship]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[OSGi]]></category>
		<category><![CDATA[syndicate]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[restfuse]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=6670</guid>
		<description><![CDATA[For several projects at EclipseSource we are creating REST APIs. I&#8217;m involved in most of them and there is one thing that bothers me with every project. That is, testing. I mean, of course we are writing our unit tests first and we mock our services to get fast unit tests, but at some point [...]]]></description>
			<content:encoded><![CDATA[<p>For several projects at <a href="http://eclipsesource.com/">EclipseSource</a> we are creating <a href="http://en.wikipedia.org/wiki/Representational_state_transfer">REST APIs</a>. I&#8217;m involved in most of them and there is one thing that bothers me with every project. That is, testing. I mean, of course we are writing our unit tests first and we mock our services to get fast unit tests, but at some point you also have to make sure that the whole system works with integration tests. And, when writing integration tests for REST APIs in Java, as far as I know, there are currently only two solutions.</p>
<p>The first one is to write plain <a href="http://junit.org">JUnit</a> tests and do all the requests yourself within the test methods (maybe with the help of utilities). The other option is to use a library called <a href="http://code.google.com/p/rest-assured/">rest-assured</a>. This library provides a kind of DSL for testing REST APIs. You can write your tests with JUnit and use <a href="http://eclipsesource.com/blogs/2011/09/19/effective-mockito-part-1/">mockito</a>-like syntax to send a request and test the response. But this doesn&#8217;t feel like the right solution, because you always have to configure your request with real Java code within your test method. I would prefer a solution where I can simply configure the request using something like annotations and just execute the test after the request is sent.</p>
<p>Another thing that bugs me are asynchronous services. When it comes to handling asynchronous services you always have two options: callbacks or polling. How can I test those services? For polling it&#8217;s easier &#8211; you can loop the request code &#8211; but does this sound right to you? For callbacks you have to open a server, again within your test method or before. It seems there are no cool solutions for this right now &#8211; even rest-assured can&#8217;t handle these services very well. That&#8217;s why I took a little time and tried to solve these problems. The result is a small library called <a href="http://restfuse.com/">restfuse</a>.</p>
<p>Restfuse is a JUnit extension. It introduces an annotation called <a href="http://restfuse.com/doc/index.html?com/eclipsesource/restfuse/annotation/HttpTest.html"><code>@HttpTest</code></a> which can be used to configure a request. The request is sent before the annotated method is executed as a test method. The response is then injected into the test object and can be used within the test method. It also provides some new asserts like assertNotFound or <code>assertOk</code> to test response codes. A simple <code>@HttpTest</code> looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RunWith<span style="color: #009900;">&#40;</span> HttpJUnitRunner.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RestfuseTest <span style="color: #009900;">&#123;</span>
&nbsp;
  @Rule
  <span style="color: #000000; font-weight: bold;">public</span> Destination destination <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Destination<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;http://restfuse.com&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
  @<span style="color: #003399;">Context</span>
  <span style="color: #000000; font-weight: bold;">private</span> Response response<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will be injected after every request</span>
&nbsp;
  @HttpTest<span style="color: #009900;">&#40;</span> method <span style="color: #339933;">=</span> <span style="color: #003399;">Method</span>.<span style="color: #006633;">GET</span>, path <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> checkRestfuseOnlineStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    assertOk<span style="color: #009900;">&#40;</span> response <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>  
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And of course, it also solves the problem with asynchronous services by introducing two annotations called <a href="http://restfuse.com/doc/index.html?com/eclipsesource/restfuse/annotation/Callback.html"><code>@Callback</code></a> and <a href="http://restfuse.com/doc/index.html?com/eclipsesource/restfuse/annotation/Poll.html"><code>@Poll</code></a> which can be used together with the @HttpTest annotation. For more information on these tests for asynchronous services <a href="http://restfuse.com/asynchron/">take a look at the restfuse site</a>.</p>
<p><a href="http://restfuse.com">Restfuse</a> is open source, licensed under the <a href="http://www.eclipse.org/legal/epl-v10.html">EPL v &#8211; 1.0</a> and is hosted at <a href="https://github.com/eclipsesource/restfuse">github</a>. The 1.0 version is also in the <a href="http://search.maven.org/#artifactdetails%7Ccom.restfuse%7Ccom.eclipsesource.restfuse%7C1.0.0%7Cjar">Maven Central</a> and online as a <a href="http://restfuse.com/p2/">p2 repository</a> (yes, it&#8217;s an OSGi bundle).  I hope this small library will help you as much as it helped me. I would appreciate feedback on how to make restfuse even better.</p>
<p><a href="http://twitter.com/hstaudacher"><img src="http://download.eclipsesource.com/~hstaudacher/followme.png" title="Follow @hstaudacher" width="191" height="58" border="0" alt="followme Introducing restfuse   a JUnit Extension to test REST APIs" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsesource.com/blogs/2011/11/14/introducing-restfuse-a-junit-extension-to-test-rest-apis/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Effective Mockito Part 4</title>
		<link>http://eclipsesource.com/blogs/2011/10/17/effective-mockito-part-4/</link>
		<comments>http://eclipsesource.com/blogs/2011/10/17/effective-mockito-part-4/#comments</comments>
		<pubDate>Mon, 17 Oct 2011 07:55:31 +0000</pubDate>
		<dc:creator>Holger Staudacher</dc:creator>
				<category><![CDATA[craftsmanship]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[syndicate]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[Mocking]]></category>
		<category><![CDATA[Mockito]]></category>
		<category><![CDATA[test driven]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=6545</guid>
		<description><![CDATA[This Effective Mockito Post will be IDE specific again but related to the last post on Mockito&#8217;s spies. If you&#8217;ve read Part 3 you should now be familiar how to use them to &#8220;pseudo mock&#8221; statics. When writing code it often comes to a point where we want to debug using single step debugging. When [...]]]></description>
			<content:encoded><![CDATA[<p>This Effective Mockito Post will be IDE specific again but related to the <a href="http://eclipsesource.com/blogs/2011/10/13/effective-mockito-part-3/">last post</a> on <a href="http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#spy(T)">Mockito&#8217;s spies</a>. If you&#8217;ve read Part 3 you should now be familiar how to use them to &#8220;pseudo mock&#8221; statics. When writing code it often comes to a point where we want to debug using single step debugging. When using <a href="http://code.google.com/p/mockito/">Mockito</a> and especially when spies come into the game there is still something pretty annoying.</p>
<p>That is, when we want to debug our Object-Under-Test&#8217;s real method and the object is a spy. When we try to step into the object&#8217;s method we always land in internal Mockito code. After a little reading we can drill deeper but the next landing place is in java.lang.reflect code and so on. To be honest, this is really not the nicest way to debug code. What I want is that regardless of whether I use spies or not, when I step into an object&#8217;s method I land directly in this method. In the past my workaround was to set a breakpoint in the test at the method call and in the method itself. This enabled me to stop at the first point and resume the execution until it stops the second time in the method. But there is a much more elegant way. Let&#8217;s see how we can do this.</p>
<p>First of all, let&#8217;s take a look at an example. The code below shows a simple test from the <a href="http://eclipsesource.com/blogs/2011/10/13/effective-mockito-part-3/">last Effective Mockito post</a>. The test uses a spy to &#8220;pseudo mock&#8221; a static method. We set a breakpoint in the line of the <code>isPropertySet</code> call and debug this method. Sadly when doing this we run into the effect described above.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RunWith<span style="color: #009900;">&#40;</span> MockitoJUnitRunner.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyObjectText <span style="color: #009900;">&#123;</span>
&nbsp;
  @Spy
  <span style="color: #000000; font-weight: bold;">private</span> MyObject objectUnderTest<span style="color: #339933;">;</span>
&nbsp;
  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testIsPropertySet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    doReturn<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;some runtime property&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span> objectUnderTest <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">boolean</span> isPropertySet <span style="color: #339933;">=</span> objectUnderTest.<span style="color: #006633;">isPropertySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    assertFalse<span style="color: #009900;">&#40;</span> isPropertySet <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When using <a href="http://www.eclipse.org/downloads/">Eclipse</a> there is a very simple way to avoid this called &#8220;Step Filtering&#8221;. We can add packages or classes to this Filterlist using the preferences. When filters are activated the classes will automatically be skipped during debugging. By setting a filter like the one in the screenshot below we can simply step over the Mockito and reflection code.</p>
<p><a href="http://eclipsesource.com/blogs/wp-content/uploads/2011/10/stepfilters.png"><img class="aligncenter size-full wp-image-6553" title="stepfilters" src="http://eclipsesource.com/blogs/wp-content/uploads/2011/10/stepfilters.png" alt="stepfilters Effective Mockito Part 4" width="652" height="599" /></a></p>
<p>If you are not using Eclipse there is probably a similar way to achieve this with your IDE. It would be cool if you would share this tip with us in a comment. Meantime, I hope that Step Filtering is as helpful to you as it is for me.  Finally, I want to thank <a href="http://eclipsesource.com/blogs/author/fwaibel/">Fluffi</a> and <a href="http://www.codeaffine.com/author/fappel/">Frank</a> for getting me to dive into this.</p>
<p><a href="http://twitter.com/hstaudacher"><img src="http://download.eclipsesource.com/~hstaudacher/followme.png" title="Follow @hstaudacher" width="191" height="58" border="0" alt="followme Effective Mockito Part 4" /></a></p>
<p><strong><em>Read the other Effective Mockito posts:</em></strong></p>
<ul>
<li><a href="http://eclipsesource.com/blogs/2011/09/19/effective-mockito-part-1/">Effective Mockito Part 1</a></li>
<li><a href="http://eclipsesource.com/blogs/2011/09/29/effective-mockito-part-2/">Effective Mockito Part 2</a></li>
<li><a href="http://eclipsesource.com/blogs/2011/10/13/effective-mockito-part-3/">Effective Mockito Part 3</a></li>
<li><a href="http://eclipsesource.com/blogs/2011/10/17/effective-mockito-part-4/">Effective Mockito Part 4</a></li>
<li><a href="http://eclipsesource.com/blogs/2011/11/16/effective-mockito-part-5/">Effective Mockito Part 5</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://eclipsesource.com/blogs/2011/10/17/effective-mockito-part-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Effective Mockito Part 3</title>
		<link>http://eclipsesource.com/blogs/2011/10/13/effective-mockito-part-3/</link>
		<comments>http://eclipsesource.com/blogs/2011/10/13/effective-mockito-part-3/#comments</comments>
		<pubDate>Thu, 13 Oct 2011 06:23:33 +0000</pubDate>
		<dc:creator>Holger Staudacher</dc:creator>
				<category><![CDATA[craftsmanship]]></category>
		<category><![CDATA[syndicate]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[Mocking]]></category>
		<category><![CDATA[Mockito]]></category>
		<category><![CDATA[Software craftsmanship]]></category>
		<category><![CDATA[test driven]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=6518</guid>
		<description><![CDATA[In the previous Effective Mockito post we saw how to use the @Mock Annotation to get a clean test. In this post I want to show you how to use Mockito&#8217;s spy mechanism to eliminate testing troubles with third party libraries. Testing is one of the most important things in software development. I assume you [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://eclipsesource.com/blogs/2011/09/29/effective-mockito-part-2/">previous Effective Mockito post</a> we saw how to use the <a href="http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#9">@Mock Annotation</a> to get a clean test. In this post I want to show you how to use <a href="http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#spy(T)">Mockito&#8217;s spy mechanism</a> to eliminate testing troubles with third party libraries.</p>
<p>Testing is one of the most important things in software development. I assume you agree with me because you decided to read this blog post <img src='http://eclipsesource.com/blogs/wp-includes/images/smilies/icon_wink.gif' alt="icon wink Effective Mockito Part 3" class='wp-smiley' title="Effective Mockito Part 3" /> .  It goes without saying that when we develop software we often rely on third party functionality. This does not affect our testing because we can mock objects from third party libraries thanks to <a href="http://code.google.com/p/mockito/">Mockito</a>. But there is one thing that always ruins my karma&#8230;</p>
<p>That is, static methods. And even worse, static methods that depend on runtime state. I know there are solutions like <a href="http://code.google.com/p/powermock/wiki/MockitoUsage13">PowerMock to enable mocking statics</a>, but I think there is a more elegant solution to avoid the troubles with statics -  without manipulating the bytecode and without adding another mocking library, as we would with PowerMock.</p>
<p>Let&#8217;s dive into an example to see how. First, take a look at our third party code. In this example I know that I will have to use a static method from the framework which exists in the class <code>FancyFrameworkUtil</code>. The only purpose of this code is to show you that I have to use it and that the third party library changes its return type magically at runtime as the comment says (this sort of thing happens way too often).</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FancyFrameworkUtil <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;some runtime property&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this property changes at runtime ;)</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Let&#8217;s get to the code we want to write: a highly sophisticated type called <code>MyObject</code> <img src='http://eclipsesource.com/blogs/wp-includes/images/smilies/icon_wink.gif' alt="icon wink Effective Mockito Part 3" class='wp-smiley' title="Effective Mockito Part 3" /> . This type has only one method called <code>isPropertySet</code> which will become API (by this I mean the method will be added to a public interface). The method only checks to see if the value of the third party&#8217;s framework has a specific value and returns a boolean depending on the value.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyObject <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isPropertySet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> property <span style="color: #339933;">=</span> FancyFrameworkUtil.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">boolean</span> result <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> property.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;some runtime property&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      result <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We now know that the framework method returns a value that can change at runtime but this does not mean we can&#8217;t write a test.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyObjectTest <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> MyObject objectUnderTest<span style="color: #339933;">;</span>
&nbsp;
  @Before
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    objectUnderTest <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testIsPropertySet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    assertFalse<span style="color: #009900;">&#40;</span> objectUnderTest.<span style="color: #006633;">isPropertySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see it&#8217;s a very straightforward test. But it&#8217;s not complete because we haven&#8217;t tested what happens when the framework&#8217;s property value changes. We can&#8217;t be sure if our <code>MyObject#isPropertySet</code> ever returns true. I&#8217;m sure you&#8217;ve had a similar problem in the past, too.</p>
<p>At this point we have a choice. Introduce another framework to mock the static framework method or do something like the following. First, we need to extract the call to the third party library in a separate method in our <code>MyObject</code> implementation. It&#8217;s important that we change the visibility of this method to package private. This is the price we have to pay. But I don&#8217;t think its a high price because this method will not become API and is only visible in the implementation&#8217;s package. By separating the interface and the implementation into different packages this really isn&#8217;t a problem.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyObject <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isPropertySet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> property <span style="color: #339933;">=</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">boolean</span> result <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> property.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;some runtime property&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      result <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #003399;">String</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> FancyFrameworkUtil.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is all we have to do to our implementation to enable the &#8220;pseudo mocking&#8221; of the <code>FancyFrameworkUtil</code>&#8216;s method. Now comes the testing, and at this point we can use a spy. A spy is a real object with one or many mocked methods. Therefore we can spy on our <code>objectUnderTest</code>. In our case we want to mock the <code>getProperty</code> method that was added last. It can look like this.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyObjectTest <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> MyObject objectUnderTest<span style="color: #339933;">;</span>
&nbsp;
  @Before
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    objectUnderTest <span style="color: #339933;">=</span> spy<span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> MyObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testIsPropertySet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    doReturn<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;some runtime property&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span> objectUnderTest <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    assertFalse<span style="color: #009900;">&#40;</span> objectUnderTest.<span style="color: #006633;">isPropertySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testIsPropertySetWhenPropertyChanged<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    doReturn<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;foo&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span> objectUnderTest <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    assertTrue<span style="color: #009900;">&#40;</span> objectUnderTest.<span style="color: #006633;">isPropertySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see in the test methods, the spying in the setUp method enables us to change the return value of the framework&#8217;s method by introducing a delegation step. With this we can ensure that our method <code>isPropertySet</code> reacts to changes in the framework&#8217;s property.</p>
<p>I hope it&#8217;s now clear how to use a spy to mock static framework methods by using delegation steps. If you know alternative ways, please take a minute to share it with us in a comment. By the way, there is also an <a href="http://docs.mockito.googlecode.com/hg/latest/org/mockito/Spy.html">@Spy Annotation</a> you can use. Have fun spying <img src='http://eclipsesource.com/blogs/wp-includes/images/smilies/icon_wink.gif' alt="icon wink Effective Mockito Part 3" class='wp-smiley' title="Effective Mockito Part 3" /> </p>
<p><a href="http://twitter.com/hstaudacher"><img src="http://download.eclipsesource.com/~hstaudacher/followme.png" title="Follow @hstaudacher" width="191" height="58" border="0" alt="followme Effective Mockito Part 3" /></a></p>
<p><strong><em>Read the other Effective Mockito posts:</em></strong></p>
<ul>
<li><a href="http://eclipsesource.com/blogs/2011/09/19/effective-mockito-part-1/">Effective Mockito Part 1</a></li>
<li><a href="http://eclipsesource.com/blogs/2011/09/29/effective-mockito-part-2/">Effective Mockito Part 2</a></li>
<li><a href="http://eclipsesource.com/blogs/2011/10/13/effective-mockito-part-3/">Effective Mockito Part 3</a></li>
<li><a href="http://eclipsesource.com/blogs/2011/10/17/effective-mockito-part-4/">Effective Mockito Part 4</a></li>
<li><a href="http://eclipsesource.com/blogs/2011/11/16/effective-mockito-part-5/">Effective Mockito Part 5</a></li>
</ul>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsesource.com/blogs/2011/10/13/effective-mockito-part-3/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Testing hard to test code with EasyMock</title>
		<link>http://eclipsesource.com/blogs/2009/07/11/tip-making-hard-to-test-code-testable-with-easymock/</link>
		<comments>http://eclipsesource.com/blogs/2009/07/11/tip-making-hard-to-test-code-testable-with-easymock/#comments</comments>
		<pubDate>Sat, 11 Jul 2009 02:31:59 +0000</pubDate>
		<dc:creator>Elias Volanakis</dc:creator>
				<category><![CDATA[syndicate]]></category>
		<category><![CDATA[riena]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=2380</guid>
		<description><![CDATA[If you are into unit testing, you may find EasyMock quite useful. It is very valuable for making hard-to-test-code testable. For example I recently was adding tab-switching via keyboard to Riena. The state of each tab is kept in an interface named INavigationNode which has about 40 methods (!). Creating a mock by hand for [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://eclipsesource.com/blogs/wp-content/uploads/2009/07/hammock.jpg"><img class="size-full wp-image-2382 alignnone" title="hammock" src="http://eclipsesource.com/blogs/wp-content/uploads/2009/07/hammock.jpg" alt="hammock Testing hard to test code with EasyMock" width="600" height="250" /></a></p>
<p>If you are into unit testing, you may find <a href="http://easymock.org/">EasyMock</a> quite useful. It is very valuable for making hard-to-test-code testable.</p>
<p>For example I recently was adding tab-switching via keyboard to <a href="http://wiki.eclipse.org/Riena_New_And_Noteworthy#1.2.0.M1">Riena</a>. The state of each tab is kept in an interface named INavigationNode which has about 40 methods (!). Creating a mock by hand for such a big interface would not be fun at all. With EasyMock it just takes is a call to:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ISubApplicationNode nodeA <span style="color: #339933;">=</span> EasyMock.<span style="color: #006633;">createMock</span><span style="color: #009900;">&#40;</span>ISubApplicationNode.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><br/>The code for determining the next tab, depends strictly on the isSelected property. Manipulating the return value of this method takes just another EasyMock call:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">EasyMock.<span style="color: #006633;">expect</span><span style="color: #009900;">&#40;</span>nodeA.<span style="color: #006633;">isSelected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">andReturn</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This tells EasyMock that nodeA.isSelected() will be called once and should return true.</p>
<p>After everything is set-up I switch from &#8216;record&#8217; to &#8216;play-back&#8217; mode:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">EasyMock.<span style="color: #006633;">replay</span><span style="color: #009900;">&#40;</span>nodeA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here&#8217;s a full example of a test case:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setUp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    handler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SwitchSubApplication<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    nodeA <span style="color: #339933;">=</span> EasyMock.<span style="color: #006633;">createMock</span><span style="color: #009900;">&#40;</span>ISubApplicationNode.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    nodeB <span style="color: #339933;">=</span> EasyMock.<span style="color: #006633;">createMock</span><span style="color: #009900;">&#40;</span>ISubApplicationNode.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    nodeC <span style="color: #339933;">=</span> EasyMock.<span style="color: #006633;">createMock</span><span style="color: #009900;">&#40;</span>ISubApplicationNode.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testFindNextSubApplicationAtoB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    EasyMock.<span style="color: #006633;">expect</span><span style="color: #009900;">&#40;</span>nodeA.<span style="color: #006633;">isSelected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">andReturn</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    EasyMock.<span style="color: #006633;">replay</span><span style="color: #009900;">&#40;</span>nodeA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    ISubApplicationNode<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> nodes <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> nodeA, nodeB, nodeC <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    assertSame<span style="color: #009900;">&#40;</span>nodeB, handler.<span style="color: #006633;">findNextNode</span><span style="color: #009900;">&#40;</span>nodes<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This post barely scratches the surface. Curious? Read this nice <a href="http://easymock.org/EasyMock2_5_1_Documentation.html">EasyMock introduction</a>.</p>
<p><span style="color: #c0c0c0;">Image: (c) </span><a href="http://www.flickr.com/photos/emzee/139794246/"><span style="color: #c0c0c0;">*Micky/flickr</span></a><span style="color: #c0c0c0;">. Licensed under creative commons.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsesource.com/blogs/2009/07/11/tip-making-hard-to-test-code-testable-with-easymock/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Unit testing revelations</title>
		<link>http://eclipsesource.com/blogs/2009/02/17/unit-testing-revelations/</link>
		<comments>http://eclipsesource.com/blogs/2009/02/17/unit-testing-revelations/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 08:35:48 +0000</pubDate>
		<dc:creator>Manuel Woelker</dc:creator>
				<category><![CDATA[syndicate]]></category>
		<category><![CDATA[first]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[unit]]></category>

		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=394</guid>
		<description><![CDATA[The other day I experienced an unexpected light bulb moment concerning unit testing. Maybe this one is obvious to most of you, but I wish someone would have told me earlier. So here goes.

My biggest gripes with unit testing has been that I couldn't get any satisfactory answers to these two questions:

   1. Why should I practice Test-First?
   2. How do you test the tests?

]]></description>
			<content:encoded><![CDATA[<p>The other day I experienced an unexpected light bulb moment concerning unit testing. Maybe this one is obvious to most of you, but I wish someone would have told <em>me</em> earlier. So here goes.</p>
<p>My biggest gripes with unit testing has been that I couldn&#8217;t get any satisfactory answers to these two questions:</p>
<ol>
<li>Why should I practice Test-First?</li>
<li>How do you test the tests?</li>
</ol>
<p>Concerning the first issue, well we discussed some papers that where trying to answer that question when I attended a software quality course at university. The gist of the results were: There is no statistically significant difference in code quality between Test-First and and Test-Later. (Sorry can&#8217;t find links to the papers atm. Holler if you want me to find them and I&#8217;ll do some more digging.)</p>
<p>The second issue is discussed often as well: If tests are code and code should be tested, doesn&#8217;t that lead to more and more tests? This is sometimes referred to as the stack overflow of unit testing.</p>
<p>The revelation I had was this: <em>These two questions answer each other!</em> What&#8217;s the easiest way to test a test? A broken implementation. Where do you get a broken implementation? Just use an incomplete implementation. If you practice  Test-First all your implementations start off incomplete by definition. This means that each assertion in your test is guaranteed to fail at least once, giving you the confidence that the assertions actually perform significant work.</p>
<p>I am often surprised that assertions that I expect to fail actually go through just fine. This usually means one of two things: Either the functionality is already there by some fluke (cf. accidental correctness), or my test is incorrect. In either case I can then fine-tune and adapt the assertions to make sure that they fail and thus test some missing functionality.</p>
<p>Two conundrums solved.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsesource.com/blogs/2009/02/17/unit-testing-revelations/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

