Unit testing with assertTrue vs others

java, junit, unit-testing

Solution

The main benefits of using other assertions is that they better communicate intent and are likely to give a more meaningful default message in the event of failure.

e.g.

if you write `assertEquals(2, x)` if `x` is actually 1 then the failure message will be:

java.lang.AssertionError: expected:<2> but was:<1>

which is more helpful than if you write `assertTrue(x == 2)` where all you would see is the `AssertionError` and the stack trace.

This is even more important when you are using TDD because you when you write a failing test first you want to be confident that the test is failing for the reason you are expecting it to and that there is not some accidental behaviour going on.

Problem

I work in a TDD environment and I use assertTrue a lot whereas there are many other methods, such as assert equals etc. I have a class that I have more than 40 test cases and they are all assertTrue. is this acceptable? I wanted to ask as a style, is this proper? any suggestions? if u think this question is inappropriate let me know i ll delete it. EDIT: ``` assertTrue(targetSpecifiers.size() == 2); assertTrue(targetSpecifiers.get(0).getPlacementId().compareTo(new BigInteger("1")) ==0); assertTrue(targetSpecifiers.get(1).getPlacementId().compareTo(new BigInteger("2")) ==0); ```

Original source