Difference between matrix guardian libraries and coreMatchers for hamcrest-core

The hamcrest class org.hamcrest.Matchers org.hamcrest.Matchers very similar to org.hamcrest.CoreMatchers (although it looks like the Matchers bigger). Why should I use CoreMatchers (except that it looks a little smaller), and why are the two classes so similar?

+48
junit hamcrest
Jun 07 '12 at 13:35
source share
2 answers

Hamcrest connectors are divided into several modules. The "core" includes the most basic classes and abstract classes needed to build other helpers. org.hamcrest.CoreMatchers includes factory methods for these mappings only. Other matches are in the โ€œlibraryโ€ module, grouped by the types of objects that they correspond to, and are optional. org.hamcrest.Matchers includes both sets of matches.

What should you use? I statically import all of the latter without a problem. It might take a little longer to compile, but this has never been a problem for me. I put this at the top of my unit tests in addition to importing JUnit:

 import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.Matchers.*; 

This gives better readability in test methods.

+37
Jun 08 '12 at 20:45
source share

If you use Mockito a lot (like me), you can do:

 import org.mockito.Mockito; 

or

 static import org.mockito.Mockito.*; 

and since the Mockito class extends the Mockito Matchers class, you may run into conflicts between the Matchers classes or their static methods. Having CoreMatchers allows me to use CoreMatchers, based on JUnit, in the same class as Mockito, without having to fully qualify them at my point of use.

+7
Mar 19 '13 at 22:23
source share



All Articles