From 412c138f6a087a926ea00f5e35c949c15f62d6d1 Mon Sep 17 00:00:00 2001 From: Christoph Deppisch Date: Wed, 6 Nov 2024 19:37:13 +0100 Subject: [PATCH] chore: Catch all errors to avoid loosing failures - Avoid test false positives when errors such as ClassNotFoundError are the cause of the test failure --- .../org/citrusframework/DefaultTestCase.java | 13 ++++++++----- .../common/DefaultTestLoader.java | 18 +++++++++++++----- .../citrusframework/container/Parallel.java | 2 +- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/core/citrus-base/src/main/java/org/citrusframework/DefaultTestCase.java b/core/citrus-base/src/main/java/org/citrusframework/DefaultTestCase.java index 8d2f69e8b0..b5b062920b 100644 --- a/core/citrus-base/src/main/java/org/citrusframework/DefaultTestCase.java +++ b/core/citrus-base/src/main/java/org/citrusframework/DefaultTestCase.java @@ -120,7 +120,7 @@ public void doExecute(final TestContext context) { } catch (final TestCaseFailedException e) { gracefullyStopTimer(); throw e; - } catch (final Exception | AssertionError e) { + } catch (final Exception | Error e) { testResult = getTestResultInstanceProvider(context).createFailed(this, e); throw new TestCaseFailedException(e); } @@ -143,7 +143,7 @@ public void start(final TestContext context) { debugVariables("Test", context); beforeTest(context); - } catch (final Exception | AssertionError e) { + } catch (final Exception | Error e) { testResult = getTestResultInstanceProvider(context).createFailed(this, e); throw new TestCaseFailedException(e); } @@ -171,7 +171,7 @@ public void afterTest(final TestContext context) { if (sequenceAfterTest.shouldExecute(getName(), packageName, groups)) { sequenceAfterTest.execute(context); } - } catch (final Exception | AssertionError e) { + } catch (final Exception | Error e) { logger.warn("After test failed with errors", e); } } @@ -194,7 +194,7 @@ public void executeAction(final TestAction action, final TestContext context) { } else { context.getTestActionListeners().onTestActionSkipped(this, action); } - } catch (final Exception | AssertionError e) { + } catch (final Exception | Error e) { testResult = getTestResultInstanceProvider(context).createFailed(this, e); throw new TestCaseFailedException(e); } finally { @@ -238,8 +238,11 @@ public void finish(final TestContext context) { throw new TestCaseFailedException(contextException); } } catch (final TestCaseFailedException e) { + if (isNull(testResult) || testResult.isSuccess()) { + testResult = getTestResultInstanceProvider(context).createFailed(this, e.getCause()); + } throw e; - } catch (final Exception | AssertionError e) { + } catch (final Exception | Error e) { testResult = getTestResultInstanceProvider(context).createFailed(this, e); throw new TestCaseFailedException(e); } finally { diff --git a/core/citrus-base/src/main/java/org/citrusframework/common/DefaultTestLoader.java b/core/citrus-base/src/main/java/org/citrusframework/common/DefaultTestLoader.java index fab4e795e2..248e836dd6 100644 --- a/core/citrus-base/src/main/java/org/citrusframework/common/DefaultTestLoader.java +++ b/core/citrus-base/src/main/java/org/citrusframework/common/DefaultTestLoader.java @@ -16,6 +16,10 @@ package org.citrusframework.common; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + import org.citrusframework.Citrus; import org.citrusframework.CitrusContext; import org.citrusframework.DefaultTestCase; @@ -28,10 +32,6 @@ import org.citrusframework.exceptions.CitrusRuntimeException; import org.citrusframework.exceptions.TestCaseFailedException; -import java.util.ArrayList; -import java.util.List; -import java.util.function.Consumer; - import static org.citrusframework.TestResult.failed; /** @@ -91,9 +91,17 @@ public final void load() { try { doLoad(); } catch (TestCaseFailedException e) { + if (testCase == null) { + testCase = runner.getTestCase(); + } + + if (testCase.getTestResult() == null || testCase.getTestResult().isSuccess()) { + testCase.setTestResult(failed(testCase.getName(), testCase.getTestClass().getName(), e)); + } + // This kind of exception indicates that the error has already been handled. Just throw and end test run. throw e; - } catch (Exception | AssertionError e) { + } catch (Exception | Error e) { if (testCase == null) { testCase = runner.getTestCase(); } diff --git a/core/citrus-base/src/main/java/org/citrusframework/container/Parallel.java b/core/citrus-base/src/main/java/org/citrusframework/container/Parallel.java index 01f2799cbe..3a5f7ed305 100644 --- a/core/citrus-base/src/main/java/org/citrusframework/container/Parallel.java +++ b/core/citrus-base/src/main/java/org/citrusframework/container/Parallel.java @@ -107,7 +107,7 @@ public void run() { } catch (CitrusRuntimeException e) { logger.error("Parallel test action raised error", e); exceptionHandler.accept(e); - } catch (Exception | AssertionError e) { + } catch (Exception | Error e) { logger.error("Parallel test action raised error", e); exceptionHandler.accept(new CitrusRuntimeException(e)); }