Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the alternative to using the Deprecated Hamcrest method is()?

I use the following code at the moment to assert on a boolean value, however the method org.hamcrest.Matchers.is() is deprecated.

assertThat(someValue, is(false)); 

Is there a simple alternative syntax to test for boolean values without resorting to assertTrue() which gives you poor failure messages like "java.lang.AssertionError"


Edit after receiving comments/answers

My initial concerns were raised because Eclipse shows the following import statement as deprecated

enter image description here

On viewing the Hamcrest API docs there are 3 overloaded variations of the is() method, only one of which is deprecated.

Therefore, to clarify the comment from @mark and the answer from @matt, the use of is() that I have posted above is valid and not deprecated.

like image 643
Brad Avatar asked Sep 26 '12 16:09

Brad


People also ask

What can I use instead of assertThat?

assertThat method is deprecated. Its sole purpose is to forward the call to the MatcherAssert. assertThat defined in Hamcrest 1.3. Therefore, it is recommended to directly use the equivalent assertion defined in the third party Hamcrest library.

What is hamcrest used for?

Hamcrest is a framework that assists writing software tests in the Java programming language. It supports creating customized assertion matchers ('Hamcrest' is an anagram of 'matchers'), allowing match rules to be defined declaratively. These matchers have uses in unit testing frameworks such as JUnit and jMock.

How do I use hamcrest in eclipse?

Including HamcrestRight click on your eclipse project and select 'Properties' and then select 'Java Build Path' in the left pane and 'Libraries' on the right. On the 'Libraries' tab click the 'Add External Jars' button and navigate to the hamcrest-all jar you previously downloaded.


2 Answers

Have you tried equalTo(T)?

assertThat(someValue, equalTo(false)); 

I don't see that is(T) is deprecated - is(Class) is deprecated however.

like image 129
matt b Avatar answered Oct 01 '22 22:10

matt b


I had thought this was a transitive dependency issue, but it's really just a display issue in Eclipse where it marks the import as deprecated because one overloaded form is. The code should compile fine since the import will expose all forms.

The deprecated form has been removed from the source and will not exist in the next release (1.4).

Original Answer

The problem is that JUnit includes a set of Hamcrest classes in its JAR. You can use junit-dep.jar for now, but newer versions (4.9 and 4.10 so far) of JUnit omit them.

like image 38
David Harkness Avatar answered Oct 01 '22 23:10

David Harkness