<?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>btaz &#187; Testing</title>
	<atom:link href="http://www.btaz.com/category/java/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.btaz.com</link>
	<description>Promoting the art of coding</description>
	<lastBuildDate>Fri, 16 Jul 2010 17:01:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JUnit 4 error: reference to assertEquals is ambiguous</title>
		<link>http://www.btaz.com/java/junit-4-error-reference-to-assertequals-is-ambiguous/</link>
		<comments>http://www.btaz.com/java/junit-4-error-reference-to-assertequals-is-ambiguous/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 19:56:21 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[JUnit]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[ambiguous]]></category>
		<category><![CDATA[assertEqual]]></category>

		<guid isPermaLink="false">http://www.btaz.com/?p=67</guid>
		<description><![CDATA[This is a somewhat confusing compilation failure that sometimes happen when you write unit tests using JUnit 4. This is an example code snippet that produces this error (result.getValue() returns an Integer object):
assertEquals(12345, result.getValue());
And when you try to compile your project it produces a compilation error like this:
/projects/myapp/src/test/java/org/myapp/MyTest.java:[88,8] reference to assertEquals is ambiguous, both method [...]


Related posts:<ol><li><a href='http://www.btaz.com/java/java-primitive-data-type-sizes-for-byte-short-int-long-float-double-and-char/' rel='bookmark' title='Permanent Link: Java primitive data type sizes for byte, short, int, long, float, double and char'>Java primitive data type sizes for byte, short, int, long, float, double and char</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>This is a somewhat confusing compilation failure that sometimes happen when you write unit tests using JUnit 4. This is an example code snippet that produces this error (result.getValue() returns an Integer object):</p>
<pre>assertEquals(12345, result.getValue());</pre>
<p>And when you try to compile your project it produces a compilation error like this:</p>
<p><span style="color: #0000ff;">/projects/myapp/src/test/java/org/myapp/MyTest.java:[88,8] reference to assertEquals is ambiguous, both method assertEquals(double,double) in org.junit.Assert and method assertEquals(java.lang.Object,java.lang.Object) in org.junit.Assert match</span></p>
<p>Since JUnit offer several methods that are very similar the compiler can not always determine which one to use. In our case we have the number 12345 which is an int and result.getValue() which is an Integer. You&#8217;d think that the compiler could figure out to convert the number 12345 to an Integer object which would give us a match on &#8220;assertEquals(java.lang.Object, java.lang.Object) method, but instead it throws the above error.</p>
<p>The reason for this is that Java uses best match to try to determine the method that will require the least conversion of the parameters. In this case one method requires boxing, and the other method would require un-boxing. Both of these methods have equal priority and that&#8217;s why there is ambiguity since none of the methods are more specific than the other.</p>
<p>The solution is to help the compiler determine what it need to do. We can do that using this:</p>
<pre>assertEquals(12345, (int) result.getValue());</pre>
<p>or this:</p>
<pre>
<pre>assertEquals((Integer)12345, result.getValue());</pre>
</pre>
<p>to resolve this problem. The second solution that converts 12345 to an Integer object is most likely the better once since Java will auto-box a null value to the number 0 which may not be the correct behavior for the class you&#8217;re testing.</p>


<p>Related posts:<ol><li><a href='http://www.btaz.com/java/java-primitive-data-type-sizes-for-byte-short-int-long-float-double-and-char/' rel='bookmark' title='Permanent Link: Java primitive data type sizes for byte, short, int, long, float, double and char'>Java primitive data type sizes for byte, short, int, long, float, double and char</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.btaz.com/java/junit-4-error-reference-to-assertequals-is-ambiguous/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Mockito an alternative to JMock</title>
		<link>http://www.btaz.com/java/mockito-an-alternative-to-jmock/</link>
		<comments>http://www.btaz.com/java/mockito-an-alternative-to-jmock/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 05:35:22 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.btaz.com/?p=38</guid>
		<description><![CDATA[I&#8217;ve used JMock for quite some time and I&#8217;ve found it to be a great and very useful mocking framework for unit testing. I was never quite happy with the syntax though; especially the part where you specify expectations.  Here&#8217;s a JMock snippet illustrating an expectation and a return value from a mocked object
context.checking(new Expectations() [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve used JMock for quite some time and I&#8217;ve found it to be a great and very useful mocking framework for unit testing. I was never quite happy with the syntax though; especially the part where you specify expectations.  Here&#8217;s a JMock snippet illustrating an expectation and a return value from a mocked object</p>
<pre><span style="color: #0000ff;">context.checking(new Expectations() {{
    oneOf (department).employees(); will(returnIterator(employees));
}});</span></pre>
<p>In Mockito the equivalent code would look like this</p>
<pre><span style="color: #0000ff;">when(department.employees()).thenReturn(employees);</span></pre>
<p>I find the Mockito syntax to be easier to write and understand. I&#8217;ve seen fellow developers implement JMock expectations in a way where they actually didn&#8217;t perform a useful test, and I think the sometimes confusing syntax of JMock was the reason. I&#8217;m not dismissing <a href="http://www.jmock.org/">JMock</a> as an inferior mocking framework, but I do think that the learning curve is a bit steeper. If you haven&#8217;t used <a title="Mockito" href="http://www.mockito.org/">Mockito</a> yet I encourage you to take a look at it.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.btaz.com/java/mockito-an-alternative-to-jmock/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
