-3
  1. I have worked with junits but never gave a thought on how maven comes to know that the class defined src/test/java is a test class and not a normal class.

One possibility is it may scan for @Test in the class. Is this how it is achived? If not how?.

  1. What exactly are Runners and what is their role. Can I have test cases without Runners?
Tushar Banne
  • 1,587
  • 4
  • 20
  • 38
  • That's the default `includes` value: http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#includes - see also the testSourceDirectory parameter. – assylias Nov 14 '17 at 10:39
  • 1
    you should go and read about unit-testing, junit and maven. Question is too broad and doesnt show any efforts from writer's side. – Prateek Jain Nov 14 '17 at 10:40
  • 1) http://maven.apache.org/surefire/maven-surefire-plugin/ 2) http://junit.org/junit4/ or http://junit.org/junit5/ – Seelenvirtuose Nov 14 '17 at 10:41

2 Answers2

1

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.

Pierre B.
  • 11,612
  • 1
  • 37
  • 58
0

The @Test is an annotation and allows frameworks like junit and maven to identify which tests should be run.

They also allow other things to be specified (for example, an expected exception).

http://junit.org/junit4/javadoc/4.12/org/junit/Test.html

Test runners are more complicated and allow you to have more control over how the tests are executed, for example if you are using frameworks. But for "plain vanilla" testing you shouldnt need them.

https://www.mscharhag.com/java/understanding-junits-runner-architecture

vikingsteve
  • 38,481
  • 23
  • 112
  • 156
  • Already seen https://www.mscharhag.com/java/understanding-junits-runner-architecture this blog, but it does not specify what it the purpose of using it. – Tushar Banne Nov 14 '17 at 10:54
  • You really dont need to worry about test runners unless you're using a framework that requires special setup / execution. – vikingsteve Nov 14 '17 at 12:21