-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce rootCause() condition for use with the EngineTestKit
Closes #3839
- Loading branch information
Showing
3 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...s/src/test/java/org/junit/platform/testkit/engine/TestExecutionResultConditionsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.testkit.engine; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import org.assertj.core.api.Condition; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.commons.PreconditionViolationException; | ||
|
||
/** | ||
* Tests for {@link TestExecutionResultConditions}. | ||
* | ||
* @since 1.11 | ||
*/ | ||
class TestExecutionResultConditionsTests { | ||
|
||
private static final String EXPECTED = "expected"; | ||
|
||
private static final String UNEXPECTED = "unexpected"; | ||
|
||
private static final Predicate<Throwable> messageEqualsExpected = // | ||
throwable -> EXPECTED.equals(throwable.getMessage()); | ||
|
||
private static final Condition<Throwable> expectedMessageCondition = // | ||
new Condition<>(messageEqualsExpected, "message matches %s", EXPECTED); | ||
|
||
private static final Condition<Throwable> rootCauseCondition = // | ||
TestExecutionResultConditions.rootCause(expectedMessageCondition); | ||
|
||
@Test | ||
void rootCauseFailsForNullThrowable() { | ||
assertThatExceptionOfType(PreconditionViolationException.class)// | ||
.isThrownBy(() -> rootCauseCondition.matches(null))// | ||
.withMessage("Throwable must not be null"); | ||
} | ||
|
||
@Test | ||
void rootCauseFailsForThrowableWithoutCause() { | ||
Throwable throwable = new Throwable(); | ||
|
||
assertThatExceptionOfType(PreconditionViolationException.class)// | ||
.isThrownBy(() -> rootCauseCondition.matches(throwable))// | ||
.withMessage("Throwable does not have a cause"); | ||
} | ||
|
||
@Test | ||
void rootCauseMatchesForDirectCauseWithExpectedMessage() { | ||
RuntimeException cause = new RuntimeException(EXPECTED); | ||
Throwable throwable = new Throwable(cause); | ||
|
||
assertThat(rootCauseCondition.matches(throwable)).isTrue(); | ||
} | ||
|
||
@Test | ||
void rootCauseDoesNotMatchForDirectCauseWithDifferentMessage() { | ||
RuntimeException cause = new RuntimeException(UNEXPECTED); | ||
Throwable throwable = new Throwable(cause); | ||
|
||
assertThat(rootCauseCondition.matches(throwable)).isFalse(); | ||
} | ||
|
||
@Test | ||
void rootCauseMatchesForRootCauseWithExpectedMessage() { | ||
RuntimeException rootCause = new RuntimeException(EXPECTED); | ||
RuntimeException intermediateCause = new RuntimeException("intermediate cause", rootCause); | ||
Throwable throwable = new Throwable(intermediateCause); | ||
|
||
assertThat(rootCauseCondition.matches(throwable)).isTrue(); | ||
} | ||
|
||
@Test | ||
void rootCauseDoesNotMatchForRootCauseWithDifferentMessage() { | ||
RuntimeException rootCause = new RuntimeException(UNEXPECTED); | ||
RuntimeException intermediateCause = new RuntimeException("intermediate cause", rootCause); | ||
Throwable throwable = new Throwable(intermediateCause); | ||
|
||
assertThat(rootCauseCondition.matches(throwable)).isFalse(); | ||
} | ||
|
||
} |
9b7957a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
9b7957a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 🙇