public final class ExpectFailure extends Object implements org.junit.rules.TestRule
Subject
fail when they should.
Usage:
AssertionError failure =
expectFailure(whenTesting -> whenTesting.that(cancelButton).isVisible());
assertThat(failure).hasMessageThat().contains("visible");
...
private static AssertionError expectFailure(
ExpectFailure.SimpleSubjectBuilderCallback<UiElementSubject, UiElement> assertionCallback) {
return ExpectFailure.expectFailureAbout(uiElements(), assertionCallback);
}
Or, if you can't use lambdas:
@Rule public final ExpectFailure expectFailure = new ExpectFailure();
... expectFailure.whenTesting().about(uiElements()).that(cancelButton).isVisible(); assertThat(expectFailure.getFailure()).hasMessageThat().contains("visible");
Modifier and Type | Class and Description |
---|---|
static interface |
ExpectFailure.SimpleSubjectBuilderCallback<S extends Subject<S,A>,A>
A "functional interface" for
expectFailureAbout() to invoke and
capture failures. |
static interface |
ExpectFailure.StandardSubjectBuilderCallback
A "functional interface" for
expectFailure() 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.StandardSubjectBuilderCallback assertionCallback)
Static alternative that directly returns the triggered failure.
|
static <S extends Subject<S,A>,A> |
expectFailureAbout(Subject.Factory<S,A> factory,
ExpectFailure.SimpleSubjectBuilderCallback<S,A> assertionCallback)
Static alternative that directly returns the triggered failure.
|
static <S extends Subject<S,A>,A> |
expectFailureAbout(SubjectFactory<S,A> factory,
ExpectFailure.SimpleSubjectBuilderCallback<S,A> assertionCallback)
Deprecated.
When you switch from
SubjectFactory to Subject.Factory , you'll
switch from this overload to the Subject.Factory overload. |
AssertionError |
getFailure()
Returns the captured failure, if one occurred.
|
StandardSubjectBuilder |
whenTesting()
Returns a test verb that expects the chained assertion to fail, and makes the failure available
via
getFailure() . |
public StandardSubjectBuilder whenTesting()
getFailure()
.
An instance of ExpectFailure
supports only one whenTesting
call per test
method. The static expectFailure(com.google.common.truth.ExpectFailure.StandardSubjectBuilderCallback)
method, by contrast, does not have this limitation.
public AssertionError getFailure()
public static AssertionError expectFailure(ExpectFailure.StandardSubjectBuilderCallback assertionCallback)
expectThrows()
:
AssertionError failure = expectFailure(whenTesting ->
whenTesting.that(4).isNotEqualTo(4));
@Deprecated public static <S extends Subject<S,A>,A> AssertionError expectFailureAbout(SubjectFactory<S,A> factory, ExpectFailure.SimpleSubjectBuilderCallback<S,A> assertionCallback)
SubjectFactory
to Subject.Factory
, you'll
switch from this overload to the Subject.Factory
overload.expectThrows()
:
AssertionError failure = expectFailureAbout(myTypes(), whenTesting ->
whenTesting.that(myType).hasProperty());
public static <S extends Subject<S,A>,A> AssertionError expectFailureAbout(Subject.Factory<S,A> factory, ExpectFailure.SimpleSubjectBuilderCallback<S,A> 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.