Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit4: is() vs is(notNullValue())

Regarding JUnit4 assertThat, I have seen this done both ways. Is one correct over the other or is it all the same?

byte[] val;
...
assertThat(val, notNullValue());
        --vs--
assertThat(val, is(notNullValue()));

The second option reads "assert that val is not null" which sounds better. (On the other hand, it may be redundant.)

I have used both ways and they seem to produce correct results.

like image 758
Daniel Avatar asked Dec 27 '22 07:12

Daniel


1 Answers

The Hamcrest documentation says:

Hamcrest strives to make your tests as readable as possible. For example, the is matcher is a wrapper that doesn't add any extra behavior to the underlying matcher. The following assertions are all equivalent:

assertThat(theBiscuit, equalTo(myBiscuit));
assertThat(theBiscuit, is(equalTo(myBiscuit)));
assertThat(theBiscuit, is(myBiscuit));

The last form is allowed since is(T value) is overloaded to return is(equalTo(value)).

like image 166
Jesper Avatar answered Jan 10 '23 04:01

Jesper