S
- deprecated - the self-type, allowing this
-returning methods to avoid
needing subclassing. Both type parameters will be removed, as the methods that need them
are being removed. You can prepare for this change by editing your class to refer to raw
Subject
today.T
- deprecated - the type of the object being tested by this Subject
.
Both type parameters will be removed, as the methods that need them are being removed. You
can prepare for this change by editing your class to refer to raw Subject
today.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,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()
Deprecated.
Instead, declare a private field to store the actual value inside your
Subject subclass. We will be releasing a tool to automate most migrations. |
protected String |
actualAsString()
Deprecated.
Now that
named(java.lang.String, java.lang.Object...) is being removed, this method simply returns actualCustomStringRepresentation() , surrounded by angle brackets (and we discourage angle
brackets in the new key-value style of failure messages). Most callers can use the actual
value directly instead. If they format it in actualCustomStringRepresentation() ,
they may wish to apply similar formatting here. |
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(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 |
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 |
failWithoutActual(Fact first,
Fact... rest)
Fails, reporting a message with the given facts, without automatically adding the actual
value.
|
protected T |
getSubject()
Deprecated.
Instead, declare a private field to store the actual value inside your
Subject subclass. We will be releasing a tool to automate most migrations. |
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()
Deprecated.
This method will be removed when
named(java.lang.String, java.lang.Object...) is. |
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 |
isNotSameInstanceAs(Object unexpected)
Fails if the subject is the same instance as the given object.
|
void |
isNull()
Fails if the subject is not null.
|
void |
isSameInstanceAs(Object expected)
Fails if the subject is not the same instance as the given object.
|
S |
named(String format,
Object... args)
Deprecated.
Instead of
assertThat(foo).named("foo") , use assertWithMessage("foo").that(foo) . For custom subjects, use assertWithMessage("foo").about(foos()).that(foo) . For other scenarios, see this FAQ entry about adding
messages. We will be releasing a tool to automate most migrations. |
String |
toString() |
protected Subject(FailureMetadata metadata, @NullableDecl T actual)
check()
.that(actual)
.@Deprecated protected String internalCustomName()
named(java.lang.String, java.lang.Object...)
is.named(String, Object...)
.@CanIgnoreReturnValue @Deprecated public S named(String format, Object... args)
assertThat(foo).named("foo")
, use assertWithMessage("foo").that(foo)
. For custom subjects, use assertWithMessage("foo").about(foos()).that(foo)
. For other scenarios, see this FAQ entry about adding
messages. We will be releasing a tool to automate most migrations.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
.
Note: This method does not test the Object.equals(java.lang.Object)
implementation itself; it
assumes that method is functioning correctly according to its contract. Testing an
equals
implementation requires a utility such as guava-testlib's EqualsTester.
public void isNotEqualTo(@NullableDecl Object unexpected)
isEqualTo(java.lang.Object)
method.public final void isSameInstanceAs(@NullableDecl @CompatibleWith(value="T") Object expected)
public final void isNotSameInstanceAs(@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()
Subject
subclass. We will be releasing a tool to automate most migrations.@Deprecated protected final T actual()
Subject
subclass. We will be releasing a tool to automate most migrations.@Deprecated protected final String actualAsString()
named(java.lang.String, java.lang.Object...)
is being removed, this method simply returns actualCustomStringRepresentation()
, surrounded by angle brackets (and we discourage angle
brackets in the new key-value style of failure messages). Most callers can use the actual
value directly instead. If they format it in actualCustomStringRepresentation()
,
they may wish to apply similar formatting here.@ForOverride protected String actualCustomStringRepresentation()
Subjects should override this with care.
By default, this returns String.ValueOf(getActualValue())
.
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"))
.
protected final void failWithoutActual(Fact first, Fact... rest)
Most failure messages should report the actual value, so most checks should call failWithActual
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 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 © 2019. All rights reserved.