Testing

We have already seen the surefire plugin in regards to testing but now lets go a little deeper, there are two plugins that you need to know about, both support POJO testing, JUnit versions 3 to 5 and TestNG.

POJO testing

Plain Old Java Objects (POJO) can be tested right out of the box (no need for any changes) using the surefire plugin. You notice that the test class file names ends in Test, you can use any of the following class names Test*.java, *Test.java, *Tests.java and *TestCase.java


Junit 4 and 5 Unit Tests

Maven supports Junit 4 and 5 tests, you need to add in the dependency for Junit and state the version

For Junit 5 you need to add the below dependency and specify the version of the surefire plugin as this version supports Junit 5, the code for the test class remains the same, the below will allow you to run both Junit4 and 5 tests. The engine is the one that does all the work, the vintage enables the Junit 5 to run Junit 4 tests.

TestNg Testing

TestNG is a test engine framework, its seems that TestNg is still popular, the test code is the same as Junit 5


Generating Test Reports

You can add the surefire report plugin which will create a number of reports in the target directory (right screenshot)

Maven Failsafe Plugin

The failsafe plugin is similar to the surefire plugin but it allows you to run integration tests, these are more heavy-weight tests. When you run the verify phase both the surefire and the failsafe tests will run.

Jacoco (Test Coverage)

Jacoco is a test coverage plugin which will tell you how much of your code is being tested, there are lots of options, here I am going to explain the setting up, however best to also check online as this may change when the versions change.


When you run verify the jacoco directories (jacoco-it and jacoco-ut) will be created in the target/site directory, looking at the index.html you can see that I don't have much testing coverage.