From: Patricia Shanahan on
Mike Schilling wrote:
> Joshua Cranmer wrote:
>> Mike Schilling wrote:
>>> I'm also wondering why Longs and Integers insread of longs and
>>> ints.
>>> (assertEquals(long, int) would unambiguously call the (long, long)
>>> overload).Without seeing the code it's hard to tell, but I wonder
>>> if
>>> it should be using primitives, but autoboxing had made using
>>> objects
>>> instead transparent until this cropped up.
>> A (Long, Integer) can be passed into a (long, long) method call, so
>> it's not necessarily the autounboxing per se that's causing the
>> problem.
>
> The problem, I suspect, is that assertEquals() has many, many
> overloads, and the result is that which one to call is ambiguous .
>
>

There is a simple approach to assertEquals that always works, without
having to spend time rereading the method selection rules in the JLS.

Decide the appropriate type for the comparison. Cast/box/unbox etc. to
force both the expect and actual arguments to be expressions of that
type. In the OP's case, I think the appropriate type for the comparison
is long, so I would use the longValue methods to get a long from each of
i and l.

Patricia
From: Mike Schilling on
Patricia Shanahan wrote:

>
> There is a simple approach to assertEquals that always works,
> without
> having to spend time rereading the method selection rules in the
> JLS.
>
> Decide the appropriate type for the comparison. Cast/box/unbox etc.
> to
> force both the expect and actual arguments to be expressions of that
> type. In the OP's case, I think the appropriate type for the
> comparison is long, so I would use the longValue methods to get a
> long from each of i and l.

Unless null is a possible and perhaps expected result. Then it's a
bit more complicated.


From: Pitch on
In article <b3d89e90-d8d7-4c1a-8c51-fcba40e41d20
@y36g2000yqh.googlegroups.com>, grz01(a)spray.se says...
>
> Hi
> In JUnit I want to compare an Integer with a Long, as 2 Integers --
> say like this:
>
> Integer i = ...;
> Long l = ...;
> assertEquals(i,l); // assertEquals is ambigous, doesnt work

Maybe you should redesign the code so they are the same type.
From: Lew on
grz01 says...
>> In JUnit I want to compare an Integer with a Long, as 2 Integers --
>> say like this:
>>
>> Integer i = ...;
>> Long l = ...;
>> assertEquals(i,l); // assertEquals is ambigous, doesnt work

Pitch wrote:
> Maybe you should redesign the code so they are the same type.

grz01 already told us two days ago:
> This was a simplified example I wrote only to demonstrate the problem,
> but in the actual code I have here, the objects and types come from
> third-party frameworks we are using, so I cant use your advice re. int/
> long, etc.
> The Integer and Long types are what they are and cannot be changed.

--
Lew
From: markspace on
Morris Keesan wrote:

>>> Integer i = ...;
>>> Long l = ...;
>>> assertEquals(i,l); // assertEquals is ambigous, doesnt work

> or simply sidestep the issue by replacing the assertEquals with
> assertTrue(i == l);


Will that work if i and l are objects instead of primitives, as has
already been indicated?

Also, I like to use assertEquals if possible, because it provides more
information on a failure.

First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9
Prev: JDK 1.5.0_21 released
Next: LWUIT and J2ME