samshers
samshers

Reputation: 3680

JUnit / Hamcrest - org.hamcrest.CoreMatchers.is() is deprecated. What should I use instead?

The method org.hamcrest.CoreMatchers.is() deprecated.
The doc says to use - org.hamcrest.CoreMatchers.isA() instead.

But isA() seems to serve a different case all together.

Ok. What ever, coming to my problem. Earlier I was using is() as below

// might be i should not be using it like this, but it works.
assertThat(actualRes, is(true));

Now i can not use that same with isA(). It throws compilation error not applicable for arguments(boolean)

I understand what isA() does. What I want to know is, given is() is deprecated, what should I be using as replacement for assertThat(actualRes, is(true))?

Upvotes: 4

Views: 6111

Answers (1)

glytching
glytching

Reputation: 47905

The deprecated form of CoreMatchers.is() is this one:

is(java.lang.Class type)

Deprecated. use isA(Class type) instead.

So, for this isA is the correct alternative but the form of CoreMatchers.is() which you are using in this assertion: assertThat(actualRes, is(true)); is this one ...

is(T value)

A shortcut to the frequently used is(equalTo(x)).

... which is not deprecated.

Here's some code which might clarify matters:

boolean actualRes = true;

// this passes because the *value of* actualRes is true
assertThat(actualRes, CoreMatchers.is(true));

// this matcher is deprecated but the assertion still passes because the *type of* actualRes is a Boolean
assertThat(actualRes, CoreMatchers.is(Boolean.class));

// this passes because the *type of* actualRes is a Boolean
assertThat(actualRes, CoreMatchers.isA(Boolean.class));

Upvotes: 6

Related Questions