public final class ExpectFailure extends Object implements org.junit.rules.TestRule
@Rule
, so that unexpectedly-passing
assertions will cause your test to fail. In Java 8 you can use expectFailure()
to instead capture failures directly.
By design this class expects exactly one failure per instance, meaning you should create
separate tests for each failure case you intend to assert on, rather than trying to capture
multiple failures in a single test. With expectFailure()
you can safely capture multiple
failures in the same test method, but creating separate tests is still encouraged. (Currently,
calling whenTesting()
more than once on the same instance fails. However, you should not
rely on this behaviour.)
Note: this class is not intended for general use; it exists primarily to write tests of
Subject
implementations and other Truth internals. If you are writing your own custom
subject you are encouraged to use this class rather than define a custom FailureStrategy
or directly catch AssertionError
. Use expectFailure.whenTesting().about(subjectFactory()).that(...)
to make assertions about custom
subjects.
Modifier and Type | Class and Description |
---|---|
static interface |
ExpectFailure.AssertionCallback
A "functional interface" for
expectFailure() to invoke and capture
failures. |
static interface |
ExpectFailure.DelegatedAssertionCallback<S extends Subject<S,D>,D>
A "functional interface" for
expectFailureAbout() to invoke and
capture failures. |
Constructor and Description |
---|
ExpectFailure() |
Modifier and Type | Method and Description |
---|---|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description) |
static AssertionError |
expectFailure(ExpectFailure.AssertionCallback assertionCallback)
Static alternative that directly returns the triggered failure.
|
static <S extends Subject<S,D>,D> |
expectFailureAbout(SubjectFactory<S,D> factory,
ExpectFailure.DelegatedAssertionCallback<S,D> assertionCallback)
Static alternative that directly returns the triggered failure.
|
AssertionError |
getFailure()
Returns the captured failure, if one occurred.
|
TestVerb |
whenTesting()
Returns a test verb that expects the chained assertion to fail, and makes the failure available
via
getFailure() . |
public TestVerb whenTesting()
getFailure()
.public AssertionError getFailure()
public static AssertionError expectFailure(ExpectFailure.AssertionCallback assertionCallback)
expectThrows()
:
AssertionError failure = expectFailure(whenTesting ->
whenTesting.that(4).isNotEqualTo(4));
public static <S extends Subject<S,D>,D> AssertionError expectFailureAbout(SubjectFactory<S,D> factory, ExpectFailure.DelegatedAssertionCallback<S,D> assertionCallback)
expectThrows()
:
AssertionError failure = expectFailureAbout(myTypes(), whenTesting ->
whenTesting.that(myType).hasProperty());
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
apply
in interface org.junit.rules.TestRule
Copyright © 2017. All rights reserved.