<?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; JUnit</title>
	<atom:link href="http://www.btaz.com/category/java/junit/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>
	</channel>
</rss>
