1

You can test objects assertEquals(a,b) and assertTrue(a.equals(b)) or assertTrue(a==b) (for primitives). In this case of course assertEquals(a,b) is the only possible variant. It is null safe and more informative in case of test fault (you get exact fault not true or false).

I have a next question.

What about situation when we have only Boolean value to test?

assertEquals(true, a) and assertTrue(a)

What the difference can be?

I see the case when we check Boolean. Using assertTrue(a) is not null safe. And this is very important reason. In case of boolean don't see the difference.

Mike Menko
  • 819
  • 8
  • 9
  • `assertEquals` at a failure would also show the expected and wrong value. That actually is the reason it exists. Compare it with java `assert ...;` – Joop Eggen Feb 13 '20 at 13:25

1 Answers1

3

assertEquals(a, b) and assertTrue(a.equals(b)) are not the same, since the former will return true if both are null, while the latter will throw a null pointer exception.

The idiomatic way of writing assertTrue(a == b) is assertSame(a, b), if a and b are references. For primitives, use assertEquals.

assertEquals(true, a) and assertTrue(a) are semantically identical. It's just a matter of style. The former would be considered by many to be bad style, in the same as this would be.

if (isActive() == true) {
    doThing()
}
Michael
  • 41,989
  • 11
  • 82
  • 128