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
in Truth
fits into the assertion fluent chain, taking the place
of the "subject of the test". For instance, in assertThat("foo").isNotNull()
,
Truth#assertThat(String)
returns a StringSubject which wraps the actual value itself,
providing a hook for methods which test propositions about the actual value.
Custom sub-types of Subject provide type-appropriate methods which can then provide more suitable error messages than the traditional assertions may provide.
Modifier and Type | Field and Description |
---|---|
protected FailureStrategy |
failureStrategy |
Constructor and Description |
---|
Subject(FailureStrategy failureStrategy,
T actual) |
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 TestVerb |
check()
A convenience for implementers of
Subject subclasses to use other truth Subject
wrappers within their own propositional logic. |
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 proposition)
Reports a failure constructing a message from a simple verb.
|
protected void |
fail(String verb,
Object... messageParts)
Assembles a failure message and passes such to the FailureStrategy
|
protected void |
fail(String verb,
Object other)
Assembles a failure message and passes such to the FailureStrategy.
|
protected void |
failWithBadResults(String verb,
Object expected,
String failVerb,
Object actual)
Assembles a failure message and passes it to the FailureStrategy
|
protected void |
failWithCustomSubject(String verb,
Object expected,
Object actual)
Assembles a failure message with an alternative representation of the wrapped subject and
passes it to the FailureStrategy
|
protected void |
failWithoutActual(String proposition)
Assembles a failure message without a given subject and passes it to the FailureStrategy
|
protected void |
failWithoutSubject(String proposition)
Deprecated.
|
protected void |
failWithRawMessage(String message,
Object... parameters)
Passes through a failure message verbatim.
|
protected String |
getDisplaySubject()
Deprecated.
Prefer
#actualAsString() for display-formatted access to the subject. |
protected T |
getSubject()
Deprecated.
Prefer
#actual() for direct access to the subject. |
int |
hashCode()
Deprecated.
Object.hashCode() is not supported on Truth subjects. |
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 other)
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 other)
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 other)
Fails if the subject is the same instance as the given object.
|
void |
isNull()
Fails if the subject is not null.
|
void |
isSameAs(Object other)
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.
|
protected final FailureStrategy failureStrategy
public Subject(FailureStrategy failureStrategy, @Nullable T actual)
protected String internalCustomName()
named(String, Object...)
.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, similar to String.format(String, Object...)
, the chief difference being
that extra parameters (for which there are no template variables) will be appended to the
resulting string in brackets. Additionally, this only supports the %s
template variable
type.
public void isNull()
public void isNotNull()
public void isEqualTo(@Nullable Object other)
Objects.equal(java.lang.Object, java.lang.Object)
Arrays#equals
overload
Byte
, Short
, Character
, Integer
, or Long
) and they are numerically equal when converted to Long
.
public void isNotEqualTo(@Nullable Object other)
isEqualTo(java.lang.Object)
method.public void isSameAs(@Nullable Object other)
public void isNotSameAs(@Nullable Object other)
public void isInstanceOf(Class<?> clazz)
public void isNotInstanceOf(Class<?> clazz)
public void isIn(Iterable<?> iterable)
public void isAnyOf(@Nullable Object first, @Nullable Object second, @Nullable Object... rest)
public void isNotIn(Iterable<?> iterable)
public void isNoneOf(@Nullable Object first, @Nullable Object second, @Nullable Object... rest)
@Deprecated protected T getSubject()
#actual()
for direct access to the subject.protected final T actual()
@Deprecated protected String getDisplaySubject()
#actualAsString()
for display-formatted access to the subject.protected final String actualAsString()
protected String actualCustomStringRepresentation()
Subjects should override this with care.
By default, this returns String.ValueOf(getActualValue())
.
protected TestVerb check()
Subject
subclasses to use other truth Subject
wrappers within their own propositional logic.protected final void fail(String proposition)
proposition
- the proposition being assertedprotected final void fail(String verb, Object other)
other
have the same toString()'s.verb
- the proposition being assertedother
- the value against which the subject is comparedprotected final void fail(String verb, Object... messageParts)
verb
- the proposition being assertedmessageParts
- the expectations against which the subject is comparedprotected final void failWithBadResults(String verb, Object expected, String failVerb, Object actual)
verb
- the proposition being assertedexpected
- the expectations against which the subject is comparedfailVerb
- the failure of the proposition being assertedactual
- the actual value the subject was compared againstprotected final void failWithCustomSubject(String verb, Object expected, Object actual)
verb
- the proposition being assertedexpected
- the expected value of the propositionactual
- the custom representation of the subject to be reported in the failure.@Deprecated protected final void failWithoutSubject(String proposition)
failWithoutActual(String)
protected final void failWithoutActual(String proposition)
proposition
- the proposition being assertedprotected void failWithRawMessage(String message, Object... parameters)
Subject
subclasses which need to
provide alternate language for more fit-to-purpose error messages.message
- the message template to be passed to the failure. Note, this method only
guarantees to process %s
tokens. It is not guaranteed to be compatible with String.format()
. Any other formatting desired (such as floats or scientific notation)
should be performed before the method call and the formatted value passed in as a string.parameters
- the object parameters which will be applied to the message template.@Deprecated public final boolean equals(@Nullable 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 © 2017. All rights reserved.