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:

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:

For junit-jupiter engine:

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

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

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:

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

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:

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)

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

Tests can define their own listeners via nested listener elements.

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

testclasses can define their own listeners via nested listener elements.

Examples

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

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

Launches the JUnit 5 platform to run the org.myapp.SimpleTest test

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

Launches 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" methods="testFoo, testBar"/>
</junitlauncher>

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

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

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

Selects 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>
        <listener type="legacy-xml" sendSysOut="true" sendSysErr="true"/>
        <listener type="legacy-plain" sendSysOut="true" />
    </testclasses>
</junitlauncher>

Selects 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. 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.