What to prefer:
Assert.That(obj.Foo, Is.EqualTo(true))
or
Assert.True(obj.Foo)
For me, both asserts are equivalent, so which one should be prefered?
Asserts that a condition is true. If the condition is false the method throws an AssertionException. Asserts that a condition is true.
The assertThat assertion is the only one in JUnit 4 that has a reverse order of the parameters compared to the other assertions. In this case, the assertion has an optional failure message, the actual value, and a Matcher object.
AssertThat has a different semantic and the javadoc says it explicitly : Asserts that actual satisfies the condition specified by matcher. If not, an AssertionError is thrown with information about the matcher and failing value.
assertEquals() is the method of Assert class in JUnit, assertThat() belongs to Matchers class of Hamcrest. Both methods assert the same thing; however, hamcrest matcher is more human-readable. As you see, it is like an English sentence “Assert that actual is equal to the expected value”.
In this particular case, there is no difference: you will see the output of roughly the same level of detail (i.e. it tells you that something that was expected to evaluate to true
has evaluated to false
). Same goes for
Assert.IsTrue(obj.Foo);
and
Assert.That(obj.Foo, Is.True);
Your team should pick one style of assertions, and stick with it throughout all your tests. If your team prefers the Assert.That
style, then you should use Assert.That(obj.Foo, Is.True)
.
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