PHP 1, Java 0: The method assertEquals(Object, Object) is ambiguous for the type
Th old Ashley would never say this, maybe I’ve been working with PHP too long, but seriously, I’m going to have to side with the dynamic language crowd on this one – I think I have been spending too much time developing with Magento!
After an Eclipse upgrade a whole raft of my unit tests started failing to compile with the error: The method assertEquals(Object, Object) is ambiguous for the type. What this means is I’m passing an int and and Integer into a method that has two different signatures: assertEquals(Object, Object) and assertEquals(int, int) both of which could be called, thanks to autoboxing.
I’m hardly doing a lot of Java coding these days, and relish the chance to get stuck in to a little (truthfully, I still feel like PHP coding is ‘hacking’ compared to a good session on Java). But here I am confessing that I honestly cannot be f’ed fixing these errors – I mean seriously, int, and Integer, autoboxing it’s all more work for no gain in this case.
And before anyone responds to tell me that there is a perfectly good reason we have primitives and Objects in Java, and that autoboxing is a great solution to deal with converting between the two. Or that allowing polymorphism through method overloading is a powerful OO tool in Java, I know all of that, I understand it, I just do not want to have to change this:
assertEquals(-11.70, orderItem.getCommission());
into this, 88 times:
assertEquals(new Double(-11.70), orderItem.getCommission());
I hate to moan about a language feature and then not suggest something constructive. How about a version of Java called ‘slow Java‘, where there are no primitives and everything is really an object (some smart ass will tell me it’s called Eiffel right?). If someone clever writes a static analysis tool that will then optimize away the unnecessary new() calls (like where I have an object int that doesn’t need to be on the heap), then It’ll be slower sure, but maybe not impossibly slow? When you can rent cores for 10c an hour, does it really matter anymore? Would slow Java really get spanked in real-world applications? (like my unit tests)
</rant>
You might also be interested in:
- shuffle() or: How I Learned to Stop Worrying and Love PHP
- Magento 1.1.7 Google Checkout and Free Shipping
- My list of PHP language features with hard-to-imagine-use-cases: #1 – The @ symbol that supresses errors
- Taking GWT 1.5 RC1 for a spin – a review of the new GWT release candidate.
- Magento Events Explained and a few Gotchas avoided!
Tagged as autoboxing, dynamic, java, Magento, PHP, static + Categorized as Language Design, PHP, Web Development
![Email Me: ashley.schroder[at]gmail Email Me: ashley.schroder[at]gmail](/wp-content/themes/aschroder/images/email.png)














using assertEquals for doubles is deprecated. You should use a delta/tolerance
If you use assertEquals(double, double, 0.0), the autoboxing ambiguity will take go away as a side benefit.
int/Integer might still be an issue of course…
Thanks Greg, good point re: doubles.
The all-object Java with invisible autoboxing already exists: It’s called Scala. It fixes a number of Java’s annoyances concerning types.