It's deprecated because of the lack of expressivity of the Java type system.
Whereas all of the other assertEquals
methods will compare the parameters using ==
(for primitives) or equals
(for reference types), you don't want to use either to compare arrays: all arrays are subtypes of Object
(i.e. they are reference types), and don't override equals
, so using assertEquals
to compare arrays will check whether the two arrays are identical.
Instead, you should invoke assertArrayEquals
, which compares whether the arrays have the same length, and if so, whether the corresponding array elements are equal.
Ideally, you would be able to specify parameter types like this:
assertEquals(T, T)
where T
is "any subtype of Object
, except for arrays". But you simply can't do that in Java; even if there were a way to express such a constraint, you couldn't prevent the method being called with array, because you can always upcast them to Object
s.
The only thing that you can do is to:
- Provide an overload which accepts
Object
s
- Provide overloads which accept more specific types, and mark these overloads
@Deprecated
. To cover all array types, you need 9 overloads (8 for each of the primitive array types; 1 for Object[]
, which covers all other reference types).
This doesn't prevent you from invoking assertEquals(T[], T[])
, but it does highlight that there is a problem there, via compiler warnings; yellow squiggles in Eclipse; etc.
Of course, this won't help in the case that you've upcast the arrays to Object
; but you won't, in most cases, unless you really intend to invoke that specific method.