Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert.That vs Assert.True

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?

like image 378
Razer Avatar asked Apr 18 '13 15:04

Razer


People also ask

What is assert true?

Asserts that a condition is true. If the condition is false the method throws an AssertionException. Asserts that a condition is true.

What is assert that in JUnit?

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.

Is assertThat actual?

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.

What is the difference between assertThat and assertEquals?

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”.


1 Answers

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).

like image 176
Sergey Kalinichenko Avatar answered Sep 20 '22 03:09

Sergey Kalinichenko