diff --git a/src/etc/testcases/taskdefs/optional/TEST-org.example.junitlauncher.jupiter.JupiterSampleTestFailingBeforeAll.txt b/src/etc/testcases/taskdefs/optional/TEST-org.example.junitlauncher.jupiter.JupiterSampleTestFailingBeforeAll.txt new file mode 100644 index 000000000..e69de29bb diff --git a/src/etc/testcases/taskdefs/optional/junitlauncher.xml b/src/etc/testcases/taskdefs/optional/junitlauncher.xml index 1d6076edb..cb9a5418e 100644 --- a/src/etc/testcases/taskdefs/optional/junitlauncher.xml +++ b/src/etc/testcases/taskdefs/optional/junitlauncher.xml @@ -300,6 +300,24 @@ + + + + + + + + + + + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java index a052a26c9..65ba96ee8 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java @@ -304,7 +304,7 @@ public class LauncherSupport { // print the summary to System.out summary.printTo(new PrintWriter(System.out, true)); } - final boolean hasTestFailures = summary.getTestsFailedCount() != 0; + final boolean hasTestFailures = summary.getTotalFailureCount() != 0; if (hasTestFailures) { // keep track of the test failure(s) for the entire launched instance this.testsFailed = true; diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junitlauncher/JUnitLauncherTaskTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junitlauncher/JUnitLauncherTaskTest.java index 5bfd21ed5..1bb53c13d 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junitlauncher/JUnitLauncherTaskTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junitlauncher/JUnitLauncherTaskTest.java @@ -17,12 +17,24 @@ */ package org.apache.tools.ant.taskdefs.optional.junitlauncher; +import static org.example.junitlauncher.Tracker.verifyFailed; +import static org.example.junitlauncher.Tracker.verifySetupFailed; +import static org.example.junitlauncher.Tracker.verifySkipped; +import static org.example.junitlauncher.Tracker.verifySuccess; +import static org.example.junitlauncher.Tracker.wasTestRun; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildFileRule; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.JUnitLauncherTask; import org.apache.tools.ant.util.LoaderUtils; import org.example.junitlauncher.jupiter.JupiterSampleTest; +import org.example.junitlauncher.jupiter.JupiterSampleTestFailingBeforeAll; import org.example.junitlauncher.jupiter.JupiterTagSampleTest; import org.example.junitlauncher.vintage.AlwaysFailingJUnit4Test; import org.example.junitlauncher.vintage.ForkedTest; @@ -32,16 +44,6 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import java.io.File; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -import static org.example.junitlauncher.Tracker.verifyFailed; -import static org.example.junitlauncher.Tracker.verifySkipped; -import static org.example.junitlauncher.Tracker.verifySuccess; -import static org.example.junitlauncher.Tracker.wasTestRun; - /** * Tests the {@link JUnitLauncherTask} */ @@ -78,7 +80,7 @@ public class JUnitLauncherTaskTest { } } } - + /** * Tests that when a test, that's isn't configured with {@code haltOnFailure=true}, continues the * build even when there are test failures @@ -368,6 +370,39 @@ public class JUnitLauncherTaskTest { Assert.assertFalse("testMethodIncludeTagisNotExecutedTagSampleTest2 was expected NOT to be run", wasTestRun(tracker2, JupiterTagSampleTest.class.getName(), "testMethodIncludeTagisNotExecutedTagSampleTest2")); } + + + /** + * Tests that failure at with beforeall stops the build + */ + @Test + public void testBeforeAllFailureStopsBuild() throws Exception { + final String targetName = "test-beforeall-failure-stops-build"; + final Path trackerFile = setupTrackerProperty(targetName); + try { + buildRule.executeTarget(targetName); + Assert.fail(targetName + " was expected to fail"); + } catch (BuildException e) { + // expected, but do further tests to make sure the build failed for expected reason + if (!verifySetupFailed(trackerFile, JupiterSampleTestFailingBeforeAll.class.getName())) { + // throw back the original cause + throw e; + } + } + } + + /** + * Tests that when a test, that's isn't configured with {@code haltOnFailure=true}, continues the + * build even when there are test failures + */ + @Test + public void testBeforeAllFailureContinuesBuild() throws Exception { + final String targetName = "test-beforeall-failure-continues-build"; + final Path trackerFile = setupTrackerProperty(targetName); + buildRule.executeTarget(targetName); + Assert.assertTrue("Expected @BeforeAll failure to lead to failing testcase", verifySetupFailed(trackerFile, JupiterSampleTestFailingBeforeAll.class.getName())); + } + /** * Tests execution of a test which is configured to execute only methods with special tags, two classes specified diff --git a/src/tests/junit/org/example/junitlauncher/Tracker.java b/src/tests/junit/org/example/junitlauncher/Tracker.java index 1437cd679..ba31ec872 100644 --- a/src/tests/junit/org/example/junitlauncher/Tracker.java +++ b/src/tests/junit/org/example/junitlauncher/Tracker.java @@ -143,6 +143,11 @@ public class Tracker implements TestResultFormatter { final List lines = readTrackerFile(trackerFile); return lines.contains(TestExecutionResult.Status.FAILED + ":test-method:" + className + "#" + methodName); } + + public static boolean verifySetupFailed(final Path trackerFile, final String className) throws IOException { + final List lines = readTrackerFile(trackerFile); + return lines.contains(TestExecutionResult.Status.FAILED + ":test-class:" + className); + } public static boolean verifySuccess(final Path trackerFile, final String className, final String methodName) throws IOException { final List lines = readTrackerFile(trackerFile); diff --git a/src/tests/junit/org/example/junitlauncher/jupiter/JupiterSampleTestFailingBeforeAll.java b/src/tests/junit/org/example/junitlauncher/jupiter/JupiterSampleTestFailingBeforeAll.java new file mode 100644 index 000000000..d42e70349 --- /dev/null +++ b/src/tests/junit/org/example/junitlauncher/jupiter/JupiterSampleTestFailingBeforeAll.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.example.junitlauncher.jupiter; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * + */ +public class JupiterSampleTestFailingBeforeAll { + + private static final String message = "The quick brown fox jumps over the lazy dog"; + + @BeforeAll + static void beforeAll() { + throw new RuntimeException("Intentional failure"); + } + + @BeforeEach + void beforeEach() { + } + + @Test + void testSucceeds() { + System.out.println(message); + System.out.print("Hello world! "); + } + + @AfterEach + void afterEach() { + } + + @AfterAll + static void afterAll() { + } +}