Apache Ant

Recording test failures under Apache Ant *

See Recording test failures for an overview of LR4J test integration.

The following instructions apply to Apache Ant.

1. Ant: record all tests and generate one recording file per failing test class

This is the least intrusive method of integration since it doesn’t require build changes. Add the following rule to each of your test classes:

@Rule
public final TestRule watchman =
    new TestWatcher() {
        protected void failed(Throwable thr, Description description) {
            // Tell LiveRecorder to name the recording file after this
            // failing method & class
            System.setProperty("io.undo.output", description.toString());
            // Tell LiveRecorder that the test failed
            System.setProperty("io.undo.failed", "true");
        }
    };

Add the following to the junit target of your build.xml file:

<junit printsummary="yes" fork="yes" forkmode="perTest" haltonerror="no" haltonfailure="no" showoutput="true">

    <!-- set these JVM arguments to enable recording failed tests -->
    <jvmarg value="-XX:-Inline"/>
    <jvmarg value="-XX:TieredStopAtLevel=1"/>
    <jvmarg value="-XX:UseAVX=2"/>
    <jvmarg value="-agentpath:/path/to/lr4j-record-1.0.so=save_on=failure"/>

    <!-- add other nested elements as required... -->

</junit>

And ensure that Ant’s parallel task is not being used to wrap <junit/>.

The haltOnError/haltOnFailure attributes when set to yes cause the test run to stop at the first method in a class that errors/fails so that each recording file contains exactly one test error/failure, and that error/failure is located at the end of the recording (though the recording file may contain some earlier test successes too). When haltOnError/haltOnFailure are set to no, the recording may contain multiple test successes and failures and will be named after the last failing method.

2. Ant: record all tests and generate one recording file per failing test method

Add the following rule to each of your test classes:

@Rule
public final TestRule wrapper =
        new TestRule() {
            @Override
            public Statement apply(final Statement base, final Description description) {
                return new Statement() {
                    @Override
                    public void evaluate() throws Throwable {
                        UndoLR.setEventLogSize(1000 * 1000 * 1000);
                        UndoLR.start();
                        try {
                            base.evaluate();
                        } catch (Throwable e) {
                            UndoLR.save(description + ".undo");
                        } finally {
                            UndoLR.stop();
                        }
                    }
                };
            }
        };

Create a libs folder under your project root and add the follows jars to it:

  • LiveRecorder API

  • JUnit API (unless already part of Ant’s own libraries)

  • Any other libraries required for the tests to run.

mkdir {project_root}/libs
cp /path/to/lr4j/lr4j_api-1.1.jar {project_root}/libs
cp /path/to/junit.jar {project_root}/libs
(etc. for all required libraries)

Add the following to the junit encapsulating target of your build.xml file:

<target name="my-junit-tests">
    <junit printsummary="yes" fork="yes" forkmode="perTest" haltonerror="no" haltonfailure="no" showoutput="true">

        <!-- set these JVM arguments to enable recording failed tests -->
        <jvmarg value="-XX:-Inline"/>
        <jvmarg value="-XX:TieredStopAtLevel=1"/>
        <jvmarg value="-XX:UseAVX=2"/>
        <jvmarg value="-agentpath:/path/to/lr4j-record-1.0.so=save_on=failure"/>

    <!-- add other nested elements as required... -->

    </junit>
<target name="my-junit-tests">

And ensure that Ant’s parallel task is not being used to wrap <junit/>.

3. Ant: run all tests, and re-run failing test methods under recording

The following instructions are for JUnit 4. For JUnit 5 see the rerunner-jupiter extension.

Add the following rule to each of your test classes. If the test fails it will re-run the test under recording until it fails again.

@Rule
public final TestRule wrapper =
        new TestRule() {
            @Override
            public Statement apply(final Statement base, final Description description) {
                return new Statement() {
                    @Override
                    public void evaluate() throws Throwable {
                        try {
                            base.evaluate();
                        } catch (Throwable e) {
                            UndoLR.setEventLogSize(1000 * 1000 * 1000);
                            boolean testSucceeded = true;
                            for (int i = 0; i < 5 && testSucceeded; i++) {
                                UndoLR.start();
                                try {
                                    base.evaluate();
                                } catch (Throwable t) {
                                    UndoLR.save(description + ".undo");
                                    testSucceeded = false;
                                } finally {
                                    UndoLR.stop();
                                }
                            }
                            throw e;
                        }
                    }
                };
            }
        };

Create a libs folder under your project root and add the follows jars to it:

  • LiveRecorder API

  • JUnit API (unless already part of Ant’s own libraries)

  • Any other libraries required for the tests to run.

mkdir {project_root}/libs
cp /path/to/lr4j/lr4j_api-1.1.jar {project_root}/libs
cp /path/to/junit.jar {project_root}/libs
(etc. for all required libraries)

Add the following to the junit encapsulating target of your build.xml file:

<target name="my-junit-tests">
    <retry retrycount="5">
        <junit printsummary="yes" fork="yes" forkmode="once" haltonerror="yes" haltonfailure="yes" showoutput="true">

            <!-- set these JVM arguments to enable recording failed tests -->
            <jvmarg value="-XX:-Inline"/>
            <jvmarg value="-XX:TieredStopAtLevel=1"/>
            <jvmarg value="-XX:UseAVX=2"/>
            <jvmarg value="-agentpath:/path/to/lr4j-record-1.0.so=save_on=failure"/>

            <!-- add other nested elements as required... -->

        </junit>
    </retry>
<target>

Note that the forkmode is set to once to re-use the same JVM. It may also be set to perBatch if running multiple batches of tests.

Also ensure that Ant’s parallel task is not being used to wrap <junit/>.

*

Apache Ant and the Apache Ant project logo are trademarks of the Apache Software Foundation in the United States and/or other countries. No endorsement by The Apache Software Foundation is implied by the use of these marks.