I wonder why this fails:
assertEquals(Key.class, expectedKey.getClass());
and this does not:
assertTrue(expectedKey instanceof Key);
Is there a real difference between the two?
assertEquals() Asserts that two objects are equal. assertSame() Asserts that two objects refer to the same object. the assertEquals should pass and assertSame should fail, as the value of both classes are equal but they have different reference location.
assertTrue is used to verify if a given Boolean condition is true. This assertion returns true if the specified condition passes, if not, then an assertion error is thrown.
assertEquals. Asserts that two object arrays are equal. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null , they are considered equal.
In assertTrue, you are asserting that the expression is true. If it is not, then it will display the message and the assertion will fail. In assertFalse, you are asserting that an expression evaluates to false. If it is not, then the message is displayed and the assertion fails.
Because expectedKey.getClass()
gives the Class
object of runtime type of the expectedKey
, which may be different from Key
class.
However, with instanceof
, even if expectedKey
runtime type is some subclass of Key
class, the result will be true
, because an instance of subclass, is also an instanceof
the super class.
Because expectedKey
is an instance of a subclass of Key, most probably. The error message you get from the failed assertion should tell you. Read it.
"s"
, for example is an instance of java.lang.Object, but its class is not java.lang.Object, it's java.lang.String.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With