## JUnitLauncher

### Description

This task allows tests to be launched and run using the JUnit 5 framework.

JUnit 5 introduced a newer set of APIs to write and launch tests. It also introduced the concept of test engines. Test engines decide which classes are considered as testcases and how they are executed. JUnit 5 supports running tests that have been written using JUnit 4 constructs as well as tests that have been written using JUnit 5 constructs. For more details about JUnit 5 itself, please refer to the JUnit 5 project's documentation at https://junit.org/junit5/.

The goal of this `junitlauncher` task is to allow launching the JUnit 5 test launcher and building the test requests so that the selected tests can then be parsed and executed by the test engine(s) supported by JUnit 5. This task in itself does not understand what a test case is nor does it execute the tests itself.

Note: This task depends on external libraries not included in the Apache Ant distribution. See Library Dependencies for more information.

Note: You must have the necessary JUnit 5 libraries in the classpath of the tests. At the time of writing this documentation, the list of JUnit 5 platform libraries that are necessary to run the tests are:

- `junit-platform-commons.jar`
- `junit-platform-engine.jar`
- `junit-platform-launcher.jar`
- `opentest4j.jar`

Depending on the test engine(s) that you want to use in your tests, you will further need the following libraries in the classpath

For junit-vintage engine:

- `junit-vintage-engine.jar`
- `junit.jar` (JUnit 4.x version)

For junit-jupiter engine:

- `junit-jupiter-api.jar`
- `junit-jupiter-engine.jar`

To have these in the test classpath, you can follow either of the following approaches:

- Recommended approach since Ant 1.10.6: Place the `ant-junitlauncher.jar` in `ANT_HOME/lib` directory and use the nested `<classpath>` element to specify the location of the the rest of the JUnit specific jars (noted above). Please read the using classpath element section for more details.
- OR Put all these relevant jars along with the `ant-junitlauncher.jar` in `ANT_HOME/lib` directory
- OR Put `ant-junitlauncher.jar` in the `ANT_HOME/lib` directory and include all other relevant jars in the classpath by passing them as a `-lib` option, while invoking Ant

Tests are defined by nested elements like `test`, `testclasses` tags (see nested elements).

### Parameters

Attribute | Description | Required
--- | --- | ---
haltOnFailure | A value of true implies that build has to stop if any failure occurs in any of the tests. JUnit 4+ classifies failures as both assertion failures as well as exceptions that get thrown during test execution. As such, this task too considers both these cases as failures and doesn't distinguish one from another. | No; default is false
failureProperty | The name of a property to set in the event of a failure (exceptions in tests are considered failures as well). | No
printSummary | If the value is set to `true` then this task, upon completion of the test execution, prints the summary of the execution to `System.out`. The summary itself is generated by the JUnit 5 platform and not by this task. | No; defaults to `false`

### Nested Elements

#### classpath

The nested `<classpath>` element that represents a PATH like structure can be used to configure the task to use this classpath for finding and running the tests. This classpath will be used for:

- Finding the test classes to execute
- Finding test engines that run the tests
- If configured to do so, finding all necessary JUnit libraries

If the `classpath` element isn't configured for the task, then the classpath of Ant itself will be used for finding the test classes and the JUnit libraries.

Below is an example of setting up the classpath to include the Jupiter test engine and the JUnit platform libraries during the execution of the tests.

    <project>

        <property name="output.dir" value="${basedir}/build"/>
        <property name="src.test.dir" value="${basedir}/src/test"/>
        <property name="build.classes.dir" value="${output.dir}/classes"/>

        <target name="init">
            <mkdir dir="${output.dir}"/>
        </target>

        <path id="junit.platform.libs.classpath">
            <fileset dir="${basedir}/src/lib/junit-platform/"/>
        </path>

        <path id="junit.engine.jupiter.classpath">
            <fileset dir="${basedir}/src/lib/jupiter/"/>
        </path>

        <target name="compile-test" depends="init">
            <mkdir dir="${build.classes.dir}"/>
            <javac srcdir="${src.test.dir}"
               destdir="${build.classes.dir}">
               <!-- our tests only need JUnit Jupiter engine
               libraries in our compile classpath for the tests -->
               <classpath refid="junit.engine.jupiter.classpath"/>
            </javac>
        </target>

        <target name="test" depends="compile-test">
            <junitlauncher>
                <!-- include the JUnit platform related libraries
                required to run the tests -->
                <classpath refid="junit.platform.libs.classpath"/>

                <!-- include the JUnit Jupiter engine libraries -->
                <classpath refid="junit.engine.jupiter.classpath"/>

                <classpath>
                    <!-- the test classes themselves -->
                    <pathelement location="${build.classes.dir}"/>
                </classpath>
                <testclasses outputdir="${output.dir}">
                    <fileset dir="${build.classes.dir}"/>
                    <listener type="legacy-brief" sendSysOut="true"/>
                    <listener type="legacy-xml" sendSysErr="true" sendSysOut="true"/>

                </testclasses>
            </junitlauncher>
        </target>
    </project>

In the example above,

- The `src/lib/jupiter` directory is expected to contain the Jupiter test engine related jars (which have been listed in an earlier section of this document).
- The `src/lib/junit-platform` directory is expected to contain the JUnit platform jars (which have been listed in an earlier section of this document)

In the `test` target we use the `classpath` nested element to point to the `junit.engine.jupiter.classpath` and `junit.platform.libs.classpath` containing those jars. In this `test` target we also use another `classpath` element to point to the location containing our test classes. If required, all these classpaths can be combined into one.

#### listener

The `junitlauncher` task can be configured with `listener`(s) to listen to test execution events (such as a test execution starting, completing etc...). The listener is expected to be a class which implements the `org.junit.platform.launcher.TestExecutionListener`. This `TestExecutionListener` interface is an API exposed by the JUnit 5 platform APIs and isn't specific to Ant. As such, you can use any existing implementation of `TestExecutionListener` in this task.

##### Test result formatter

`junitlauncher` provides a way where the test execution results can be formatted and presented in a way that's customizable. The task allows for configuring test result formatters, through the use of `listener` element. As noted previously, the `listener` element expects the listener to implement the `org.junit.platform.launcher.TestExecutionListener` interface. Typically, result formatters need a bit more configuration details to be fed to them, during the test execution—details like where to write out the formatted result. Any such listener can optionally implement the `org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter` interface. This interface is specific to Ant `junitlauncher` task and it extends the `org.junit.platform.launcher.TestExecutionListener` interface

The `junitlauncher` task comes with the following pre-defined test result formatter types:

- legacy-plain : This formatter prints a short statistics line for all test cases.
- legacy-brief : This formatter prints information for tests that failed or were skipped.
- legacy-xml : This formatter prints statistics for the tests in XML format.

Note: Each of these formatters named legacy try to format the results similar to what the `junit` task's formatters used to do. Furthermore, the legacy-xml formatter generates the XML to comply with the same schema that the `junit` task's XML formatter used to follow. As a result, the XML generated by this formatter, can be used as-is by the `junitreport` task.

The `listener` element supports the following attributes:

Attribute | Description | Required
--- | --- | ---
type | Use a predefined formatter (either legacy-xml, legacy-plain or legacy-brief). | Exactly one of these
classname | Name of a listener class which implements `org.junit.platform.launcher.TestExecutionListener` or the `org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter` interface
resultFile | The file name to which the formatted result needs to be written to. This attribute is only relevant when the listener class implements the `org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter` interface. If no value is specified for this attribute and the listener implements the `org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter` then the file name will be defaulted to and will be of the form `TEST-testname.formatter-specific-extension` (ex: `TEST-org.myapp.SomeTest.xml` for the legacy-xml type formatter) This file is considered relative to the `outputDir` configured on the listener. If no `outputDir` is set on the listener, then the file is considered relative to the `outputDir` of the test in context of which this listener is being run. | No
outputDir | Directory into which to create the output of the listener. Since Ant 1.10.6 | No
sendSysOut | If set to true then the listener will be passed the `stdout` content generated by the test(s). This attribute is relevant only if the listener class implements the `org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter` interface. | No; defaults to false
sendSysErr | If set to true then the listener will be passed the `stderr` content generated by the test(s). This attribute is relevant only if the listener class implements the `org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter` interface. | No; defaults to false
if | Only use this listener if the named property is set. | No
unless | Only use this listener if the named property is not set. | No

#### test

Defines a single test class.

Attribute | Description | Required
--- | --- | ---
name | Fully qualified name of the test class. | Yes
methods | Comma-separated list of names of test case methods to execute. If this is specified, then only these test methods from the test class will be executed. | No
haltOnFailure | Stop the build process if a failure occurs during the test run (exceptions are considered as failures too). Overrides value set on `junitlauncher` element. | No
failureProperty | The name of a property to set in the event of a failure (exceptions are considered failures as well). Overrides value set on `junitlauncher` element. | No
outputDir | Directory to write the reports to. | No; default is the base directory of the project.
if | Only run this test if the named property is set. | No
unless | Only run this test if the named property is not set. | No
includeEngines | A comma separated set of test engine ids. If specified, only these test engine(s) will be used for running the tests. For example: `includeEngines="junit-jupiter"` will only use the Jupiter test engine for execution of the tests and will ignore any other engines that might have been found in the classpath. | No
excludeEngines | A comma separated set of test engine ids. If specified, these test engine(s) will be excluded when running the tests. For example: `excludeEngines="junit-vintage"` will exclude the vintage test engine during execution of the tests and will use any other engines that might have been found in the classpath. | No

Tests can define their own listeners via nested `listener` elements.

The fork nested element can be used to run the test in a newly forked JVM.

#### testclasses

Define a number of tests based on pattern matching.

`testclasses` collects the included resources from any number of nested Resource Collections. It then selects each resource whose name ends in `.class`. These classes are then passed on to the JUnit 5 platform for it to decide and run them as tests.

Attribute | Description | Required
--- | --- | ---
haltOnFailure | Stop the build process if a failure occurs during the test run (exceptions are considered as failures too). Overrides value set on `junitlauncher` element. | No
failureProperty | The name of a property to set in the event of a failure (exceptions are considered failures as well). Overrides value set on `junitlauncher` element. | No
outputDir | Directory to write the reports to. | No; default is the base directory of the project.
if | Only run the tests if the named property is set. | No
unless | Only run the tests if the named property is not set. | No
includeEngines | A comma separated set of test engine ids. If specified, only these test engine(s) will be used for running the tests. For example: `includeEngines="junit-jupiter"` will only use the Jupiter test engine for execution of the tests and will ignore any other engines that might have been found in the classpath. | No
excludeEngines | A comma separated set of test engine ids. If specified, these test engine(s) will be excluded when running the tests. For example: `excludeEngines="junit-vintage"` will exclude the vintage test engine during execution of the tests and will use any other engines that might have been found in the classpath. | No

`testclasses` can define their own listeners via nested `listener` elements.

The fork nested element can be used to run the tests in a newly forked JVM. All tests that are part of this `testclasses` element will run in one single instance of the newly forked JVM.

#### fork

Since Ant 1.10.6

Tests launched using the `junitlauncher` task, by default, run in the same JVM that initiates the task. This behaviour can be changed using the `fork` element. The `fork` element and its attributes define the characteristics of the new JVM instance that will be created to launch the tests.

Attribute | Description | Required
--- | --- | ---
dir | The user working directory that will be used for the forked JVM | No
timeout | A value in milliseconds, specifying a maximum duration, the test running in this forked JVM is allowed to run. If the test runs longer than this configured value, then the JVM is killed | No
includeJUnitPlatformLibraries | If set to `true`, then the jar files that make up the JUnit platform, will be included in the runtime classpath of the forked JVM. If set to `false`, then the configured classpath of this task, which will be made available to the runtime classpath of the forked JVM, is expected to contain the JUnit platform library jars | No. Value defaults to `true`.
includeAntRuntimeLibraries | If set to `true`, then the jar files that make up the Ant runtime, will be included in the runtime classpath of the forked JVM. If set to `false`, then the configured classpath of this task, which will be made available to the runtime classpath of the forked JVM, is expected to contain the Ant runtime jars | No. Value defaults to `true`.

The `fork` element allows the following nested elements:

##### jvmarg

Additional JVM arguments may be passed to the forked JVM via the `jvmarg` elements. For example:

        <fork ...>
          <jvmarg value="-Djava.compiler=NONE"/>
          ...
        </fork>

`jvmarg` allows all attributes described in Command-line Arguments

##### sysproperty

The `sysproperty` elements allow passing Java system properties to the forked JVM:

        <fork>
            <sysproperty key="greeting" value="hello world"/>
        ...
        </fork>

The attributes for this element are the same as for environment variables

##### syspropertyset

You can specify a set of properties to be used as system properties with syspropertyset(s)

##### env

It is possible to specify environment variables to pass to the forked JVM via nested `env` elements. For a description of the `env` element's attributes, see the description in the exec task.

##### modulepath

The location of Java modules can be specified using the `modulepath` element, which is a path-like structure.

For example:

    <fork>
      <modulepath>
        <pathelement location="lib"/>
        <pathelement location="dist/test.jar"/>
      </modulepath>
      ...
    </fork>

##### upgrademodulepath

The location of Java modules, that replace upgradeable modules in the runtime, can be specified using the `upgrademodulepath` element, which is a path-like structure.

### Examples

Launch the JUnit 5 platform to run the `org.myapp.SimpleTest` test

    <path id="test.classpath">
        ...
    </path>

    <junitlauncher>
        <classpath refid="test.classpath"/>
        <test name="org.myapp.SimpleTest"/>
    </junitlauncher>

Launch the JUnit 5 platform to run the `org.myapp.SimpleTest` and the `org.myapp.AnotherTest` tests. The build process will be stopped if any test, in the `org.myapp.SimpleTest`, fails.

    <junitlauncher>
        <classpath refid="test.classpath"/>
        <test name="org.myapp.SimpleTest" haltOnFailure="true"/>
        <test name="org.myapp.AnotherTest"/>
    </junitlauncher>

Launch the JUnit 5 platform to run only the `testFoo` and `testBar` methods of the `org.myapp.SimpleTest` test class.

    <junitlauncher>
        <classpath refid="test.classpath"/>
        <test name="org.myapp.SimpleTest" methods="testFoo, testBar"/>
    </junitlauncher>

Select any `.class` files that match the `org/example/**/tests/**/` `fileset` filter, under the `${build.classes.dir}` and passes those classes to the JUnit 5 platform for execution as tests.

    <junitlauncher>
        <classpath refid="test.classpath"/>

        <testclasses outputdir="${output.dir}">
            <fileset dir="${build.classes.dir}">
                <include name="org/example/**/tests/**/"/>
            </fileset>
        </testclasses>
    </junitlauncher>

Select any `.class` files that match the `org/example/**/tests/**/` `fileset` filter, under the `${build.classes.dir}` and pass those classes to the JUnit 5 platform for execution as tests. Test results will be written out to the `${output.dir}` by the legacy-xml and legacy-plain formatters, in separate files. Furthermore, both the legacy-xml and the legacy-plain listeners, above, are configured to receive the standard output content generated by the tests. The legacy-xml listener is configured to receive standard error content as well.

    <junitlauncher>
        <classpath refid="test.classpath"/>

        <testclasses outputdir="${output.dir}">
            <fileset dir="${build.classes.dir}">
                <include name="org/example/**/tests/**/"/>
            </fileset>
            <listener type="legacy-xml" sendSysOut="true" sendSysErr="true"/>
            <listener type="legacy-plain" sendSysOut="true" />
        </testclasses>
    </junitlauncher>