|
64 | 64 | * href="https://junit.org/junit4/javadoc/latest/org/junit/Assert.html#assertThrows%28java.lang.Class,%20org.junit.function.ThrowingRunnable%29">JUnit |
65 | 65 | * 4</a>, <a |
66 | 66 | * href="https://docs.junit.org/current/api/org.junit.jupiter.api/org/junit/jupiter/api/Assertions.html#assertThrows(java.lang.Class,org.junit.jupiter.api.function.Executable)">JUnit |
67 | | - * 5</a>). We recommend it over {@code assertThrows} when you're testing a Truth subject because it |
68 | | - * also checks that the assertion you're testing uses the supplied {@link FailureStrategy} and calls |
69 | | - * {@link FailureStrategy#fail} only once. |
| 67 | + * 5</a>). We recommend it over {@code assertThrows} when you're testing a Truth subject because: |
| 68 | + * |
| 69 | + * <ul> |
| 70 | + * <li>It performs additional checks: |
| 71 | + * <ul> |
| 72 | + * <li>It checks that the assertion you're testing uses the supplied {@link |
| 73 | + * FailureStrategy}. |
| 74 | + * <li>It checks that the assertion you're testing calls {@link FailureStrategy#fail} only |
| 75 | + * once. |
| 76 | + * </ul> |
| 77 | + * <li>It instructs Truth to generate failure messages <i>without</i> adding lines like "value of: |
| 78 | + * foo()" for {@code assertThat(foo())....} calls that it detects in the test bytecode. Truth |
| 79 | + * doesn't provide guarantees for when such lines will be generated, so tests become more |
| 80 | + * resilient without them. |
| 81 | + * </ul> |
70 | 82 | */ |
71 | 83 | public final class ExpectFailure implements Platform.JUnitTestRule { |
72 | 84 | private boolean inRuleContext = false; |
@@ -97,9 +109,16 @@ public StandardSubjectBuilder whenTesting() { |
97 | 109 | "ExpectFailure.whenTesting() called previously, but did not capture a failure."); |
98 | 110 | } |
99 | 111 | failureExpected = true; |
100 | | - return StandardSubjectBuilder.forCustomFailureStrategy(this::captureFailure); |
| 112 | + return StandardSubjectBuilder.forCustomFailureStrategy( |
| 113 | + (ExpectFailureFailureStrategy) this::captureFailure); |
101 | 114 | } |
102 | 115 |
|
| 116 | + /** |
| 117 | + * Marker interface that we check for so that we can automatically disable "value of" lines during |
| 118 | + * {@link ExpectFailure} assertions. |
| 119 | + */ |
| 120 | + interface ExpectFailureFailureStrategy extends FailureStrategy {} |
| 121 | + |
103 | 122 | /** |
104 | 123 | * Enters rule context to be ready to capture failures. |
105 | 124 | * |
|
0 commit comments