Skip to content

Commit

Permalink
Improve AssertionFailedError sanitization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MaisiKoleni committed Mar 31, 2021
1 parent 0ad2d46 commit 1fc4acb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
29 changes: 21 additions & 8 deletions src/test/java/de/tum/in/test/api/ExceptionFailureTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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.*;

Expand All @@ -8,6 +9,7 @@
import org.assertj.core.error.AssertJMultipleFailuresError;
import org.assertj.core.error.MultipleAssertionsError;
import org.junit.platform.testkit.engine.Events;
import org.opentest4j.AssertionFailedError;
import org.opentest4j.MultipleFailuresError;

import de.tum.in.test.api.util.UnexpectedExceptionError;
Expand All @@ -23,8 +25,9 @@ class ExceptionFailureTest {
@UserTestResults
private static Events tests;

private final String assertionFailed = "assertionFailed";
private final String assertJMultipleFailures = "assertJMultipleFailures";
private final String assertionFailOnly = "assertionFailOnly";
private final String assertionFailed = "assertionFailed";
private final String customException = "customException";
private final String exceptionInInitializer = "exceptionInInitializer";
private final String faultyGetCauseException = "faultyGetCauseException";
Expand All @@ -36,13 +39,6 @@ class ExceptionFailureTest {
private final String throwExceptionInInitializerError = "throwExceptionInInitializerError";
private final String throwNullPointerException = "throwNullPointerException";

@TestTest
void test_assertionFailed() {
tests.assertThatEvents().haveExactly(1,
event(test(assertionFailed), finishedWithFailure(instanceOf(AssertionError.class),
message(m -> m.contains("expected: <1> but was: <2>")))));
}

@TestTest
void test_assertJMultipleFailures() {
tests.assertThatEvents().haveExactly(1,
Expand All @@ -54,6 +50,23 @@ void test_assertJMultipleFailures() {
new Condition<>(t -> t.getSuppressed().length == 2, "failures added as suppressed"))));
}

@TestTest
void test_assertionFailOnly() {
tests.assertThatEvents().haveExactly(1,
testFailedWith(assertionFailOnly, AssertionFailedError.class, "This test failed. Penguin."));
}

@TestTest
void test_assertionFailed() {
tests.assertThatEvents().haveExactly(1,
event(test(assertionFailed), finishedWithFailure(instanceOf(AssertionFailedError.class),
message("expected: <1> but was: <2>"), new Condition<>(t -> {
var afe = (AssertionFailedError) t;
return afe.getActual().getStringRepresentation().equals("2")
&& afe.getExpected().getStringRepresentation().equals("1");
}, "expected and actual are correct"))));
}

@TestTest
void test_customException() {
tests.assertThatEvents().haveExactly(1,
Expand Down
15 changes: 10 additions & 5 deletions src/test/java/de/tum/in/testuser/ExceptionFailureUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ static class InitFailure {
}
}

@PublicTest
void assertionFailed() throws Exception {
assertEquals(1, 2);
}

@PublicTest
void assertJMultipleFailures() {
assertThat("ABC").satisfiesAnyOf(s -> {
Expand All @@ -79,6 +74,16 @@ void assertJMultipleFailures() {
});
}

@PublicTest
void assertionFailOnly() throws Exception {
org.junit.jupiter.api.Assertions.fail("This test failed. Penguin.");
}

@PublicTest
void assertionFailed() throws Exception {
assertEquals(1, 2);
}

@PublicTest
void customException() {
throw new CustomException();
Expand Down

0 comments on commit 1fc4acb

Please sign in to comment.