HOW-TO: Test dependencies in a Maven project (JUnit, Mockito, 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:
1 2 3 4 5 6 | < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.11</ version > < scope >test</ scope > </ dependency > |
The dependency:tree
produces:
1 2 | [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:
1 2 3 4 5 6 | < dependency > < groupId >org.mockito</ groupId > < artifactId >mockito-core</ artifactId > < version >1.9.5</ version > < scope >test</ scope > </ dependency > |
Now the dependency:tree
produces:
1 2 3 4 | [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:
01 02 03 04 05 06 07 08 09 10 11 12 13 | < dependency > < groupId >org.hamcrest</ groupId > < artifactId >hamcrest-core</ artifactId > < version >1.3</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >org.objenesis</ groupId > < artifactId >objenesis</ artifactId > < version >1.3</ version > < scope >test</ scope > </ dependency > |
Having this we can easily add Hamcrest library, that provides a library of matcher objects, dependency:
01 02 03 04 05 06 07 08 09 10 11 12 13 | < dependency > < groupId >org.hamcrest</ groupId > < artifactId >hamcrest-core</ artifactId > < version >1.3</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >org.objenesis</ groupId > < artifactId >objenesis</ artifactId > < version >1.3</ version > < scope >test</ scope > </ dependency > |
And the dependency:tree
produces:
1 2 3 4 5 | [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:
1 2 3 4 5 6 | < dependency > < groupId >org.assertj</ groupId > < artifactId >assertj-core</ artifactId > < version >1.5.0</ version > < scope >test</ scope > </ dependency > |
Which result in the following tree:
1 2 3 4 5 6 | [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:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <!-- Test --> < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.11</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >org.mockito</ groupId > < artifactId >mockito-core</ artifactId > < version >1.9.5</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >org.assertj</ groupId > < artifactId >assertj-core</ artifactId > < version >1.5.0</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >org.hamcrest</ groupId > < artifactId >hamcrest-core</ artifactId > < version >1.3</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >org.hamcrest</ groupId > < artifactId >hamcrest-library</ artifactId > < version >1.3</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >org.objenesis</ groupId > < artifactId >objenesis</ artifactId > < version >1.3</ version > < scope >test</ scope > </ dependency > |
- 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).