Re: Integer / Long comparison
markspace wrote:
Mike Schilling wrote:
Now, when a test calls assertEquals(Boolean, Quaternion), it'll find
an exact match.
I think the OP could just make one assertEquals( Object, Object )
method, and call the toString of each object. Then compare based on the
strings.
There would be no need to create a new assertEquals. Junit's
assertEquals already handles two String arguments.
It is a possible solution for the original example, because the
toString() of an Integer and a Long are equal if, and only if, they
represent the same integer.
It is not a general solution, because there is no general guarantee that
actual.toString() gives a different result from expected.toString() in
all failure cases.
In particular, a method that returns Object, String, CharSequence,
Serializable, or Comparable could go wrong by returning the toString()
of the object it should have returned. A String-base unit test would not
distinguish an Object reference to the String "1" from an Object
reference to Long.valueOf(1).
Patricia