Ready to Test: Maven + TestNG + Hamcrest + Mockito
I’m no Maven fanboy, but for a new, small Java project the ultra-fast setup time is compelling. So, for my latest little application, I decided to give it a go. Sadly, the default Maven archetype lumped my project with JUnit 3.8.1. Boo. And although the TestNG website mentions an alternative archetype, it appears to have disappeared off the face of the internet.
Luckily, dragging my project into the present wasn’t difficult. Along the way I also added my essential testing libraries: Hamcrest for matchers and Mockito for mocking (well, stubbing, but that’s another story). For posterity’s sake, and for others that share my testing tastes, here’s how it’s done.
I’m assuming that you have Maven 2 installed already. If not, it’s trivial to:
- Download the latest (2.1.0 at time of writing); and
- Install it according to the instructions provided.
You can check if you have Maven ready to go by running:
Apache Maven 2.1.0 (r755702; 2009-03-18 19:10:27+0000)
Java version: 1.6.0_12
Java home: /usr/local/java/jdk1.6.0_12/jre
Default locale: en_AU, platform encoding: UTF-8
OS name: “linux” version: “2.6.28-11-generic” arch: “amd64″ Family: “unix”
With the lack of an available alternative, I found it easiest to start with the default archetype. To create a new project, run something like:
Remember that if you’ve just installed Maven it will take this opportunity to download the internet. Be patient. If you’re new to Maven, you might also want to check out the 5 minute guide which walks through this in more detail.
Check that your bare application has been created:
$ find . -type f
$ mvn package
$ java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Add the Dependencies
Next you need to update the project POM to add the testing libraries as dependencies to your build. This involves three changes:
- Removing the default dependency on JUnit.
- Adding new dependencies for TestNG, Hamcrest and Mockito to the “test” scope.
- Configuring the compiler to accept Java 5 source.
The last step is necessary as the Maven default setting assumes Java 1.3 source, which apart from being ancient doesn’t support goodies such as annotations that are required for the new testing libraries. Your updated pom.xml file should look something like:
I’ve used the latest available versions of each of the libraries in this example — tweak them to suit your current reality.
Update the Sample Test
Now you’re ready to try updating the sample test case to use the trinity of TestNG, Hamcrest and Mockito. The easiest way to do this is to get Maven to generate a project for your IDE, e.g.
Fire up your chosen IDE, open the AppTest class, and edit it to exercise all of the dependencies:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
What are you waiting for? Try it out:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESSFUL
[INFO] Total time: 18 seconds
[INFO] Finished at: Wed Jun 24 16:11:48 BST 2009
[INFO] Final Memory: 20M/100M
If you got this far, then everything you need is in place. Now you just have to … implement your project!
If you poke about a bit, you will also find that the maven surefire plugin, which manages the tests, generates some reports by default. Along with HTML output, it also produces a JUnit-like XML report at:
This report is ideal for integration with a continuous integration server (in my case Pulse, naturally, but many will support it).
This entry was posted on Thursday, June 25th, 2009 at 10:12 am and is filed under Agile, Build, Continuous Integration, Java, Technology, Testing. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.