Class FluentObjectAssertion<T,R>
- Type Parameters:
T
- The object type.R
- The return type.
- Direct Known Subclasses:
FluentAnyAssertion
,FluentArrayAssertion
,FluentBeanAssertion
,FluentCollectionAssertion
,FluentComparableAssertion
,FluentMapAssertion
,FluentPrimitiveArrayAssertion
,FluentProtocolVersionAssertion
,FluentRequestContentAssertion
,FluentRequestLineAssertion
,FluentResponseBodyAssertion
,FluentResponseStatusLineAssertion
,FluentStringAssertion
,FluentThrowableAssertion
,ObjectAssertion
Test Methods:
Transform Methods:
Configuration Methods:
See Also:
-
Constructor Summary
ConstructorDescriptionFluentObjectAssertion
(Assertion creator, T value, R returns) Chained constructor.FluentObjectAssertion
(T value, R returns) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionasAny()
Converts this assertion into anFluentAnyAssertion
so that it can be converted to other assertion types.asJson()
Converts this object to simplified JSON and returns it as a new assertion.Converts this object to sorted simplified JSON and returns it as a new assertion.asString()
Converts this object to a string usingObject.toString()
and returns it as a new assertion.Converts this object to a string using the specified function and returns it as a new assertion.Converts this object to text using the specified serializer and returns it as a new assertion.asTransformed
(Function<T, T> function) Applies a transform on the inner object and returns a new inner object.<T2> FluentObjectAssertion<T2,
R> asTransformedTo
(Function<T, T2> function) Applies a transform on the inner object and returns a new inner object.protected boolean
Checks two objects for equality.protected String
getFailureMessage
(Predicate<?> p, Object value) Returns the predicate failure message.Asserts that the value passes the specified predicate test.Asserts that the value equals the specified value.Asserts that the value is one of the specified values.isExactType
(Class<?> type) Asserts that the object is an instance of the specified class.isExists()
Asserts that the object is not null.Converts this object to simplified JSON and runs theFluentStringAssertion.is(String)
on the result.Asserts that the value does not equal the specified value.Asserts that the value is not one of the specified values.Asserts that the object is not null.isNull()
Asserts that the object i null.Asserts that the specified object is the same object as this object.Verifies that two objects are equivalent after converting them both to JSON.isSameSerializedAs
(Object o, WriterSerializer serializer) Asserts that the specified object is the same as this object after converting both to strings using the specified serializer.Verifies that two objects are equivalent after converting them both to sorted JSON.Asserts that the value converted to a string equals the specified value.Asserts that the object is an instance of the specified class.protected <T2> Optional<T2>
Returns the result of running the specified function against the value and returns the result.opt()
Returns the value wrapped in anOptional
.protected T
Returns the inner value or the other value if the value isnull .Allows you to override the assertion failure message.setOut
(PrintStream value) If an error occurs, send the error message to the specified stream instead of STDERR.Suppresses output to STDERR.If an error occurs, send the error message to STDOUT instead of STDERR.setThrowable
(Class<? extends RuntimeException> value) If an error occurs, throw this exception instead of the standardAssertionError
.toString()
Returns the string form of the inner object.protected T
value()
Returns the inner value after asserting it is notnull .protected String
Returns the inner value as a string.protected boolean
Returnstrue if the inner value is not null.protected boolean
Returnstrue if the inner value is null.Methods inherited from class org.apache.juneau.assertions.FluentAssertion
returns
Methods inherited from class org.apache.juneau.assertions.Assertion
arrayClass, className, error, error
-
Constructor Details
-
FluentObjectAssertion
Constructor.- Parameters:
value
- The object being tested.
Can benull .returns
- The object to return after a test method is called.
Ifnull , the test method returns this object allowing multiple test method calls to be used on the same assertion.
-
FluentObjectAssertion
Chained constructor.Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
- Parameters:
creator
- The assertion that created this assertion.
Should benull if this is the top-level assertion.value
- The object being tested.
Can benull .returns
- The object to return after a test method is called.
Ifnull , the test method returns this object allowing multiple test method calls to be used on the same assertion.
-
-
Method Details
-
asString
Converts this object to a string usingObject.toString()
and returns it as a new assertion.Example:
// Validates that the specified object is "foobar" after converting to a string. assertObject (myPojo ) .asString() .is("foobar" );- Returns:
- A new fluent string assertion.
-
asString
Converts this object to text using the specified serializer and returns it as a new assertion.Example:
// Validates that the specified object is an instance of MyBean. assertObject (myPojo ) .asString(XmlSerializer.DEFAULT ) .is("<object><foo>bar</foo><baz>qux</baz></object>" );- Parameters:
ws
- The serializer to use to convert the object to text.- Returns:
- A new fluent string assertion.
-
asString
Converts this object to a string using the specified function and returns it as a new assertion.Example:
// Validates that the specified object is "foobar" after converting to a string. assertObject (myPojo ) .asString(x ->x .toString()) .is("foobar" );- Parameters:
function
- The conversion function.- Returns:
- A new fluent string assertion.
-
asJson
Converts this object to simplified JSON and returns it as a new assertion.Example:
// Validates that the specified object is an instance of MyBean. assertObject (myPojo ) .asJson() .is("{foo:'bar',baz:'qux'}" );- Returns:
- A new fluent string assertion.
-
asJsonSorted
Converts this object to sorted simplified JSON and returns it as a new assertion.Example:
// Validates that the specified object is an instance of MyBean. assertObject (myPojo ) .asJsonSorted() .is("{baz:'qux',foo:'bar'}" );- Returns:
- A new fluent string assertion.
-
asTransformed
Applies a transform on the inner object and returns a new inner object.- Parameters:
function
- The function to apply.- Returns:
- This object.
-
asTransformedTo
Applies a transform on the inner object and returns a new inner object.- Type Parameters:
T2
- The transform-to type.- Parameters:
function
- The function to apply.- Returns:
- This object.
-
asAny
Converts this assertion into anFluentAnyAssertion
so that it can be converted to other assertion types.- Returns:
- This object.
-
isExists
Asserts that the object is not null.Equivalent to
isNotNull()
.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
isNull
Asserts that the object i null.Equivalent to
isNotNull()
.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
isNotNull
Asserts that the object is not null.Equivalent to
isNotNull()
.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
is
Asserts that the value equals the specified value.- Parameters:
value
- The value to check against.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
isString
Asserts that the value converted to a string equals the specified value.- Parameters:
value
- The value to check against.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
isNot
Asserts that the value does not equal the specified value.- Parameters:
value
- The value to check against.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
isAny
Asserts that the value is one of the specified values.- Parameters:
values
- The values to check against.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
isNotAny
Asserts that the value is not one of the specified values.- Parameters:
values
- The values to check against.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
isSame
Asserts that the specified object is the same object as this object.- Parameters:
value
- The value to check against.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
isSameJsonAs
Verifies that two objects are equivalent after converting them both to JSON.- Parameters:
o
- The object to compare against.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
isSameSortedJsonAs
Verifies that two objects are equivalent after converting them both to sorted JSON.Properties, maps, and collections are all sorted on both objects before comparison.
- Parameters:
o
- The object to compare against.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
isSameSerializedAs
Asserts that the specified object is the same as this object after converting both to strings using the specified serializer.- Parameters:
o
- The object to compare against.serializer
- The serializer to use to serialize this object.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
isType
Asserts that the object is an instance of the specified class.Example:
// Validates that the specified object is an instance of MyBean. assertObject (myPojo ).isType(MyBean.class );- Parameters:
parent
- The value to check against.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
isExactType
Asserts that the object is an instance of the specified class.Example:
// Validates that the specified object is an instance of MyBean. assertObject (myPojo ).isExactType(MyBean.class );- Parameters:
type
- The value to check against.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
is
Asserts that the value passes the specified predicate test.- Parameters:
test
- The predicate to use to test the value.- Returns:
- The fluent return object.
- Throws:
AssertionError
- If assertion failed.
-
isJson
Converts this object to simplified JSON and runs theFluentStringAssertion.is(String)
on the result.Example:
// Validates that the specified object is an instance of MyBean. assertObject (myPojo ) .asJson() .is("{foo:'bar',baz:'qux'}" );- Parameters:
value
- The expected string value.- Returns:
- The fluent return object.
-
setMsg
Description copied from class:Assertion
Allows you to override the assertion failure message.String can contain
"{msg}" to represent the original message.Example:
import static org.apache.juneau.assertions.Assertions.*;// Throws an assertion with a custom message instead of the default "Value was null." assertString (myString ) .setMsg("My string was bad: {msg}" ) .isNotNull();- Overrides:
setMsg
in classFluentAssertion<R>
- Parameters:
msg
- The assertion failure message.args
- Optional message arguments.- Returns:
- This object.
-
setOut
Description copied from class:Assertion
If an error occurs, send the error message to the specified stream instead of STDERR.- Overrides:
setOut
in classFluentAssertion<R>
- Parameters:
value
- The output stream. Can benull to suppress output.- Returns:
- This object.
-
setSilent
Description copied from class:Assertion
Suppresses output to STDERR.This is the equivalent to calling
out( .null )- Overrides:
setSilent
in classFluentAssertion<R>
- Returns:
- This object.
-
setStdOut
Description copied from class:Assertion
If an error occurs, send the error message to STDOUT instead of STDERR.- Overrides:
setStdOut
in classFluentAssertion<R>
- Returns:
- This object.
-
setThrowable
Description copied from class:Assertion
If an error occurs, throw this exception instead of the standardAssertionError
.The throwable class must have a public constructor that takes in any of the following parameters:
If the throwable cannot be instantiated, a
RuntimeException
is thrown instead.Example:
import static org.apache.juneau.assertions.Assertions.*;// Throws a BadRequest instead of an AssertionError if the string is null. assertString (myString ) .setThrowable(BadRequest.class ) .isNotNull();- Overrides:
setThrowable
in classFluentAssertion<R>
- Parameters:
value
- The new value for this setting.- Returns:
- This object.
-
value
Returns the inner value after asserting it is notnull .- Returns:
- The inner value.
- Throws:
AssertionError
- If inner value wasnull .
-
valueAsString
Returns the inner value as a string.- Returns:
- The inner value as a string, or
null if the value was null.
-
orElse
Returns the inner value or the other value if the value isnull .- Parameters:
other
- The other value.- Returns:
- The inner value.
-
valueIsNull
Returnstrue if the inner value is null.- Returns:
true if the inner value is null.
-
valueIsNotNull
Returnstrue if the inner value is not null.- Returns:
true if the inner value is not null.
-
opt
Returns the value wrapped in anOptional
.- Returns:
- The value wrapped in an
Optional
.
-
map
Returns the result of running the specified function against the value and returns the result.- Type Parameters:
T2
- The mapper-to type.- Parameters:
mapper
- The function to run against the value.- Returns:
- The result, never
null .
-
getFailureMessage
Returns the predicate failure message.If the predicate extends from
AssertionPredicate
, then the message comes fromAssertionPredicate.getFailureMessage()
. Otherwise, returns a generic"Unexpected value: x" message.- Parameters:
p
- The function to run against the value.value
- The value that failed the test.- Returns:
- The result, never
null .
-
equals
Checks two objects for equality.- Parameters:
o1
- The first object.o2
- The second object.- Returns:
true if the objects are equal.
-
toString
Returns the string form of the inner object. Subclasses can override this method to affect theasString()
method (and related).
-