Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between hamcrest-library Matchers and hamcrest-core CoreMatchers

Tags:

junit

hamcrest

It looks like the hamcrest org.hamcrest.Matchers class is very similar to org.hamcrest.CoreMatchers (though it looks like Matchers has more). Why would I choose to use CoreMatchers (other than it looks like the class is slightly smaller), and why are these two classes so similar?

like image 519
Jeff Storey Avatar asked Jun 07 '12 13:06

Jeff Storey


People also ask

What does hamcrest core do?

Hamcrest is a widely used framework for unit testing in the Java world. Hamcrest target is to make your tests easier to write and read. For this, it provides additional matcher classes which can be used in test for example written with JUnit. You can also define custom matcher implementations.

What is org hamcrest matchers in?

Hamcrest is a popular framework that help us to create the matcher objects. It is used for writing software tests and also performs unit testing in Java programming language. Hamcrest is mainly used with other unit testing frameworks like JUnit, jMockit, Mockito, etc.

What is hamcrest library?

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.

What is Corematchers?

Creates a matcher that matches if the examined object matches ALL of the specified matchers. static.


2 Answers

The Hamcrest matchers are split into several modules. The "core" includes the most basic matchers and abstract classes required for building other matchers. org.hamcrest.CoreMatchers includes the factory methods for just these matchers. The other matchers are in the "library" module grouped by the types of objects they match and are optional. org.hamcrest.Matchers includes both sets of matchers.

Which should you use? I statically import everything from the latter without any trouble. Perhaps the compile times might take slightly longer, but that's never been an issue for me. I put this at the top of my unit tests in addition to the JUnit imports:

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

This gives the best readability in the test methods.

like image 84
David Harkness Avatar answered Oct 11 '22 12:10

David Harkness


If you use Mockito a lot (as I do), you might be doing:

import org.mockito.Mockito; 

or

static import org.mockito.Mockito.*; 

and since the Mockito class extends Mockito's Matchers class, then you can end up with conflicts between either the Matchers classes or their static methods. Having CoreMatchers allows me to use JUnit-derived CoreMatchers in the same class as Mockito, without having to full-qualify them at their point of usage.

like image 23
Kevin Welker Avatar answered Oct 11 '22 14:10

Kevin Welker