<?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>Sat, 28 Jan 2012 15:53:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Spring Integration Tests, Part II, Using Mock Objects</title>
		<link>http://blog.jayway.com/2011/12/12/spring-integration-tests-part-ii-using-mock-objects/</link>
		<comments>http://blog.jayway.com/2011/12/12/spring-integration-tests-part-ii-using-mock-objects/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 20:17:27 +0000</pubDate>
		<dc:creator>Mattias Severson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[easymock]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[mockito]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=10532</guid>
		<description><![CDATA[In the previous post, I wrote how you can use Spring's FactoryBean to facilitate the creation of mock objects for Spring integration tests. Now, it is time to use the EasyMockFactoryBean (in this post EasyMock has been used for creating mock objects, but a similar approach applies to Mockito as well. Start by looking at [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous post, I wrote how you can use Spring's <code>FactoryBean</code> to facilitate the <a href="http://blog.jayway.com/2011/11/30/spring-integration-tests-part-i-creating-mock-objects/">creation of mock objects</a> for Spring integration tests. Now, it is time to use the <a href="https://gist.github.com/1397610#file_easy_mock_factory_bean.java">EasyMockFactoryBean</a> <small>(in this post <a href="http://www.easymock.org">EasyMock</a> has been used for creating mock objects, but a similar approach applies to <a href="http://www.mockito.org">Mockito</a> as well. Start by looking at the <a href="https://gist.github.com/1397610#file_mockito_factory_bean.java">MockitoFactoryBean</a>)</small>.</p>
<p>Next, imagine that the <code>Facade</code> class and the <code>Delegate</code> interface below are part of a bigger system: </p>
<p><script src="https://gist.github.com/1427987.js?file=Facade.java"></script><br />
<script src="https://gist.github.com/1427987.js?file=Delegate.java"></script></p>
<p>Create a matching <a href="https://gist.github.com/1427987#file_easymock_test_config.xml">easymock-test-config.xml</a> <small>(or <a href="https://gist.github.com/1427987#file_movkito_test_config.xml">mockito-test-config.xml</a>)</small>.</p>
<p>Now, we have all the bits and pieces that are needed to start writing the integration test. We start simple to make sure that we autowiring and Spring context has been configured correctly:<br />
<script src="https://gist.github.com/1427987.js?file=InitialTest.java"></script></p>
<p>The test is executed and it passes without any problem.</p>
<p>A second test is added to the same file with some added mock behavior:<br />
<script src="https://gist.github.com/1427987.js?file=SecondTest.java"></script></p>
<p>Excellent, that test also passes.</p>
<p>Now that we have warmed up, we add a third test to the test class:<br />
<script src="https://gist.github.com/1427987.js?file=ThirdTest.java"></script><br />
As expected, this test also passes without any problem.</p>
<p>Unfortunately, that is not always true. A closer investigation reveals that all tests pass when they are executed <i>individually</i>. When executing all three tests (either from the IDE or from a build tool like Maven), the third test <i>fails</i> unexpectedly:</p>
<pre>
java.lang.AssertionError:
  Unexpected method call Delegate.doSomethingElse():
        at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
	at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:85)
	at $Proxy8.doSomethingElse(Unknown Source)
        [...]
</pre>
<p>This is EasyMock telling us that the mock object is in the wrong state. An EasyMock mock object goes through a series of steps:</p>
<ol>
<li><i>Initialization</i> - Instantiate the mock object.</li>
<li><i>Record</i> - Record the expectations of the mock object.</li>
<li><i>Replay</i> - Call <code>replay()</code> on the mock object so that it can replay the recorded state.</li>
<li><i>Test</i> - Execute the test assertions.</li>
<li><i>Verify</i> - Call <code>verify()</code> on the mock object to certify that the recorded mock expectations are fulfilled.</li>
</ol>
<p>In an ordinary unit test, the mock object is thrown away after the <i>verify</i> phase, a new mock instance is created before the next test that can be used to <i>record</i> the new behavior. However, since we are creating a Spring integration test, there is one more thing to consider.</p>
<h2>Reused beans</h2>
<p>In the <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/testing.html#testing-ctx-management">testing chapter</a> of the Spring manual it is stated that:</p>
<blockquote><p>By default, once loaded, the configured ApplicationContext and all of its beans are reused for each test method. Thus the setup cost is incurred only once (per test fixture), and subsequent test execution is much faster.</p></blockquote>
<p>Consequently, all Spring beans, including mocked beans, will preserve their state between different test methods. The mock object will be in the <i>verify</i> state when the last test begins and not in the <i>record</i> state when <code>delegateMock.doSomethingElse()</code> is called. To solve the problem, we need some mechanism to ensure that the mock object is in a known, well-defined state before each test.</p>
<h2>Reset mocks</h2>
<p>Both EasyMock and Mockito provide methods to <code>reset()</code> the state of the mocked objects. Normally, resetting mock objects in the middle of a test method is considered to be a <a href="http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#17">potential code smell</a>, because it is likely that you are testing too much and that the test could be refactored into two separate tests accordingly. Nonetheless, resetting the mock instance <a href="http://junit.sourceforge.net/javadoc/org/junit/Before.html">@Before</a> each test allow us to keep the same mock instance through the entire lifetime of the test class and yet put it in the <i>record</i> state when before each test starts:</p>
<p><script src="https://gist.github.com/1427987.js?file=SetUp.java"></script></p>
<p>When executing all tests in the test class, we see that that the problem has been solved and all tests pass.<br />
For reference, click the link for the full source code of the <a href="https://gist.github.com/1427987#file_easy_mock_facade_test.java">EasyMock test class</a> <small>(and the <a href="https://gist.github.com/1427987#file_mockito_facade_test.java">Mockito test class</a>)</small>.</p>
<h2>Comments</h2>
<p>You may choose to reset the mocks <a href="http://junit.sourceforge.net/javadoc/org/junit/After.html">@After</a> each test, the result will be the same.</p>
<p>The <code>reset()</code> method is called <i>between</i> the different test methods (because of the nature of <code>@After</code> and <code>@Before</code>), so the code smell of calling reset <i>within</i> a test is avoided.</p>
<p>Tests written using Mockito mocks are generally more tolerant for this kind of problem. In contrast to EasyMock, Mockito has no <code>replay()</code> method and no notion of steps. Therefore, it is possible to add additional mock expectations and to replace existing expectations, both after the test has been executed as well as after the result has been verified. Hence, you should always reset your mock objects before each test so that you do not get any accidental mocking behavior.</p>
<p>As a side note, another solution would be to annotate the test class (or all of its test methods) with the <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/test/annotation/DirtiesContext.html">@DirtiesContext</a> annotation. This will effectively recreate the entire application context after each test has been executed, causing all Spring beans to be in their initial state and thus the mock beans to be in the <i>record</i> state. Although this will work, there will be a significant performance penalty compared to the suggested <code>reset()</code> and <code>@Before</code> / <code>@After</code> solution, because all beans in the application context will be recreated and re-wired before each test method. </p>
<h2>Dependencies</h2>
<ul>
<li>Spring 3.0.6</li>
<li>Mockito 1.8.5</li>
<li>jUnit 4.10</li>
<li>EasyMock 3.1</li>
</ul>
<h2>References</h2>
<ul>
<li><a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/testing.html#testing-ctx-management">Spring Reference Manual - Test Context Management and Caching</a></li>
<li><a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/testing.html#testcontext-junit4-runner">Spring Reference Manual - SpringJunit4ClassRunner</a></li>
<li><a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/testing.html#integration-testing-annotations">Spring Reference Manual - ContextConfiguration</a></li>
<li><a href="http://easymock.org/EasyMock3_1_Documentation.html">EasyMock</a></li>
<li><a href="http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html">Mockito</a></li>
</ul>
<p>Previous post: <a href="http://blog.jayway.com/2011/11/30/spring-integration-tests-part-i-creating-mock-objects/">Spring Integration Tests, Part I, Creating Mock Objects</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/12/12/spring-integration-tests-part-ii-using-mock-objects/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Spring Integration Tests, Part I, Creating Mock Objects</title>
		<link>http://blog.jayway.com/2011/11/30/spring-integration-tests-part-i-creating-mock-objects/</link>
		<comments>http://blog.jayway.com/2011/11/30/spring-integration-tests-part-i-creating-mock-objects/#comments</comments>
		<pubDate>Wed, 30 Nov 2011 20:26:05 +0000</pubDate>
		<dc:creator>Mattias Severson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[easymock]]></category>
		<category><![CDATA[factory-method]]></category>
		<category><![CDATA[factorybean]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[mockito]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=10514</guid>
		<description><![CDATA[When writing integration tests with Spring, it can sometimes be convenient to mock one or more of Spring bean dependencies. However, during some circumstances strange things may happen... (In this post, Mockito has been used for creating mock objects, but the same problem applies to EasyMock as well. You can find the corresponding files if [...]]]></description>
			<content:encoded><![CDATA[<p>When writing integration tests with Spring, it can sometimes be convenient to mock one or more of Spring bean dependencies. However, during some circumstances strange things may happen...</p>
<p><small>(In this post, <a href="http://www.mockito.org">Mockito</a> has been used for creating mock objects, but the same problem applies to <a href="http://www.easymock.org">EasyMock</a> as well. You can find the corresponding files if you follow the provided links.)</small></p>
<h2>Test setup</h2>
<p>In the following example the <code>SomeClass</code> needs a reference to an instance of the <code>SomeDependency</code> interface:</p>
<p><script src="https://gist.github.com/1397610.js?file=SomeClass.java"></script></p>
<p><script src="https://gist.github.com/1397610.js?file=SomeDependency.java"></script></p>
<p>When writing the integration test, we use the static  <a href="http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#mock(java.lang.Class)">Mockito.mock(Class classToMock)</a> method to create a mock instance. The straightforward approach would be to let Spring instantiate the mock object with a <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-class-static-factory-method">static factory method</a> in the xml configuration, e.g.</p>
<p><script src="https://gist.github.com/1397610.js?file=failing-mockito-config.xml"></script><br />
<small>(Corresponding <a href="https://gist.github.com/1397610#file_failing_easymock_config.xml">EasyMock config</a>.)</small></p>
<p>To verify that the beans are created and wired correctly, we write a simple test:</p>
<p><script src="https://gist.github.com/1397610.js?file=BeanWiringTest.java"></script><br />
<small>(Same test for EasyMock, just update the <code>@ContextConfiguration</code> with the EasyMock config file.)</code></small></p>
<p>To our surprise, the test fails:</p>
<pre>
[...]
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'someClass': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
    Could not autowire field: private com.jayway.example.SomeDependency com.jayway.example.SomeClass.someDependency;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
    No matching bean of type [com.jayway.example.SomeDependency] found for dependency:
    expected at least 1 bean which qualifies as autowire candidate for this dependency.
    Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
[...]
</pre>
<h2>Explanation</h2>
<p>In order to find a solution to the problem, we need to understand Spring's initialization process:</p>
<ol>
<li>
<i>Load bean definitions.</i><br />
This step includes scanning the application context XML files, scanning packages defined by <code>component-scan</code>, and loading the bean definitions found into the bean factory. (BeanFactoryPostProcessors such as the PropertyPlaceHolderConfigurer may be called to update the bean definitions.)
</li>
<li>
<i>Instantiate beans and store them in the application context.</i><br />
The bean factory creates the bean from the bean definitions. Bean dependencies get injected.
</li>
<li>
<i>Bean post processing.</i><br />
All initializing methods (e.g. annotated with <code>@PostConstruct</code>, <code>init-method</code>s declared in the XML and the <code>afterPropertiesSet()</code> method will execute. Annotations such as <code>@Required</code> will be validated. Dynamic proxies in an AOP environment will be created and so on.
</li>
</ol>
<p>The problem that we see occurs due to a corner case in the first two steps. When the bean definition is created Spring scans through the application context XML file. It finds the declaration of the mock bean and a reflective call is made to find out the return type of the method defined by the <code>factory-method</code> declaration in an attempt to determine the bean's type. The return type of this method is declared as the formal type parameter <code>&lt;T&gt;</code> (remember that we use the <a href="http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#mock(java.lang.Class)">Mockito.mock(Class&lt;T&gt; classToMock)</a> as factory method). Consequently, the bean definition of <code>someDependencyMock</code> will be of <code>java.lang.Object</code> and not <code>SomeDependency</code> as one would think.</p>
<p>In the second step, the <code>someClass</code> bean is instantiated. Springs discovers the <code>@Autowired</code> annotation and tries to fetch an existing <code>SomeDependency</code> bean from the application context. None has yet been created, so the bean factory proceeds and looks for suitable bean definition in order to create an instance of the requested bean on the fly. This operation fails because the bean definition of <code>someDependency</code> is mapped to <code>java.lang.Object</code>. At this point, Spring bails out and throws the <code>NoSuchBeanDefinitionException</code>.  </p>
<h2>FactoryBean to the rescue</h2>
<p>The problem is solved by implementing a custom <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-extension-factorybean">FactoryBean</a> that can be used whenever we need to mock an object:</p>
<p><script src="https://gist.github.com/1397610.js?file=MockitoFactoryBean.java"></script><br />
<small>(In the <a href="https://gist.github.com/1397610#file_easy_mock_factory_bean.java">EasyMockFactoryBean</a>, I have added an enum to define the type of mock object.)</small></p>
<p>Next, the Spring test config is updated to use the factory bean:</p>
<p><script src="https://gist.github.com/1397610.js?file=mockito-factory-bean.xml"></script><br />
<small>(Corresponding <a href="https://gist.github.com/1397610#file_easymock_factory_bean.xml">EasyMock config</a>.)</small></p>
<p>Spring will still instantiate the beans in the order that they were declared in the <code>mockito-test-config.xml</code>, i.e. starting by creating an instance of <code>SomeClass</code> and then attempt to inject an instance of a <code>SomeDependency</code> into it before any has been created. The good news is that bean factory will scan through all declared <code>FactoryBean</code>s, calls its <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/FactoryBean.html#getObjectType()">getObjectType()</a> method, and if the returned type is correct, call its <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/FactoryBean.html#getObject()">getObject()</a> which should return an instance of the requested type.</p>
<h2>Alternative solution</h2>
<p>There is another more brittle solution to the problem. By simply changing the order of the tags in the initial application context, e.g. putting the bean declaration that defines the mock above the component scan, will cause the test to pass. The bean definition will still be wrong in the sense that the <code>someDependencyMock</code> will be defined as a <code>java.lang.Object</code>, but there is another important difference. In this case, the bean factory will invoke the <a href="http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#mock(java.lang.Class)">Mockito.mock(Class&lt;T&gt; classToMock)</a> method with <code>SomeDependency</code> as parameter causing the <code>someDependencyMock</code> bean to be created and subsequently stored in the application context. Later, the <code>someClass</code> bean is created and the dependency can obtained from the application context when requested by the bean factory and the wiring of the beans will succeed. However, making sure that the order of the bean declarations in the XML is always correct may neither be simple nor obvious, and it will get increasingly more difficult the more beans you add to the configuration.</p>
<h2>Disclaimer</h2>
<p>As stated in the in the first paragraph, I would only consider this solution for <i>integration</i> tests because of performance reasons. The application context itself adds some overhead, but the big performance hit is to create and wire all beans that are associated with it. When writing <i>unit</i> tests there is no need for this, so you could revert to constructor or setter dependency injection, Spring's <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/test/util/ReflectionTestUtils.html#setField%28java.lang.Object,%20java.lang.String,%20java.lang.Object%29">ReflectionTestUtils.setField()</a>, PowerMock's <a href="http://powermock.googlecode.com/svn/docs/powermock-1.4.10/apidocs/org/powermock/reflect/Whitebox.html#setInternalState%28java.lang.Object,%20java.lang.Class,%20java.lang.Object%29">Whitebox.setInternalState()</a>, Mockito's <a href="http://docs.mockito.googlecode.com/hg/org/mockito/InjectMocks.html">@InjectMocks</a> or similar techniques.</p>
<h2>Acknowledgements</h2>
<p>Thanks to <a href="http://stackoverflow.com/users/97641/wilhelm-kleu">Wilhelm Kleu</a> at stack overflow for <a href="http://stackoverflow.com/questions/6340007/autowiring-of-beans-generated-by-easymock-factory-method/6340391#6340391">suggesting</a> the solution and to <a href="http://blog.jayway.com/author/mattiasarthursson/">Mattias Hellborg Arthursson</a> for valuable discussions.</p>
<h2>Dependencies</h2>
<ul>
<li>Spring 3.0.6</li>
<li>Mockito 1.8.5</li>
<li>jUnit 4.10</li>
<li>EasyMock 3.1</li>
</ul>
<h2>References</h2>
<ul>
<li><a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-class-static-factory-method">Spring Reference Manual - Static Factory Method</a></li>
<li><a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-extension-factorybean">Spring Reference Manual - FactoryBean</a></li>
<li><a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/testing.html#integration-testing-annotations">Spring Reference Manual - Integration Testing</a></li>
<li><a href="http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html">Mockito</a></li>
<li><a href="http://easymock.org/EasyMock3_1_Documentation.html">EasyMock</a></li>
</ul>
<p>Next post: <a href="http://blog.jayway.com/2011/12/12/spring-integration-tests-part-ii-using-mock-objects/">Spring Integration Tests, Part II, Using Mock Objects</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/11/30/spring-integration-tests-part-i-creating-mock-objects/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<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>1</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>

