<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
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/"
> <channel><title>Comments on: Returning Parameters in JMock 2</title> <atom:link href="http://www.symphonious.net/2010/03/09/returning-parameters-in-jmock-2/feed/" rel="self" type="application/rss+xml" /><link>http://www.symphonious.net/2010/03/09/returning-parameters-in-jmock-2/</link> <description>Living in a state of accord.</description> <lastBuildDate>Tue, 07 Feb 2012 01:07:58 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>By: Adrian Sutton</title><link>http://www.symphonious.net/2010/03/09/returning-parameters-in-jmock-2/comment-page-1/#comment-181238</link> <dc:creator>Adrian Sutton</dc:creator> <pubDate>Wed, 05 Jan 2011 14:53:43 +0000</pubDate> <guid
isPermaLink="false">http://www.symphonious.net/?p=1312#comment-181238</guid> <description>Nice. Not sure why I didn&#039;t notice the TypeSafeMatcher class when I was originally doing this.</description> <content:encoded><![CDATA[<p>Nice. Not sure why I didn&#8217;t notice the TypeSafeMatcher class when I was originally doing this.</p> ]]></content:encoded> </item> <item><title>By: IndividualRich</title><link>http://www.symphonious.net/2010/03/09/returning-parameters-in-jmock-2/comment-page-1/#comment-181237</link> <dc:creator>IndividualRich</dc:creator> <pubDate>Wed, 05 Jan 2011 14:49:29 +0000</pubDate> <guid
isPermaLink="false">http://www.symphonious.net/?p=1312#comment-181237</guid> <description>I like your solution but I&#039;m not a fan of catching the ClassCastException. Here&#039;s a revision using TypeSafeMatcher that gets rid of the try-catch;
class CapturingMatcher extends TypeSafeMatcher implements Action {
public T captured;
public void describeTo(Description description) {
description.appendText(&quot;captured value &quot;);
description.appendValue(captured);
}
public Object invoke(Invocation invocation) throws Throwable {
return captured;
}
@Override
public boolean matchesSafely(T arg) {
captured = (T)arg;
return true;
}
}</description> <content:encoded><![CDATA[<p>I like your solution but I&#8217;m not a fan of catching the ClassCastException. Here&#8217;s a revision using TypeSafeMatcher that gets rid of the try-catch;</p><p> class CapturingMatcher extends TypeSafeMatcher implements Action {</p><p> public T captured;</p><p> public void describeTo(Description description) {<br
/> description.appendText(&#8220;captured value &#8220;);<br
/> description.appendValue(captured);<br
/> }</p><p> public Object invoke(Invocation invocation) throws Throwable {<br
/> return captured;<br
/> }</p><p> @Override<br
/> public boolean matchesSafely(T arg) {<br
/> captured = (T)arg;<br
/> return true;<br
/> }</p><p> }</p> ]]></content:encoded> </item> <item><title>By: Adrian Sutton</title><link>http://www.symphonious.net/2010/03/09/returning-parameters-in-jmock-2/comment-page-1/#comment-174674</link> <dc:creator>Adrian Sutton</dc:creator> <pubDate>Wed, 10 Mar 2010 11:07:06 +0000</pubDate> <guid
isPermaLink="false">http://www.symphonious.net/?p=1312#comment-174674</guid> <description>I suppose you could assert that it&#039;s passing in a specific default value just as easily and then you&#039;d know what to return, but that defines more about the implementation than I think a test really should (at least in this case).  I probably would have just done that if I didn&#039;t already have the capturing matcher around to make my life easier with complex asserts.  In this case the mock is half stub/half mock so I want to be as loose as possible in what method calls and arguments are used (mostly) and just assert that the result is correct.</description> <content:encoded><![CDATA[<p>I suppose you could assert that it&#8217;s passing in a specific default value just as easily and then you&#8217;d know what to return, but that defines more about the implementation than I think a test really should (at least in this case).  I probably would have just done that if I didn&#8217;t already have the capturing matcher around to make my life easier with complex asserts.  In this case the mock is half stub/half mock so I want to be as loose as possible in what method calls and arguments are used (mostly) and just assert that the result is correct.</p> ]]></content:encoded> </item> <item><title>By: Steve Freeman</title><link>http://www.symphonious.net/2010/03/09/returning-parameters-in-jmock-2/comment-page-1/#comment-174673</link> <dc:creator>Steve Freeman</dc:creator> <pubDate>Wed, 10 Mar 2010 11:00:19 +0000</pubDate> <guid
isPermaLink="false">http://www.symphonious.net/?p=1312#comment-174673</guid> <description>Thanks for posting the tip. describeTo() is intended to help you understand what has happened when it goes wrong.
I&#039;m curious, though, as to why this trick is necessary in a unit test. Surely you know what the expected default value should be?</description> <content:encoded><![CDATA[<p>Thanks for posting the tip. describeTo() is intended to help you understand what has happened when it goes wrong.<br
/> I&#8217;m curious, though, as to why this trick is necessary in a unit test. Surely you know what the expected default value should be?</p> ]]></content:encoded> </item> <item><title>By: Adrian Sutton</title><link>http://www.symphonious.net/2010/03/09/returning-parameters-in-jmock-2/comment-page-1/#comment-174672</link> <dc:creator>Adrian Sutton</dc:creator> <pubDate>Wed, 10 Mar 2010 09:47:44 +0000</pubDate> <guid
isPermaLink="false">http://www.symphonious.net/?p=1312#comment-174672</guid> <description>Yeah I&#039;ve seen Mockito, there&#039;s not really enough that&#039;s better to justify rewriting everything.  JMock 2 has a very friendly syntax, with the only weird bit being the context.checking wrapper.  I think the JMock API is actually much easier to read than Mockito in many ways, mostly because it uses full words (equal instead of eq) and better matching normal lingo - e.g. will(returnValue(...)) instead of thenAnswer since Java returns things it doesn&#039;t answer.
It&#039;s all much a muchness though.</description> <content:encoded><![CDATA[<p>Yeah I&#8217;ve seen Mockito, there&#8217;s not really enough that&#8217;s better to justify rewriting everything.  JMock 2 has a very friendly syntax, with the only weird bit being the context.checking wrapper.  I think the JMock API is actually much easier to read than Mockito in many ways, mostly because it uses full words (equal instead of eq) and better matching normal lingo &#8211; e.g. will(returnValue(&#8230;)) instead of thenAnswer since Java returns things it doesn&#8217;t answer.</p><p>It&#8217;s all much a muchness though.</p> ]]></content:encoded> </item> <item><title>By: Nathan</title><link>http://www.symphonious.net/2010/03/09/returning-parameters-in-jmock-2/comment-page-1/#comment-174671</link> <dc:creator>Nathan</dc:creator> <pubDate>Wed, 10 Mar 2010 03:32:58 +0000</pubDate> <guid
isPermaLink="false">http://www.symphonious.net/?p=1312#comment-174671</guid> <description>Slightly off topic, but from this post note that you&#039;re using Jmock for mocking framework.  Have you ever looked at Mockito? http://code.google.com/p/mockito/
I&#039;ve found it&#039;s much more intuitive than other mocking frameworks.  Supports hamcrest as well, so with similar CapturingMatcher def as you&#039;ve got (implementing Answer instead of Action), you would end up having something like:
when(mockObject.getParameter(eq(&quot;expectedParameterName&quot;), argThat(returnCapturedValue )).thenAnswer(returnCapturedValue);</description> <content:encoded><![CDATA[<p>Slightly off topic, but from this post note that you&#8217;re using Jmock for mocking framework.  Have you ever looked at Mockito? <a
href="http://code.google.com/p/mockito/" rel="nofollow">http://code.google.com/p/mockito/</a></p><p>I&#8217;ve found it&#8217;s much more intuitive than other mocking frameworks.  Supports hamcrest as well, so with similar CapturingMatcher def as you&#8217;ve got (implementing Answer instead of Action), you would end up having something like:</p><p>when(mockObject.getParameter(eq(&#8220;expectedParameterName&#8221;), argThat(returnCapturedValue )).thenAnswer(returnCapturedValue);</p> ]]></content:encoded> </item> </channel> </rss>
