Why doesn’t Maven execute my unit tests?

If you have been using Maven for some time, you might have noticed that Maven will automatically find and execute your unit tests when they are present in your project, even without any additional configuration.

This is because it is using the Surefire Plugin with a default configuration (that can be overridden of course). However, for this to execute your tests, you need to place your test classes in the proper place and you need to name them appropriately.

Location of test classes

Your test classes need to be placed under the src/test/java folder, otherwise it won’t be picked up.

Naming of test classes

By default, the Surefire Plugin will automatically include all test classes that where the name matches one of the following patterns:

  • Test*.java
  • *Test.java
  • *TestCase.java

Example

We have the following test classes:

And the result is:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.jtuts.MyTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.039 sec
Running com.jtuts.MyTestCase
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec
Running com.jtuts.TestMyCode
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec
Running com.jtuts.TestsForMyCode
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec

Results :

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

You can see that 3 of the above classes are skipped, because they do not match the naming convention.