Assert#assertThat
is a generic method. Primitive types don't work with generics. In this case, the byte
and int
are boxed to Byte
and Integer
, respectively.
It then becomes (within assertThat
)
Byte b = 0;
Integer i = 0;
b.equals(i);
Byte#equals(Object)
's implementation checks if the argument is of type Byte
, returning false
immediately if it isn't.
On the other hand, assertEquals
is Assert#assertEquals(long, long)
in which case both the byte
and int
arguments are promoted to long
values. Internally, this uses ==
on two primitive long
values which are equal.
Note that this boxing conversion works because assertThat
is declared as
public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
where the byte
is boxed to a Byte
for T
, and the int
is a boxed to an Integer
(within the call to equalTo
), but inferred as a Number
to match the Matcher<? super T>
.
This works with Java 8's improved generic inference. You'd need explicit type arguments to make it work in Java 7.
Byte.valueOf((byte) 0).equals(Integer.valueOf(0))
is false. – Berni