From eb782b6584cee6ca0d31a9d5f80f8f0f220f9ac6 Mon Sep 17 00:00:00 2001 From: Christian Femers Date: Wed, 31 Mar 2021 18:22:17 +0200 Subject: [PATCH] Improve handling of failures that only consist of the message Those changes had not any effect on the message, but appeared as null and null comparison in the IDE when looking at the failure details. --- .../sanitization/AssertionFailedErrorSanitizer.java | 9 +++++++-- .../java/de/tum/in/test/api/ExceptionFailureTest.java | 7 +++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tum/in/test/api/internal/sanitization/AssertionFailedErrorSanitizer.java b/src/main/java/de/tum/in/test/api/internal/sanitization/AssertionFailedErrorSanitizer.java index 710e3c99..9cbcfa2c 100644 --- a/src/main/java/de/tum/in/test/api/internal/sanitization/AssertionFailedErrorSanitizer.java +++ b/src/main/java/de/tum/in/test/api/internal/sanitization/AssertionFailedErrorSanitizer.java @@ -24,8 +24,13 @@ public Throwable sanitize(Throwable t, MessageTransformer messageTransformer) { ValueWrapper actual = afe.getActual(); ThrowableInfo info = ThrowableInfo.getEssentialInfosSafeFrom(t).sanitize(); String newMessage = messageTransformer.apply(info); - AssertionFailedError newAfe = new AssertionFailedError(newMessage, sanitizeValue(expected), - sanitizeValue(actual)); + AssertionFailedError newAfe; + // If both expected and actual are null, this cannot arise from assertEquals, + // and thus afe was constructed only by providing a message (and cause) + if (expected == null && actual == null) + newAfe = new AssertionFailedError(newMessage); + else + newAfe = new AssertionFailedError(newMessage, sanitizeValue(expected), sanitizeValue(actual)); SanitizationUtils.copyThrowableInfoSafe(info, newAfe); return newAfe; } diff --git a/src/test/java/de/tum/in/test/api/ExceptionFailureTest.java b/src/test/java/de/tum/in/test/api/ExceptionFailureTest.java index 57978fbb..fd2784f8 100644 --- a/src/test/java/de/tum/in/test/api/ExceptionFailureTest.java +++ b/src/test/java/de/tum/in/test/api/ExceptionFailureTest.java @@ -1,6 +1,5 @@ package de.tum.in.test.api; -import static de.tum.in.test.testutilities.CustomConditions.testFailedWith; import static org.junit.platform.testkit.engine.EventConditions.*; import static org.junit.platform.testkit.engine.TestExecutionResultConditions.*; @@ -53,7 +52,11 @@ void test_assertJMultipleFailures() { @TestTest void test_assertionFailOnly() { tests.assertThatEvents().haveExactly(1, - testFailedWith(assertionFailOnly, AssertionFailedError.class, "This test failed. Penguin.")); + event(test(assertionFailOnly), finishedWithFailure(instanceOf(AssertionFailedError.class), + message("This test failed. Penguin."), new Condition<>(t -> { + var afe = (AssertionFailedError) t; + return afe.getActual() == null && afe.getExpected() == null; + }, "expected and actual are both null")))); } @TestTest