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
ConstructorsConstructorDescriptionFluentObjectAssertion(Assertion creator, T value, R returns) Chained constructor.FluentObjectAssertion(T value, R returns) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionasAny()Converts this assertion into anFluentAnyAssertionso that it can be converted to other assertion types.asString()Converts this object to a string usingObject.toString()and returns it as a new assertion.asString(ThrowingFunction<? super T, String> function) Converts this object to a string using the specified function and returns it as a new assertion.asTransformed(UnaryOperator<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 booleanChecks two objects for equality.protected StringgetFailureMessage(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.final RAsserts 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.Asserts that the value does not equal the specified value.final RAsserts 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.isSameSerializedAs(Object o, ThrowingFunction<? super T, String> function) Asserts that the specified object is the same as this object after converting both to strings using the specified function.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 TReturns 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 Tvalue()Returns the inner value after asserting it is notnull .protected StringReturns the inner value as a string.protected booleanReturnstrue if the inner value is not null.protected booleanReturnstrue if the inner value is null.Methods inherited from class org.apache.juneau.bct.assertions.FluentAssertion
returnsMethods inherited from class org.apache.juneau.bct.assertions.Assertion
arrayClass, error, error
-
Constructor Details
-
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 (assertions such asisNull()then operate on the absence of a value).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
Constructor.- Parameters:
value- The object being tested.
Can benull (assertions such asisNull()then operate on the absence of a value).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
-
asAny
Converts this assertion into anFluentAnyAssertionso that it can be converted to other assertion types.- Returns:
- This object.
-
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 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. May throw a checked exception.- 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.
-
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.
-
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.
-
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.
-
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.
-
isExists
Asserts that the object is not null.Equivalent to
isNotNull().- 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.
-
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.
-
isNotNull
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.
-
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.
-
isSameSerializedAs
Asserts that the specified object is the same as this object after converting both to strings using the specified function.Example:
assertObject (myPojo ).isSameSerializedAs(other ,x ->x .toString());- Parameters:
o- The object to compare against.function- The function to use to serialize the objects. May throw a checked exception.- 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.
-
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.
-
setMsg
Description copied from class:AssertionAllows you to override the assertion failure message.String can contain
"{msg}" to represent the original message.Example:
import static org.apache.juneau.bct.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:
setMsgin classFluentAssertion<R>- Parameters:
msg- The assertion failure message.args- Optional message arguments.- Returns:
- This object.
-
setOut
Description copied from class:AssertionIf an error occurs, send the error message to the specified stream instead of STDERR.- Overrides:
setOutin classFluentAssertion<R>- Parameters:
value- The output stream. Can benull to suppress output.- Returns:
- This object.
-
setSilent
Description copied from class:AssertionSuppresses output to STDERR.This is the equivalent to calling
out( .null )- Overrides:
setSilentin classFluentAssertion<R>- Returns:
- This object.
-
setStdOut
Description copied from class:AssertionIf an error occurs, send the error message to STDOUT instead of STDERR.- Overrides:
setStdOutin classFluentAssertion<R>- Returns:
- This object.
-
setThrowable
Description copied from class:AssertionIf 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
RuntimeExceptionis thrown instead.Example:
import static org.apache.juneau.bct.assertions.Assertions.*;// Throws a BadRequest instead of an AssertionError if the string is null. assertString (myString ) .setThrowable(BadRequest.class ) .isNotNull();- Overrides:
setThrowablein classFluentAssertion<R>- Parameters:
value- The new value for this setting.- Returns:
- This object.
-
toString
Returns the string form of the inner object. Subclasses can override this method to affect theasString()method (and related). -
equals
Checks two objects for equality.- Parameters:
o1- The first object.o2- The second object.- Returns:
true if the objects are equal.
-
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 .
-
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 .
-
opt
Returns the value wrapped in anOptional.- Returns:
- The value wrapped in an
Optional.
-
orElse
Returns the inner value or the other value if the value isnull .- Parameters:
other- The other value.- Returns:
- The inner value.
-
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.
-
valueIsNotNull
Returnstrue if the inner value is not null.- Returns:
true if the inner value is not null.
-
valueIsNull
Returnstrue if the inner value is null.- Returns:
true if the inner value is null.
-