HOW-TO: Test dependencies in a Maven project (JUnit, Mocito, Hamcrest, AssertJ)

JUnit itself is not enough for most of today's Java projects. You also need a mocking library, maybe something else. In this mini HOW-TO I present the test dependencies you can start with in a new Java project.

All starts with JUnit

There are two artifacts in junit group in Maven Repository: junit and junit-dep. Prior to version 4.9 the latter did not contain dependency to Hamcrest inlined. Today, we use junit dependency as follows: The dependency:tree produces:

[INFO] \- junit:junit:jar:4.11:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test

Mockito

The next dependency we usually need is a mocking framework. No doubts that Mockito is one of the most popular one. It comes in two favors: mockito-all and mockito-core. The first is a single jar will all dependencies inlined inside, whereas the latter is just a Mockito. It is recommended to use mockito-core with version 4.11 of JUnit. So let's add the dependency: Now the dependency:tree produces:

[INFO] +- junit:junit:jar:4.11:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] \- org.mockito:mockito-core:jar:1.9.5:test
[INFO]    \- org.objenesis:objenesis:jar:1.0:test

Hamcrest

Knowing that mockito-core is better for declarative dependency management, we will override dependencies to both Hamcrest and Objenesis as follows: Having this we can easily add Hamcrest library, that provides a library of matcher objects, dependency: And the dependency:tree produces:

[INFO] +- junit:junit:jar:4.11:test
[INFO] +- org.mockito:mockito-core:jar:1.9.5:test
[INFO] +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] \- org.objenesis:objenesis:jar:1.3:test

AssertJ

AssertJ - fluent assertions for java - provides a rich and intuitive set of strongly-typed assertions to use for unit testing. AssertJ is a fork of FEST Assert that I wrote about some time ago in this post. And what about the dependency? Let's bave a look: Which result in the following tree:

[INFO] +- junit:junit:jar:4.11:test
[INFO] +- org.mockito:mockito-core:jar:1.9.5:test
[INFO] +- org.assertj:assertj-core:jar:1.5.0:test
[INFO] +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] \- org.objenesis:objenesis:jar:1.3:test

The Final Cut

The complete Maven structure looks as follows: You can find this in unit-testing-demo project on GitHub (link to pom.xml) or you can try out my spring-mvc-quickstart-archetype (link to pom.xml).

Popular posts from this blog

Parameterized tests in JavaScript with Jest