<?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>JSF Blog &#187; mocking</title>
	<atom:link href="http://www.jsfblog.info/tag/mocking/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jsfblog.info</link>
	<description>Discussion on all things Java and JSF</description>
	<lastBuildDate>Fri, 03 Feb 2012 20:24:46 +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>Mocking Statics and java.lang.IllegalStateException: no last call on a mock available</title>
		<link>http://www.jsfblog.info/2010/02/mocking-statics-and-java-lang-illegalstateexception-no-last-call-on-a-mock-available/</link>
		<comments>http://www.jsfblog.info/2010/02/mocking-statics-and-java-lang-illegalstateexception-no-last-call-on-a-mock-available/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 15:28:02 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[EasyMock]]></category>
		<category><![CDATA[mocking]]></category>
		<category><![CDATA[PowerMock]]></category>

		<guid isPermaLink="false">http://www.jsfblog.info/?p=112</guid>
		<description><![CDATA[If you use PowerMock to mock a static object e.g. and encounter the following error: Check that you have prepared the Class for test using the @PrepareForTest annotation. e.g. An excellent article discussing mocking statics by one of the PowerMock developers can be found here: http://blog.jayway.com/2009/05/17/mocking-static-methods-in-java-system-classes/ Note that you also need to @PrepareForTest any classes [...]]]></description>
			<content:encoded><![CDATA[<p>If you use PowerMock to mock a static object e.g.</p>
<pre class="brush: java; title: ; notranslate">
mockStatic(FacesContext.class)
</pre>
<p>and encounter the following error:</p>
<pre class="brush: plain; title: ; notranslate">
java.lang.IllegalStateException: no last call on a mock available
at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:174)
at org.easymock.EasyMock.expect(EasyMock.java:156)
</pre>
<p>Check that you have prepared the Class for test using the @PrepareForTest annotation.</p>
<p>e.g.</p>
<pre class="brush: java; title: ; notranslate">
@RunWith(PowerMockRunner.class)
@PrepareForTest(
{
	FacesContext.class
})
public class ....
</pre>
<p>An excellent article discussing mocking statics by one of the PowerMock developers can be found here: <a href="http://blog.jayway.com/2009/05/17/mocking-static-methods-in-java-system-classes/">http://blog.jayway.com/2009/05/17/mocking-static-methods-in-java-system-classes/</a></p>
<p>Note that you also need to <a href="http://powermock.googlecode.com/svn/docs/powermock-1.3.5/apidocs/org/powermock/core/classloader/annotations/PrepareForTest.html">@PrepareForTest</a> any classes where you are expecting a private method call with expectPrivate.  If you don&#8217;t, your test execution will try and go into the private method and actually run all the code. The @PrepareForTest annotation can either go at the method level as shown in the example below, or at the class level as shown in the example posted above.</p>
<pre class="brush: java; title: ; notranslate">
@PrepareForTest(ClassUnderTest.class)
@Test
public void testNext() throws Exception
{
	String privateMethod = &quot;isNextEnabled&quot;;
	ClassUnderTest partMock = createPartialMock(ClassUnderTest.class, privateMethod);
	expectPrivate(partMock, privateMethod).andReturn(true);
	replayAll();
	partMock.next();
	verifyAll();
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jsfblog.info/2010/02/mocking-statics-and-java-lang-illegalstateexception-no-last-call-on-a-mock-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chaining EasyMock&#8217;s expectation return values</title>
		<link>http://www.jsfblog.info/2010/02/chaining-easymocks-expectation-return-values/</link>
		<comments>http://www.jsfblog.info/2010/02/chaining-easymocks-expectation-return-values/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 15:53:08 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[EasyMock]]></category>
		<category><![CDATA[mocking]]></category>

		<guid isPermaLink="false">http://www.jsfblog.info/?p=33</guid>
		<description><![CDATA[Recently I discovered quite a handy feature of EasyMock&#8217;s IExpectationSetters andReturn method. You are able to chain the return values so that you can return different values each time that expectation is called. It becomes useful if you consider the following method, where Voucher is an object that cannot be instantiated and needs to be [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I discovered quite a handy feature of <a title="View the EasyMock homepage" href="http://easymock.org/">EasyMock&#8217;s</a> IExpectationSetters <a title="View andReturn method API documentation" href="http://easymock.org/api/easymock/2.5.2/org/easymock/IExpectationSetters.html#andReturn(T)">andReturn</a> method. You are able to chain the return values so that you can return different values each time that expectation is called.</p>
<p>It becomes useful if you consider the following method, where Voucher is an object that cannot be instantiated and needs to be mocked:</p>
<pre class="brush: java; title: ; notranslate">
public class VoucherHelper
{
	public static boolean isVoucherExpired(Voucher voucher)
	{
		boolean expiredVoucher = true;
		Date expiryDate = voucher.getExpiryDate();
		if (expiryDate != null)
		{
			Date now = new Date();
			expiredVoucher = now.after(expiryDate);
		}
		return expiredVoucher;
	}
}
</pre>
<p>To test this method, there are 3 tests that need to be done.</p>
<ul>
<li>expiryDate returning null</li>
<li>expiryDate returning an earlier date than the current date</li>
<li>expiryDate returning a later date than the current date</li>
</ul>
<p>This can all be done in one expectation line by chaining the &#8216;andReturn&#8217; of the expectation.</p>
<pre class="brush: java; title: ; notranslate">
@Test
public final void testIsVoucherExpired()
{
	Voucher voucher = createMock(Voucher.class);
	Calendar expiredDate = Calendar.getInstance();
	// Make it expired by taking a year off
	expiredDate.set(Calendar.YEAR, expiredDate.get(Calendar.YEAR) - 1);

	Calendar notExpired = Calendar.getInstance();
	// Make it not expired by adding a year
	notExpired.set(Calendar.YEAR, notExpired.get(Calendar.YEAR) + 1);

	// Test with date returning null, then returning expired, then returning not expired
	expect(voucher.getExpiryDate())
		.andReturn(null)
		.andReturn(expiredDate.getTime())
		.andReturn(notExpired.getTime());
	replayAll();
	assertTrue(VoucherHelper.isVoucherExpired(voucher));
	assertTrue(VoucherHelper.isVoucherExpired(voucher));
	assertFalse(VoucherHelper.isVoucherExpired(voucher));
	verifyAll();
}
</pre>
<p>EasyMock also allows chaining on the <a title="View times method API documentation" href="http://easymock.org/api/easymock/2.5.2/org/easymock/IExpectationSetters.html#times%28int%29">times</a> method and the <a title="View andThrow method API documentation" href="http://easymock.org/api/easymock/2.5.2/org/easymock/IExpectationSetters.html#andThrow%28java.lang.Throwable%29">andThrow</a> method, and all can be used at the same time if you wanted.</p>
<pre class="brush: java; title: ; notranslate">
expect(voucher.getExpiryDate())
	.andReturn(null).times(2)
	.andThrow(new RuntimeException())
	.andReturn(new Date()).times(3);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jsfblog.info/2010/02/chaining-easymocks-expectation-return-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

