Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Catch all errors to avoid loosing failures #1255

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ protected void doLoad() {
citrus.run(testCase, context);
handler.forEach(handler -> handler.accept(testCase));
} catch (NoSuchBeanDefinitionException e) {
throw citrusContext.getTestContextFactory().getObject()
.handleError(testName, packageName, "Failed to load Spring XML test with name '" + testName + "'", e);
throw context.handleError(testName, packageName, "Failed to load Spring XML test with name '" + testName + "'", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ protected void doLoad() {

handler.forEach(it -> it.accept(testCase));
} catch (IOException e) {
throw citrusContext.getTestContextFactory().getObject()
.handleError(testName, packageName, "Failed to load Groovy test source '" + testName + "'", e);
throw context.handleError(testName, packageName, "Failed to load Groovy test source '" + testName + "'", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ public void doLoad() {
citrus.run(testCase, context);
handler.forEach(handler -> handler.accept(testCase));
} catch (JAXBException | IOException e) {
throw citrusContext.getTestContextFactory().getObject()
.handleError(testName, packageName, "Failed to load XML test with name '" + testName + "'", e);
throw context.handleError(testName, packageName, "Failed to load XML test with name '" + testName + "'", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ public void doLoad() {
citrus.run(testCase, context);
handler.forEach(handler -> handler.accept(testCase));
} catch (IOException e) {
throw citrusContext.getTestContextFactory().getObject()
.handleError(testName, packageName, "Failed to load YAML test with name '" + testName + "'", e);
throw context.handleError(testName, packageName, "Failed to load YAML test with name '" + testName + "'", e);
}
}

Expand Down
Loading