S
- the self-type, allowing this
-returning methods to avoid needing subclassingT
- the type of the object being tested by this Subject
public class Subject<S extends Subject<S,T>,T> extends Object
Subject
contains isEqualTo(Object)
and isInstanceOf(Class)
, and StringSubject
contains startsWith(String)
.
To create a Subject
instance, most users will call an assertThat
method. For information about other ways to create an instance, see this FAQ entry.
For information about writing a custom Subject
, see our doc on extensions.
Modifier and Type | Class and Description |
---|---|
static interface |
Subject.Factory<SubjectT extends Subject<SubjectT,ActualT>,ActualT>
|
Modifier | Constructor and Description |
---|---|
protected |
Subject(FailureMetadata metadata,
T actual)
Constructor for use by subclasses.
|
Modifier and Type | Method and Description |
---|---|
protected T |
actual()
Returns the unedited, unformatted raw actual value.
|
protected String |
actualAsString()
Returns a string representation of the actual value.
|
protected String |
actualCustomStringRepresentation()
Supplies the direct string representation of the actual value to other methods which may prefix
or otherwise position it in an error message.
|
protected StandardSubjectBuilder |
check()
Returns a builder for creating a derived subject but without providing information about how
the derived subject will relate to the current subject.
|
protected StandardSubjectBuilder |
check(String format,
Object... args)
Returns a builder for creating a derived subject.
|
boolean |
equals(Object o)
Deprecated.
Object.equals(Object) is not supported on Truth subjects. If you meant to
test object equality between an expected and the actual value, use isEqualTo(Object) instead. |
protected void |
fail(String check)
Deprecated.
Prefer to construct
Fact -style methods, typically by using failWithActual ( simpleFact(...) ) . However, if you want to preserve your exact failure message as a
migration aid, you can inline this method (and then inline the resulting method call, as
well). |
protected void |
fail(String verb,
Object... messageParts)
Deprecated.
Prefer to construct
Fact -style methods, typically by using failWithActual(Fact, Fact...) . However, if you want to preserve your exact failure
message as a migration aid, you can inline this method. |
protected void |
fail(String verb,
Object other)
Deprecated.
Prefer to construct
Fact -style methods, typically by using failWithActual(String, Object) . However, if you want to preserve your exact failure
message as a migration aid, you can inline this method (and then inline the resulting
method call, as well). |
protected void |
failComparing(String message,
CharSequence expected,
CharSequence actual)
Deprecated.
Instead of manually testing whether the actual value is equal to the expected and
then calling this method, ask Truth to do both with a statement like
check("foo()").that(actual().foo()).isEqualTo(expectedFoo) . (If you are comparing the
values using a method other than Object.equals(java.lang.Object) , you'll have to wrap your actual and
expected values in a custom object that implements your desired logic in equals and
forwards the toString method.) |
protected void |
failComparing(String message,
CharSequence expected,
CharSequence actual,
Throwable cause)
Deprecated.
See the other
overload of this method for instructions on how to get Truth to throw a
ComparisonFailure . To make Truth also attach the Throwable cause to the assertion
failure, see the instructions on failWithRawMessageAndCause(java.lang.String, java.lang.Throwable) . |
protected void |
failWithActual(Fact first,
Fact... rest)
Fails, reporting a message with the given facts, followed by an automatically added fact of the
form:
but was: actual value.
|
protected void |
failWithActual(String key,
Object value)
Fails, reporting a message with two "facts":
key: value
but was: actual value.
|
protected void |
failWithBadResults(String verb,
Object expected,
String failVerb,
Object actual)
Deprecated.
Prefer to construct
Fact -style methods, typically by using failWithActual(Fact, Fact...) . However, if you want to preserve your exact failure
message as a migration aid, you can inline this method. |
protected void |
failWithCustomSubject(String verb,
Object expected,
Object actual)
Deprecated.
Prefer to construct
Fact -style methods, typically by using failWithoutActual(Fact, Fact...) . However, if you want to preserve your exact failure
message as a migration aid, you can inline this method. |
protected void |
failWithoutActual(Fact first,
Fact... rest)
Fails, reporting a message with the given facts, without automatically adding the actual
value.
|
protected void |
failWithoutActual(String check)
Deprecated.
Prefer to construct
Fact -style methods, typically by using failWithoutActual ( simpleFact(...) ) . However, if you want to preserve your exact failure message as a
migration aid, you can inline this method (and then inline the resulting method call, as
well). |
protected void |
failWithoutSubject(String check)
Deprecated.
Prefer to construct
Fact -style methods, typically by using failWithoutActual ( simpleFact(...) ) . However, if you want to preserve your exact failure message as a
migration aid, you can inline this method. |
protected void |
failWithRawMessage(String message,
Object... parameters)
Deprecated.
Prefer to construct
Fact -style methods, typically by using failWithActual(Fact, Fact...) . However, if you want to preserve your exact failure
message as a migration aid, you can inline this method. |
protected void |
failWithRawMessageAndCause(String message,
Throwable cause)
Deprecated.
Pass the message to
failWithRawMessage(java.lang.String, java.lang.Object...) , but also, to make Truth attach the
Throwable cause to the assertion failure, ensure that cause appears in the
assertion chain. This should typically already be the case, as in assertThat(throwable).hasMessageThat().isEqualTo("message") . If it is not, you will need
to introduce an extra check call into your chain, something like check().about(unexpectedFailures()).that(cause).wasASuccess(message) , with unexpectedFailures() returning a factory for a "dummy" subject type that defines only a
wasASuccess method that fails unconditionally. |
protected T |
getSubject()
Deprecated.
Prefer
#actual() for direct access to the subject. |
int |
hashCode()
Deprecated.
Object.hashCode() is not supported on Truth subjects. |
protected StandardSubjectBuilder |
ignoreCheck()
Begins a new call chain that ignores any failures.
|
protected String |
internalCustomName()
An internal method used to obtain the value set by
named(String, Object...) . |
void |
isAnyOf(Object first,
Object second,
Object... rest)
Fails unless the subject is equal to any of the given elements.
|
void |
isEqualTo(Object expected)
Fails if the subject is not equal to the given object.
|
void |
isIn(Iterable<?> iterable)
Fails unless the subject is equal to any element in the given iterable.
|
void |
isInstanceOf(Class<?> clazz)
Fails if the subject is not an instance of the given class.
|
void |
isNoneOf(Object first,
Object second,
Object... rest)
Fails if the subject is equal to any of the given elements.
|
void |
isNotEqualTo(Object unexpected)
Fails if the subject is equal to the given object.
|
void |
isNotIn(Iterable<?> iterable)
Fails if the subject is equal to any element in the given iterable.
|
void |
isNotInstanceOf(Class<?> clazz)
Fails if the subject is an instance of the given class.
|
void |
isNotNull()
Fails if the subject is null.
|
void |
isNotSameAs(Object unexpected)
Fails if the subject is the same instance as the given object.
|
void |
isNull()
Fails if the subject is not null.
|
void |
isSameAs(Object expected)
Fails if the subject is not the same instance as the given object.
|
S |
named(String format,
Object... args)
Adds a prefix to the subject, when it is displayed in error messages.
|
String |
toString() |
protected Subject(FailureMetadata metadata, @NullableDecl T actual)
check()
.that(actual)
.protected String internalCustomName()
named(String, Object...)
.@CanIgnoreReturnValue public S named(String format, Object... args)
toString()
representation, e.g. boolean.
Writing assertThat(foo).named("foo").isTrue();
then results in a more reasonable error
message.
named()
takes a format template and argument objects which will be substituted into
the template using Strings.lenientFormat
.
Note this only supports the %s
specifier.
format
- a template with %s
placeholdersargs
- the object parameters which will be applied to the message template.public void isNull()
public void isNotNull()
public void isEqualTo(@NullableDecl Object expected)
Objects.equal(java.lang.Object, java.lang.Object)
Arrays.equals(long[], long[])
overload
Byte
, Short
, Character
, Integer
, or Long
) and they are numerically equal when converted to Long
.
public void isNotEqualTo(@NullableDecl Object unexpected)
isEqualTo(java.lang.Object)
method.public void isSameAs(@NullableDecl @CompatibleWith(value="T") Object expected)
public void isNotSameAs(@NullableDecl @CompatibleWith(value="T") Object unexpected)
public void isInstanceOf(Class<?> clazz)
public void isNotInstanceOf(Class<?> clazz)
public void isIn(Iterable<?> iterable)
public void isAnyOf(@NullableDecl @CompatibleWith(value="T") Object first, @NullableDecl @CompatibleWith(value="T") Object second, @NullableDecl Object... rest)
public void isNotIn(Iterable<?> iterable)
public void isNoneOf(@NullableDecl @CompatibleWith(value="T") Object first, @NullableDecl @CompatibleWith(value="T") Object second, @NullableDecl Object... rest)
@Deprecated protected T getSubject()
#actual()
for direct access to the subject.protected final T actual()
protected final String actualAsString()
@ForOverride protected String actualCustomStringRepresentation()
Subjects should override this with care.
By default, this returns String.ValueOf(getActualValue())
.
protected final StandardSubjectBuilder check()
protected final StandardSubjectBuilder check(String format, Object... args)
Derived subjects retain the FailureStrategy
and messages of the current subject, and in some cases, they
automatically supplement their failure message with information about the original subject.
For example, ThrowableSubject.hasMessageThat()
, which returns a StringSubject
,
is implemented with check("getMessage()").that(actual().getMessage())
.
The arguments to check
describe how the new subject was derived from the old,
formatted like a chained method call. This allows Truth to include that information in its
failure messages. For example, assertThat(caught).hasCauseThat().hasMessageThat()
will
produce a failure message that includes the string "throwable.getCause().getMessage()," thanks
to internal check
calls that supplied "getCause()" and "getMessage()" as arguments.
If the method you're delegating to accepts parameters, you can pass check
a format
string. For example, MultimapSubject.valuesForKey(java.lang.Object)
calls check("valuesForKey(%s)", key)
.
If you aren't really delegating to an instance method on the actual value -- maybe you're
calling a static method, or you're calling a chain of several methods -- you can supply
whatever string will be most useful to users. For example, if you're delegating to getOnlyElement(actual().colors())
, you might call check("onlyColor()")
.
format
- a template with %s
placeholdersargs
- the arguments to be inserted into those placeholdersprotected final StandardSubjectBuilder ignoreCheck()
check()
but have already reported a failure. In
such cases it may still be necessary to return a Subject
instance even though any
subsequent assertions are meaningless. For example, if a user chains together more ThrowableSubject.hasCauseThat()
calls than the actual exception has causes, hasCauseThat
returns ignoreCheck().that(... a dummy exception ...)
.protected final void failWithActual(String key, @NullableDecl Object value)
This is the simplest failure API. For more advanced needs, see the other overload and failWithoutActual
.
Example usage: The check contains(String)
calls failWithActual("expected to
contain", string)
.
protected final void failWithActual(Fact first, Fact... rest)
If you have only one fact to report (and it's a key-value fact), prefer the simpler overload.
Example usage: The check isEmpty()
calls failWithActual(simpleFact("expected
to be empty"))
.
@Deprecated protected final void fail(String check)
Fact
-style methods, typically by using failWithActual
(
simpleFact(...)
)
. However, if you want to preserve your exact failure message as a
migration aid, you can inline this method (and then inline the resulting method call, as
well).check
- the check being asserted@Deprecated protected final void fail(String verb, Object other)
Fact
-style methods, typically by using failWithActual(String, Object)
. However, if you want to preserve your exact failure
message as a migration aid, you can inline this method (and then inline the resulting
method call, as well).verb
- the check being assertedother
- the value against which the subject is compared@Deprecated protected final void fail(String verb, Object... messageParts)
Fact
-style methods, typically by using failWithActual(Fact, Fact...)
. However, if you want to preserve your exact failure
message as a migration aid, you can inline this method.verb
- the check being assertedmessageParts
- the expectations against which the subject is compared@Deprecated protected final void failWithBadResults(String verb, Object expected, String failVerb, Object actual)
Fact
-style methods, typically by using failWithActual(Fact, Fact...)
. However, if you want to preserve your exact failure
message as a migration aid, you can inline this method.verb
- the check being assertedexpected
- the expectations against which the subject is comparedfailVerb
- the failure of the check being assertedactual
- the actual value the subject was compared against@Deprecated protected final void failWithCustomSubject(String verb, Object expected, Object actual)
Fact
-style methods, typically by using failWithoutActual(Fact, Fact...)
. However, if you want to preserve your exact failure
message as a migration aid, you can inline this method.verb
- the check being assertedexpected
- the expected value of the checkactual
- the custom representation of the subject to be reported in the failure.@Deprecated protected final void failWithoutSubject(String check)
Fact
-style methods, typically by using failWithoutActual
(
simpleFact(...)
)
. However, if you want to preserve your exact failure message as a
migration aid, you can inline this method.protected final void failWithoutActual(Fact first, Fact... rest)
Most failure messages should report the actual value, so most checks should call failWithAcutal
instead. However, failWithoutActual
is
useful in some cases:
isNotEmpty()
calls failWithoutActual(simpleFact("expected not to be empty")
.
isNotWithin(...).of(...)
calls failWithoutActual
so that it can put the expected and actual values together, followed
by the tolerance.
Example usage: The check isEmpty()
calls failWithActual(simpleFact("expected
to be empty"))
.
@Deprecated protected final void failWithoutActual(String check)
Fact
-style methods, typically by using failWithoutActual
(
simpleFact(...)
)
. However, if you want to preserve your exact failure message as a
migration aid, you can inline this method (and then inline the resulting method call, as
well).check
- the check being asserted@Deprecated protected void failWithRawMessage(String message, Object... parameters)
Fact
-style methods, typically by using failWithActual(Fact, Fact...)
. However, if you want to preserve your exact failure
message as a migration aid, you can inline this method.Subject
subclasses which need to
provide alternate language for more fit-to-purpose error messages.message
- a template with %s
placeholdersparameters
- the object parameters which will be applied to the message template.@Deprecated protected final void failWithRawMessageAndCause(String message, Throwable cause)
failWithRawMessage(java.lang.String, java.lang.Object...)
, but also, to make Truth attach the
Throwable cause
to the assertion failure, ensure that cause
appears in the
assertion chain. This should typically already be the case, as in assertThat(throwable).hasMessageThat().isEqualTo("message")
. If it is not, you will need
to introduce an extra check
call into your chain, something like check().about(unexpectedFailures()).that(cause).wasASuccess(message)
, with unexpectedFailures()
returning a factory for a "dummy" subject type that defines only a
wasASuccess
method that fails unconditionally.@Deprecated protected final void failComparing(String message, CharSequence expected, CharSequence actual)
check("foo()").that(actual().foo()).isEqualTo(expectedFoo)
. (If you are comparing the
values using a method other than Object.equals(java.lang.Object)
, you'll have to wrap your actual and
expected values in a custom object that implements your desired logic in equals
and
forwards the toString
method.)FailureStrategy
may use to construct a ComparisonFailure
.@Deprecated protected final void failComparing(String message, CharSequence expected, CharSequence actual, Throwable cause)
ComparisonFailure
. To make Truth also attach the Throwable cause
to the assertion
failure, see the instructions on failWithRawMessageAndCause(java.lang.String, java.lang.Throwable)
.FailureStrategy
may use to construct a ComparisonFailure
.@Deprecated public final boolean equals(@NullableDecl Object o)
Object.equals(Object)
is not supported on Truth subjects. If you meant to
test object equality between an expected and the actual value, use isEqualTo(Object)
instead.equals
in class Object
UnsupportedOperationException
- always@Deprecated public final int hashCode()
Object.hashCode()
is not supported on Truth subjects.hashCode
in class Object
UnsupportedOperationException
- alwaysCopyright © 2018. All rights reserved.