aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java/com/google/common/truth/ExpectFailure.java
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main/java/com/google/common/truth/ExpectFailure.java')
-rw-r--r--core/src/main/java/com/google/common/truth/ExpectFailure.java29
1 files changed, 9 insertions, 20 deletions
diff --git a/core/src/main/java/com/google/common/truth/ExpectFailure.java b/core/src/main/java/com/google/common/truth/ExpectFailure.java
index 970f5261..5b036085 100644
--- a/core/src/main/java/com/google/common/truth/ExpectFailure.java
+++ b/core/src/main/java/com/google/common/truth/ExpectFailure.java
@@ -23,6 +23,7 @@ import static com.google.common.truth.TruthFailureSubject.truthFailures;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.truth.Truth.SimpleAssertionError;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
@@ -66,14 +67,6 @@ import org.junit.runners.model.Statement;
* {@link FailureStrategy#fail} only once.
*/
public final class ExpectFailure implements Platform.JUnitTestRule {
- private final FailureStrategy strategy =
- new FailureStrategy() {
- @Override
- public void fail(AssertionError failure) {
- captureFailure(failure);
- }
- };
-
private boolean inRuleContext = false;
private boolean failureExpected = false;
private @Nullable AssertionError failure = null;
@@ -101,7 +94,7 @@ public final class ExpectFailure implements Platform.JUnitTestRule {
"ExpectFailure.whenTesting() called previously, but did not capture a failure.");
}
failureExpected = true;
- return StandardSubjectBuilder.forCustomFailureStrategy(strategy);
+ return StandardSubjectBuilder.forCustomFailureStrategy(this::captureFailure);
}
/**
@@ -161,6 +154,7 @@ public final class ExpectFailure implements Platform.JUnitTestRule {
* <p>{@code AssertionError failure = expectFailure(whenTesting ->
* whenTesting.that(4).isNotEqualTo(4));}
*/
+ @CanIgnoreReturnValue
public static AssertionError expectFailure(StandardSubjectBuilderCallback assertionCallback) {
ExpectFailure expectFailure = new ExpectFailure();
expectFailure.enterRuleContext(); // safe since this instance doesn't leave this method
@@ -175,30 +169,25 @@ public final class ExpectFailure implements Platform.JUnitTestRule {
* <p>{@code AssertionError failure = expectFailureAbout(myTypes(), whenTesting ->
* whenTesting.that(myType).hasProperty());}
*/
+ @CanIgnoreReturnValue
public static <S extends Subject, A> AssertionError expectFailureAbout(
- final Subject.Factory<S, A> factory,
- final SimpleSubjectBuilderCallback<S, A> assertionCallback) {
- // whenTesting -> assertionCallback.invokeAssertion(whenTesting.about(factory))
+ Subject.Factory<S, A> factory, SimpleSubjectBuilderCallback<S, A> assertionCallback) {
return expectFailure(
- new StandardSubjectBuilderCallback() {
- @Override
- public void invokeAssertion(StandardSubjectBuilder whenTesting) {
- assertionCallback.invokeAssertion(whenTesting.about(factory));
- }
- });
+ whenTesting -> assertionCallback.invokeAssertion(whenTesting.about(factory)));
}
/**
* Creates a subject for asserting about the given {@link AssertionError}, usually one produced by
* Truth.
*/
- public static TruthFailureSubject assertThat(AssertionError actual) {
+ public static TruthFailureSubject assertThat(@Nullable AssertionError actual) {
return assertAbout(truthFailures()).that(actual);
}
@Override
@GwtIncompatible("org.junit.rules.TestRule")
- public Statement apply(final Statement base, Description description) {
+ @J2ktIncompatible
+ public Statement apply(Statement base, Description description) {
checkNotNull(base);
checkNotNull(description);
return new Statement() {