Skip to content

Commit

Permalink
chore: Catch all errors to avoid loosing failures
Browse files Browse the repository at this point in the history
- Avoid test false positives when errors such as ClassNotFoundError are the cause of the test failure
  • Loading branch information
christophd committed Nov 6, 2024
1 parent e35182d commit d2dc729
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 19 deletions.
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

0 comments on commit d2dc729

Please sign in to comment.