<?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>Jayway Team Blog &#187; easymock</title>
	<atom:link href="http://blog.jayway.com/tag/easymock/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jayway.com</link>
	<description>Sharing Experience</description>
	<lastBuildDate>Tue, 20 Jul 2010 08:26:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>EasyMock: Capturing arguments from multiple calls</title>
		<link>http://blog.jayway.com/2009/03/25/easymock-capturing-arguments-from-multiple-calls/</link>
		<comments>http://blog.jayway.com/2009/03/25/easymock-capturing-arguments-from-multiple-calls/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 16:07:59 +0000</pubDate>
		<dc:creator>Jens Nordahl</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[easymock]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=1339</guid>
		<description><![CDATA[With EasyMock it is, in some cases, difficult to set up sufficiently precise expectations for the mocks. This may be because we don't know in advance with which arguments they get called or how often they get called. Or because the verification, we have to make in the test, involves relating the calls to one mock object to the calls to another mock object or relating calls to mock objects to data returned from the object being tested. 

In such cases the solution may be to capture "traces" of the method calls to the mock objects, and then, after having exercised the object being tested, to make the needed verifications using the captured traces.
]]></description>
			<content:encoded><![CDATA[<p>The EasyMock mocking framework makes it easy to use mocks in tests, to set up precise expectations to mock method calls, and to simulate return values and exceptions thrown from mock methods. </p>
<p>However, in some cases it is difficult to set up sufficiently precise expectations for the mocks</p>
<ul>
<li>because we don't know in advance with which arguments they get called or how often they get called
</li>
<li>or because the verification, we have to make in the test, involves relating the calls to one mock object to the calls to another mock object or relating calls to mock objects to data returned from the object being tested.
</li>
</ul>
<p>In such cases the solution may be to capture "traces" of the method calls to the mock objects, and then, after having exercised the object being tested, to make the needed verifications using the captured traces.</p>
<h3>Example</h3>
<p>Consider as an example a card game simulation with a <code>Dealer</code> object collaborating with multiple <code>Player</code> objects:</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Dealer <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> deal<span style="color: #66cc66;">&#40;</span>Set&lt;Card&gt; deck<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> addPlayer<span style="color: #66cc66;">&#40;</span>Player player<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Player <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> addCard<span style="color: #66cc66;">&#40;</span>Card card<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>The rules for the Dealer are:</p>
<ul>
<li>Each player must get at least two, and at most six cards</li>
<li>Four cards for each player must be dealt in total </li>
<li>Cards cannot disappear, ie. after dealing, any card not given to a player must remain in the deck</li>
<li>Cards dealt to the players must be removed from the deck</li>
<li>The dealer may deal the cards randomly</li>
</ul>
<h3>Naive test</h3>
<p>Now suppose we want to test the Dealer using EasyMock to mock the Players. How can we set up the expectations to test that the Dealer follows the rules? We can expect that the Dealer will call <code>Player.addCard()</code>, but we can't assume in advance exactly how often and with which cards. We may set up expectations like this:</p>
<pre class="java">@Test
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> testDealFor2Players<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    Player player1Mock = createMock<span style="color: #66cc66;">&#40;</span>Player.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;
    player1Mock.<span style="color: #006600;">addCard</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span>Card<span style="color: #66cc66;">&#41;</span> anyObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
    expectLastCall<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">times</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    Player player2Mock = createMock<span style="color: #66cc66;">&#40;</span>Player.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;
    player2Mock.<span style="color: #006600;">addCard</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span>Card<span style="color: #66cc66;">&#41;</span> anyObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
    expectLastCall<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">times</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    replay<span style="color: #66cc66;">&#40;</span>player1Mock, player2Mock<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    Dealer dealer = <span style="color: #000000; font-weight: bold;">new</span> DealerImpl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    dealer.<span style="color: #006600;">addPlayer</span><span style="color: #66cc66;">&#40;</span>player1Mock<span style="color: #66cc66;">&#41;</span>;
    dealer.<span style="color: #006600;">addPlayer</span><span style="color: #66cc66;">&#40;</span>player2Mock<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    Set&lt;Card&gt; deck = createDeck<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    dealer.<span style="color: #006600;">deal</span><span style="color: #66cc66;">&#40;</span>deck<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    verify<span style="color: #66cc66;">&#40;</span>player1Mock, player2Mock<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>With this naive test we only get to test the rule "Each player must get at least two, and at most six cards" - this is clearly not by any measure a good enough test of the Dealer. </p>
<p>In order to test the other rules, we have to somehow relate the actual calls to <code>player1Mock.addCard()</code> to the actual calls to <code>player1Mock.addCard()</code> and to the state of the <code>deck</code> after executing <code>Dealer.deal()</code>.</p>
<h3>Simple capture</h3>
<p>EasyMock has a feature called Capture which allows us to capture the arguments with which mocks are called, such that we can verify the argument values after having exercised the object being tested. It works something like this:</p>
<pre class="java">Player player1Mock = createMock<span style="color: #66cc66;">&#40;</span>Player.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;
Capture&lt;Card&gt; capturedCard = <span style="color: #000000; font-weight: bold;">new</span> Capture&lt;Card&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// Expect one call to addCard with any arg. Capture arg value for later</span>
player1Mock.<span style="color: #006600;">addCard</span><span style="color: #66cc66;">&#40;</span> capture<span style="color: #66cc66;">&#40;</span>capturedCard<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>; 
&nbsp;
<span style="color: #808080; font-style: italic;">// ...</span>
&nbsp;
dealer.<span style="color: #006600;">deal</span><span style="color: #66cc66;">&#40;</span>deck<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Now we can verify what we want about the captured card, eg.</span>
assertEquals<span style="color: #66cc66;">&#40;</span> Card.<span style="color: #006600;">Suit</span>.<span style="color: #006600;">SPADES</span>, capturedCard.<span style="color: #006600;">getValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getSuit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;</pre>
<p>This seems like a step in the right direction. However, using this capture feature is hard when we have to capture multiple values from multiple calls to the mock objects, and doesn't really seem to work at all when we don't know the number of calls in advance.</p>
<h3>Capturing values from multiple calls</h3>
<p>The solution to the problem lies in writing our own EasyMock argument matcher, similar to how EasyMock implements capture internally, but extended so it can accept multiple argument values for multiple methods calls: </p>
<ul>
<li>Create a class <code>MultiCaptureMatcher</code> implementing <code>org.easymock.IArgumentMatcher</code>. The class has a collection  instance variable to collect the argument values. EasyMock delegates to an instance of this class whenever the corresponding mock object method gets called.</li>
<li>Create a static method to be used when setting up expectations - this static method creates an instance of the <code>MultiCaptureMatcher</code> and tells EasyMock to use that matcher for the correponding expected method call.</li>
</ul>
<p>Here is the code:</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MultiCaptureMatcher&lt;T&gt; <span style="color: #000000; font-weight: bold;">implements</span> IArgumentMatcher <span style="color: #66cc66;">&#123;</span>
&nbsp;
    Collection&lt;T&gt; captureDestination;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> MultiCaptureMatcher<span style="color: #66cc66;">&#40;</span>Collection&lt;T&gt; captureDestination<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">captureDestination</span> = captureDestination;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> appendTo<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AStringBuffer+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">StringBuffer</span></a> buffer<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        buffer.<span style="color: #006600;">append</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;multiCapture(&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">append</span><span style="color: #66cc66;">&#40;</span>captureDestination.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">append</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;)&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> matches<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> actual<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        captureDestination.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>T<span style="color: #66cc66;">&#41;</span> actual<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> &lt;S&gt; S multiCapture<span style="color: #66cc66;">&#40;</span>Collection&lt;S&gt; destination<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      reportMatcher<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MultiCaptureMatcher&lt;S&gt;<span style="color: #66cc66;">&#40;</span>destination<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>With this argument matcher we can now set up "expectations" like this:</p>
<pre class="java">Player player1Mock = createMock<span style="color: #66cc66;">&#40;</span>Player.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;
Set&lt;Card&gt; player1Cards = <span style="color: #000000; font-weight: bold;">new</span> HashSet&lt;Card&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
player1Mock.<span style="color: #006600;">addCard</span><span style="color: #66cc66;">&#40;</span> MultiCaptureMatcher.<span style="color: #006600;">multiCapture</span><span style="color: #66cc66;">&#40;</span>player1Cards<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
expectLastCall<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">anyTimes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>I put "expectations" in quotation marks because it is not really an expectation in any real sense, but rather tells EasyMock, that the addCard method may get called any number of times with any arguments - EasyMock shouldn't do any verification, but just capture the arguments for later manual verification. </p>
<p>Here the <code>player1Cards</code> set acts as a destination for captured values. Every time during the test that <code>player1Mock.addCard()</code> gets called, the argument gets added to the <code>player1Cards</code> set. </p>
<p>After the Dealer has been tested, we can then use the captured values in player1Cards to whatever verifications we may want.</p>
<p>Here is a complete example using <code>MultiCaptureMatcher</code> to test all of the Dealer rules:</p>
<pre class="java">@Test
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> testDealFor2Players<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    Player player1Mock = createMock<span style="color: #66cc66;">&#40;</span>Player.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;
    Set&lt;Card&gt; player1Cards = <span style="color: #000000; font-weight: bold;">new</span> HashSet&lt;Card&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    player1Mock.<span style="color: #006600;">addCard</span><span style="color: #66cc66;">&#40;</span> MultiCaptureMatcher.<span style="color: #006600;">multiCapture</span><span style="color: #66cc66;">&#40;</span>player1Cards<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
    expectLastCall<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">anyTimes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    Player player2Mock = createMock<span style="color: #66cc66;">&#40;</span>Player.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;
    Set&lt;Card&gt; player2Cards = <span style="color: #000000; font-weight: bold;">new</span> HashSet&lt;Card&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    player2Mock.<span style="color: #006600;">addCard</span><span style="color: #66cc66;">&#40;</span> MultiCaptureMatcher.<span style="color: #006600;">multiCapture</span><span style="color: #66cc66;">&#40;</span>player2Cards<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
    expectLastCall<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">anyTimes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    replay<span style="color: #66cc66;">&#40;</span>player1Mock, player2Mock<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    Dealer dealer = <span style="color: #000000; font-weight: bold;">new</span> DealerImpl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    dealer.<span style="color: #006600;">addPlayer</span><span style="color: #66cc66;">&#40;</span>player1Mock<span style="color: #66cc66;">&#41;</span>;
    dealer.<span style="color: #006600;">addPlayer</span><span style="color: #66cc66;">&#40;</span>player2Mock<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    Set&lt;Card&gt; deck = createDeck<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #808080; font-style: italic;">// Store original deck for later comparison</span>
    Set&lt;Card&gt; originalDeck = <span style="color: #000000; font-weight: bold;">new</span> HashSet&lt;Card&gt;<span style="color: #66cc66;">&#40;</span>deck<span style="color: #66cc66;">&#41;</span>;
    dealer.<span style="color: #006600;">deal</span><span style="color: #66cc66;">&#40;</span>deck<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    verify<span style="color: #66cc66;">&#40;</span>player1Mock, player2Mock<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Each player must have been dealt 2 to 6 cards</span>
    assertTrue<span style="color: #66cc66;">&#40;</span>player1Cards.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>&gt;=<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
    assertTrue<span style="color: #66cc66;">&#40;</span>player1Cards.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>&lt;=<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
    assertTrue<span style="color: #66cc66;">&#40;</span>player2Cards.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>&gt;=<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
    assertTrue<span style="color: #66cc66;">&#40;</span>player2Cards.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>&lt;=<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// For two players 8 cards must have been dealt</span>
    assertEquals<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span>, player1Cards.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>+player2Cards.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Check that no cards have disappeared, and that any card is either in</span>
    <span style="color: #808080; font-style: italic;">// the deck or belongs to one but not both players</span>
    assertEquals<span style="color: #66cc66;">&#40;</span>originalDeck.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, deck.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>+player1Cards.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>+player2Cards.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    assertEquals<span style="color: #66cc66;">&#40;</span>originalDeck, SetUtils.<span style="color: #006600;">union</span><span style="color: #66cc66;">&#40;</span>deck, player1Cards, player2Cards<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<h3>Caveat</h3>
<p>There is one catch to be aware of with this approach: When an argument object is captured, we capture a reference to the object - not a copy of the objects state. This means that if argument objects get modified after being captured, for example because the object being tested modifies the argument objects after calling the mocks, then the verification of captured objects will see the modified object state instead of the object state at the time of capture.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/03/25/easymock-capturing-arguments-from-multiple-calls/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>What&#8217;s new in PowerMock 1.1</title>
		<link>http://blog.jayway.com/2008/12/15/whats-new-in-powermock-11/</link>
		<comments>http://blog.jayway.com/2008/12/15/whats-new-in-powermock-11/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 09:21:13 +0000</pubDate>
		<dc:creator>Johan Haleby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[commons-logging]]></category>
		<category><![CDATA[easymock]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[jmock]]></category>
		<category><![CDATA[log4j]]></category>
		<category><![CDATA[mockito]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[powermock]]></category>
		<category><![CDATA[slf4j]]></category>
		<category><![CDATA[testng]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=586</guid>
		<description><![CDATA[We're proud to announce that PowerMock 1.1 was released a couple of days ago and it has many new interesting features. Framework independence First of all the internal structure of the project has undergone major changes. PowerMock core is no longer coupled to EasyMock which means that PowerMock can now be used as a foundation [...]]]></description>
			<content:encoded><![CDATA[<p>We're proud to announce that <a href="http://www.powermock.org">PowerMock</a> 1.1 was released a couple of days ago and it has many new interesting features. </p>
<h3>Framework independence</h3>
<p>First of all the internal structure of the project has undergone major changes. PowerMock core is no longer coupled to EasyMock which means that PowerMock can now be used as a foundation for testability for other mock frameworks. Our intention is that in the future you pick a combination of a mock extension API of choice (such as EasyMock, Mockito or JMock) and a test framework of choice (such as JUnit or TestNG) to enable increased testability for your code. With the new release PowerMock has some basic support for <a href="http://www.mockito.org">Mockito</a> using the Mockito extension api. This means that you can mock final and static methods, use annotations, enable partial mocking, mocking of signed classes even if they're package private and spying of final classes and methods. Expect more features in the upcoming releases! It's possible to create your own extension API's as well so you can benefit from the PowerMock features in virtually any mock framework.</p>
<h3>Test listeners</h3>
<p>Another noteworthy feature of the new release is the test listener support. This means that you can create listeners and get notifications of various events, such as before a test method is about to execute or the outcome of an executed test method etc. The purpose of these test listeners is to provide a framework-independent way to get and react to these notifications by implementing the <code>org.powermock.core.spi.PowerMockTestListener</code> and pass it to the <code>PowerMockListener</code> annotation. PowerMock has some built-in test listeners for you to use, for example it has an <code>AnnotationEnabler</code> that let's you use annotations to create your mock objects. For example consider the following piece of code:</p>
<pre>
@RunWith(PowerMockRunner.class)
@PowerMockListener(AnnotationEnabler.class)
 public class PersonServiceTest {

 	@Mock
  	private PersonDao personDaoMock;

  	private PersonService classUnderTest;

  	@Before
  	public void setUp() {
  		classUnderTest = new PersonService(personDaoMock);
  	}
   ...
  }
</pre>
<p>Using the <code>@Mock</code> annotation eliminates the need to setup and tear-down mocks manually which minimizes repetitive test code and makes the test more readable. The <code>AnnotationEnabler</code> works for both the EasyMock and Mockito API's. You can also supply the names of the methods that you wish to mock if you want to create a partial mock, for example:</p>
<pre>
@RunWith(PowerMockRunner.class)
@PowerMockListener(AnnotationEnabler.class)
 public class PersonServiceTest {

 	@Mock("getPerson")
  	private PersonDao personDaoMock;

  	private PersonService classUnderTest;

  	@Before
  	public void setUp() {
  		classUnderTest = new PersonService(personDaoMock);
  	}
   ...
  }
</pre>
<p>This piece of code will instruct PowerMock to create a partial mock of the PersonDao where only the "getPerson" method is mocked. Since EasyMock has support for nice and strict mocks you can use the <code>@MockNice</code> and <code>@MockStrict</code> annotations to get the benefits of this.</p>
<h3>Mock policies</h3>
<p>One really cool feature of the 1.1 release is something that we refer to as mock policies. A Mock Policy can be used to make it easier to unit test some code with PowerMock in isolation from a certain framework. A mock policy implementation can for example suppress some methods, suppress static initializers or intercept method calls and change their return value (for example to return a mock object) for a certain framework or set of classes or interfaces. A mock policy can for example be implemented to avoid writing repetitive setup code for your tests. Say that you're using a framework X that in order for you to test it requires that certain methods should always return a mock implementation. Perhaps some static initializers must be suppressed as well. Instead of copying this code between tests it would be a good idea to write a reusable mock policy. </p>
<p>PowerMock 1.1 provides three mock policies out of the box for mocking slf4j, java commons-logging and log4j. Let's pick slf4j as an example and let's say you have a class that looks like this:</p>
<pre>
public class Slf4jUser {
        private static final Logger log = LoggerFactory.getLogger(Slf4jUser.class);

        public final String getMessage() {
                log.debug("getMessage!");
                return "log4j user";
        }
}
</pre>
<p>To stub all calls to the logger you can use the Slf4j mock policy that takes care of doing all necessary setup for you. You use it like this:</p>
<pre>
@RunWith(PowerMockRunner.class)
@MockPolicy(Slf4jMockPolicy.class)
public class Slf4jUserTest {
     ...
}
</pre>
<p>Note that we don't have do any setup at all to mock slf4j, the Slf4jMockPolicy takes care of this.</p>
<p>Mock policies can also be chained or nested like this:</p>
<pre>
@RunWith(PowerMockRunner.class)
@MockPolicy({MockPolicyX.class, MockPolicyY.class})
public class MyTest {
    ...
}
</pre>
<p>You can of course create your own mock policies by implementing the <code>org.powermock.core.spi.PowerMockPolicy</code> interface. For more information on mock policies please have a look at the <a href="http://code.google.com/p/powermock/wiki/MockPolicies">documentation</a>. </p>
<h3>Other</h3>
<p>There are of course many other smaller features and improvements such as the ability to suppress individual fields and better support for resetting mocks in the EasyMock API. You can read about all the new features and bug fixes in the <a href="http://powermock.googlecode.com/svn/trunk/changelog.txt">changelog</a>. Please visit our <a href="http://www.powermock.org">web-page</a> for more information and downloads.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2008/12/15/whats-new-in-powermock-11/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PowerMock 1.0 released</title>
		<link>http://blog.jayway.com/2008/11/21/powermock-10-released/</link>
		<comments>http://blog.jayway.com/2008/11/21/powermock-10-released/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 20:54:42 +0000</pubDate>
		<dc:creator>Jan Kronquist</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[easymock]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[oredev]]></category>
		<category><![CDATA[powermock]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=456</guid>
		<description><![CDATA[We have released 1.0 of PowerMock just in time for the Øredev conference! PowerMock is an open source mock framework based on EasyMock that allow you to mock static methods, private methods and even constructors. Our intent is mainly to allow unit testing of legacy code and people really seemed to appreciate this idea at the conference.]]></description>
			<content:encoded><![CDATA[<p>We have released 1.0 of <a href="http://www.powermock.org">PowerMock</a> just in time for the <a href="http://www.oredev.org/">Øredev</a> conference! PowerMock is an open source mock framework based on <a href="http://www.easymock.org/">EasyMock</a> that allow you to mock static methods, private methods and even constructors. Our intent is mainly to allow unit testing of legacy code and people really seemed to appreciate this idea at the conference.</p>
<p>We now think that the API is stable enough and we have also tested PowerMock in a couple of projects, but of course since we were presenting at the conference it is no coincidence that we released PowerMock 1.0 just days before. <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>The details of the changes can be found in the <a href="http://powermock.googlecode.com/svn/trunk/changelog.txt">changelog</a> but here are some of the highlights since <a href="http://blog.jayway.com/2008/10/27/powermock-08-released/">PowerMock 0.8</a>:</p>
<ul>
<li>The ability to <a href="http://code.google.com/p/powermock/wiki/BypassEncapsulation">set and get internal state</a> based on an object type. This means that you get more refactor friendly code while still being able to set internal state. </li>
<li>Implemented <a href="http://code.google.com/p/powermock/wiki/ReplayAllAndVerifyAll">replayAll and verifyAll</a>. These can be used to replay and verify all mocks created by PowerMock without having to explicitly specify each and everyone. Thanks to <a href="http://www.agilejava.eu/">Ivar Grimstad</a> for suggesting this!</li>
<li>Added support for specifying parameter types for expectNew. <a href="http://code.google.com/p/powermock/wiki/MockConstructor">Mocking constructor calls</a> is a unique feature of PowerMock that people typically find very cool!</li>
</ul>
<p>During the conference I talked to several people and got really good feedback. Everybody seemed to like the idea of being able to unit test legacy code and code that haven't been designed for testing. <a href="http://www.two-sdg.demon.co.uk/curbralan/kevlin.html">Kevlin Henney</a> made a good job of <a href="http://www.oredev.org/topmenu/program/tracktest/kevlinhenney.4.3efb083311ac562f9fe800011931.html">defining and describing various types of unit testing</a> during his talk at Øredev and we talked about the difference between essentially untestable code and accidentally untestable code. PowerMock solves the problem with code being untestable for technical reasons (part of the accidentally untestable). With all the great sessions and good discussions Øredev was yet again a great experience!</p>
<p>I have made <a href="http://powermock.googlecode.com/files/PowerMockAtOredev.pdf">the slides from the presentation</a> available at <a href="http://powermock.org">powermock.org</a>. Note that the demo we showed was actually from the trunk and not from the 1.0 release!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2008/11/21/powermock-10-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerMock 0.8 released</title>
		<link>http://blog.jayway.com/2008/10/27/powermock-08-released/</link>
		<comments>http://blog.jayway.com/2008/10/27/powermock-08-released/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 20:37:33 +0000</pubDate>
		<dc:creator>Johan Haleby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[easymock]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[powermock]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=371</guid>
		<description><![CDATA[Previously a colleague of mine described an idea for mocking static methods. Earlier this year we started an open source project to implement these ideas in a very simple to use form. The idea is that it will just be a normal jar file that extends EasyMock and JUnit. Instead of using AspectJ we have [...]]]></description>
			<content:encoded><![CDATA[<p>Previously a colleague of mine described an idea for <a href="http://blog.jayway.com/2007/02/16/static-mock-using-aspectj/">mocking static methods</a>. Earlier this year we started an open source project to implement these ideas in a very simple to use form. The idea is that it will just be a normal jar file that extends EasyMock and JUnit. Instead of using AspectJ we have created our own classloader which means that you don't have to make changes to your build environment. <a href="http://www.powermock.org">PowerMock</a>, as the project is called,  is mostly intended for whitebox unit testing where you know what is going on inside a class or method that you want to test. There is even a class called Whitebox which can be used for simple access to private methods and fields. Besides static mocking we have many other interesting features such as mocking constructors, final classes and methods, private methods, removal of static initializers etc.</p>
<p>We have received some feedback and have also started some evaluation in one of our customer's project. Besides bug-fixes and documentation updates we are starting to stabilize the API and preparing for the 1.0 release. We are also <a href="http://www.oredev.org/topmenu/program/trackjava/johanhalebyjankronqvist.4.3efb083311ac562f9fe80009168.html">presenting PowerMock</a> at this years <a href="http://www.oredev.org/">Øredev</a> conference. You're more than welcome to try it out for yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2008/10/27/powermock-08-released/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Static Mock using AspectJ</title>
		<link>http://blog.jayway.com/2007/02/16/static-mock-using-aspectj/</link>
		<comments>http://blog.jayway.com/2007/02/16/static-mock-using-aspectj/#comments</comments>
		<pubDate>Fri, 16 Feb 2007 09:00:08 +0000</pubDate>
		<dc:creator>Jan Kronquist</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[aspectj]]></category>
		<category><![CDATA[easymock]]></category>
		<category><![CDATA[mock]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=284</guid>
		<description><![CDATA[After seeing <a href="http://mockme.org/">MockME</a> developed by some of my colleagues I started thinking about how this could be made in a generic way.]]></description>
			<content:encoded><![CDATA[<p>After seeing <a href="http://mockme.org/">MockME</a> developed by some of my colleagues I started thinking about how this could be made in a generic way. For example how would we mock the following class?</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StupidSingleton <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> doStatic<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AUnsupportedOperationException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">UnsupportedOperationException</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;method not implemented yet...&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">void</span> sayHello<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Hello&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #993333;">void</span> nativeMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>We would like to write something like this:</p>
<pre class="java">&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> testMocking<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		mock<span style="color: #66cc66;">&#40;</span>StupidSingleton.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;
		expect<span style="color: #66cc66;">&#40;</span>StupidSingleton.<span style="color: #006600;">doStatic</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">17</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">andReturn</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;17&quot;</span><span style="color: #66cc66;">&#41;</span>;
		StupidSingleton.<span style="color: #006600;">sayHello</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		StupidSingleton.<span style="color: #006600;">nativeMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		replay<span style="color: #66cc66;">&#40;</span>StupidSingleton.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		assertEquals<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;17&quot;</span>, StupidSingleton.<span style="color: #006600;">doStatic</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">17</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		StupidSingleton.<span style="color: #006600;">sayHello</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		StupidSingleton.<span style="color: #006600;">nativeMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		verify<span style="color: #66cc66;">&#40;</span>StupidSingleton.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>After playing around with <a href="http://www.eclipse.org/aspectj/">AspectJ</a> I discovered that this is actually possible! Two things are needed:<br />
* An aspect that allows us to catch calls to static methods!<br />
* An implementation of the mock, replay, verify for classes</p>
<p>This turns out to be really easy! The following aspect catches ALL calls to ALL static methods. Might be a bit aggressive, but it works! If you want you can implement your own aspect with a more specialized pointcut.</p>
<pre class="java">&nbsp;
@Aspect
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StaticMockAll <span style="color: #000000; font-weight: bold;">extends</span> StaticMockAspect <span style="color: #66cc66;">&#123;</span>
	@Pointcut<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;call(static * *(..))&quot;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> anyStaticOperation<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
	@Around<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;anyStaticOperation()&quot;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> aroundStaticMethods<span style="color: #66cc66;">&#40;</span>ProceedingJoinPoint jp<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AThrowable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Throwable</span></a> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">aroundStaticMethods</span><span style="color: #66cc66;">&#40;</span>jp<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The mock, replay, verify methods turns out to be trivial:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StaticMock <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">static</span> Map&lt;Class, MockInvocationHandler&gt; mocks = <span style="color: #000000; font-weight: bold;">new</span> HashMap&lt;Class, MockInvocationHandler&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #993333;">void</span> mock<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> type<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		MockInvocationHandler h = <span style="color: #000000; font-weight: bold;">new</span> MockInvocationHandler<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>MocksControl<span style="color: #66cc66;">&#41;</span> EasyMock.<span style="color: #006600;">createControl</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		mocks.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>type, h<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #993333;">void</span> replay<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> type<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		mocks.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>type<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getControl</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">replay</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #993333;">void</span> verify<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> type<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		mocks.<span style="color: #006600;">remove</span><span style="color: #66cc66;">&#40;</span>type<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getControl</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">verify</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Finally, the base class for the aspect that does the actual work is also quite simple:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StaticMockAspect <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> aroundStaticMethods<span style="color: #66cc66;">&#40;</span>ProceedingJoinPoint jp<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AThrowable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Throwable</span></a> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">Class</span> type = jp.<span style="color: #006600;">getSignature</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getDeclaringType</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AInvocationHandler+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">InvocationHandler</span></a> h = getInvocationHandler<span style="color: #66cc66;">&#40;</span>type<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>h == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> jp.<span style="color: #006600;">proceed</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		MethodSignature methodSignature = <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>MethodSignature<span style="color: #66cc66;">&#41;</span>jp.<span style="color: #006600;">getSignature</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">return</span> h.<span style="color: #006600;">invoke</span><span style="color: #66cc66;">&#40;</span>type, methodSignature.<span style="color: #006600;">getMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, jp.<span style="color: #006600;">getArgs</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> MockInvocationHandler getInvocationHandler<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> type<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #66cc66;">&#40;</span>StaticMock.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> StaticMock.<span style="color: #006600;">mocks</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>type<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>h3. Notes</p>
<p>Things to note and think about:<br />
* Static methods and native methods can be mocked! By using the "call" pointcut we change all code that tries to call the native methods and not the native method itself.<br />
* AspectJ is required to make this work. I have been using <a href="http://www.eclipse.org/ajdt/">Eclipse AJDT</a><br />
* It should be possible to replace the Aspect with "plain" byte code manipulation and "just" reload the class. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2007/02/16/static-mock-using-aspectj/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
