Java Unit Tests Maven dependencies snippet
Pretty much every Java project I have uses these dependencies: JUnit + Hamcrest + Mockito.
So, from time to time, this pom.xml snippet containing them proves needed.
The dependencies above avoid having multiple hamcrest versions on the classpath (as JUnit or Mockito may require a different Hamcrest version at a given point in time).
Latest versions of each lib can be checked at the The Maven Central Repository Search Engine:
- JUnit:
junit; - Hamcrest:
hamcrest-junit; - Mockito:
mockito-core.
Some possible questions:
- “Why
hamcrest-junitand nothamcrest-all?”- Hamcrest folks have recently changed the way they handle releases. Their goal was to decouple Hamcrest from JUnit. The impact in our life is that we now use
hamcrest-junitinstead ofhamcrest-all. See Hamcrest-JUnit’s repo for more details.
- Hamcrest folks have recently changed the way they handle releases. Their goal was to decouple Hamcrest from JUnit. The impact in our life is that we now use
- “Why
mockito-coreand notmockito-all?”- Basically
mockito-allis sort of a fat jar, containing all its dependencies (such as Hamcrest), embedded. As we want to fine tune its dependencies (that’s what the<exclude>tag is!), then-coreis the way to go.
- Basically
Should any other questions arise, I’ll address them here. I’ll also try to keep the snippet’s versions up to date.
Cya!

Leave a Comment