There are multiple factors and parameters Maven will use to decide which classes are Test classes. In fact it is not Maven in itself that does it, but one or more plugins.
Most of the time you will use the Surefire Plugin which binds by default to the test
phase (you can also use the Failsafe Plugin which behave quite similarly but is aimed for IT tests)
The Surefire Plugin configuration will define which classes are your test classes:
testSourceDirectory
: folder containing test classes, src/test/java
by default
includes
: defines name patterns of classes to include, there are multiple default patterns such as **/*Test.java
- ... And others such as
includesFile
, test
, see the doc for details
All these classes will be used for your tests.
The Runner is basically the class that will run your tests. From the JUnit docs:
A Runner runs tests and notifies a RunNotifier of significant events
as it does so. You will need to subclass Runner when using RunWith to
invoke a custom runner. When creating a custom runner, in addition to
implementing the abstract methods here you must also provide a
constructor that takes as an argument the Class containing the tests.
A similar behavior is observed with TestNG. Basically a Runner will manage how your test classes are instanciated and executed. The behavior highly depends on implementation, I recommend reading source codes for said Runners and JUnit / TestNN docs about them to learn more.