Class Assertions
Provides assertions for various common POJO types.
Example:
Provides simple testing that Throwables
are being thrown correctly.
Example:
Provides other assertion convenience methods such as asserting non-null method arguments.
Example:
See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic final <T> AnyAssertion<T>
assertAny
(T value) Performs an assertion on an arbitrary POJO.static final <E> ArrayAssertion<E>
assertArray
(E[] value) Performs an assertion on an array of POJOs.static final <T> BeanAssertion<T>
assertBean
(T value) Performs an assertion on a Java bean.static final <E> BeanListAssertion<E>
assertBeanList
(List<E> value) Performs an assertion on a list of Java beans.static final BooleanAssertion
assertBoolean
(Boolean value) Performs an assertion on a Boolean.static final PrimitiveArrayAssertion<Boolean,
boolean[]> assertBooleanArray
(boolean[] value) Performs an assertion on a boolean array.static final PrimitiveArrayAssertion<Byte,
byte[]> assertByteArray
(byte[] value) Performs an assertion on a byte array.static final ByteArrayAssertion
assertBytes
(byte[] value) Performs an assertion on a byte array.static final ByteArrayAssertion
assertBytes
(InputStream value) Performs an assertion on the contents of an input stream.static final PrimitiveArrayAssertion<Character,
char[]> assertCharArray
(char[] value) Performs an assertion on a char array.static final <E> CollectionAssertion<E>
assertCollection
(Collection<E> value) Performs an assertion on a collection of POJOs.static final <T extends Comparable<T>>
ComparableAssertion<T>assertComparable
(T value) Performs an assertion on a Comparable.static final DateAssertion
assertDate
(Date value) Performs an assertion on a Date.static final PrimitiveArrayAssertion<Double,
double[]> assertDoubleArray
(double[] value) Performs an assertion on a double array.static final PrimitiveArrayAssertion<Float,
float[]> assertFloatArray
(float[] value) Performs an assertion on a float array.static final PrimitiveArrayAssertion<Integer,
int[]> assertIntArray
(int[] value) Performs an assertion on an int array.static final IntegerAssertion
assertInteger
(Integer value) Performs an assertion on an Integer.static final <E> ListAssertion<E>
assertList
(List<E> value) Performs an assertion on a list of POJOs.static final <E> ListAssertion<E>
assertList
(Stream<E> value) Performs an assertion on a stream of POJOs.static final LongAssertion
assertLong
(Long value) Performs an assertion on a Long.static final PrimitiveArrayAssertion<Long,
long[]> assertLongArray
(long[] value) Performs an assertion on a long array.static final <K,
V> MapAssertion<K, V> Performs an assertion on a map.static final <T> ObjectAssertion<T>
assertObject
(T value) Performs an assertion on a Java Object.static final <T> AnyAssertion<T>
assertOptional
(Optional<T> value) Performs an assertion on a Java Object wrapped in an Optional.static final StringAssertion
assertReader
(Reader value) Performs an assertion on the contents of a Reader.static final PrimitiveArrayAssertion<Short,
short[]> assertShortArray
(short[] value) Performs an assertion on a short array.static final StringAssertion
assertString
(Object value) Performs an assertion on a String.static final StringListAssertion
assertStringList
(List<String> value) Performs an assertion on a list of Strings.static final <T extends Throwable>
ThrowableAssertion<T>assertThrowable
(T value) Performs an assertion on a Throwable.static final ThrowableAssertion<Throwable>
assertThrown
(Snippet snippet) Executes an arbitrary snippet of code and captures anything thrown from it as a Throwable assertion.static final VersionAssertion
assertVersion
(Version value) Performs an assertion on a Version.static final ZonedDateTimeAssertion
assertZonedDateTime
(ZonedDateTime value) Performs an assertion on a ZonedDateTime.
-
Constructor Details
-
Assertions
public Assertions()
-
-
Method Details
-
assertAny
Performs an assertion on an arbitrary POJO.The distinction between
ObjectAssertion
andAnyAssertion
is that the latter supports all the operations of the former, but adds various transform methods for conversion to specific assertion types.Various transform methods such as
FluentListAssertion.asItem(int)
andFluentBeanAssertion.asProperty(String)
return generic any-assertions so that they can be easily transformed into other assertion types.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that the property 'foo' of a bean is 'bar'. assertAny (myPojo )// Start with AnyAssertion. .asBean(MyBean.class )// Transform to BeanAssertion. .property("foo" ).is("bar" );See Fluent Assertions for general assertion usage and
AnyAssertion
for supported operations on this type.- Type Parameters:
T
- The value type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertArray
Performs an assertion on an array of POJOs.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that an Integer array contains [1,2,3]. Integer[]array = {...};assertArray (array ) .asJson().is("[1,2,3]" );See Fluent Assertions for general assertion usage and
ArrayAssertion
for supported operations on this type.- Type Parameters:
E
- The value element type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertBean
Performs an assertion on a Java bean.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that the 'foo' and 'bar' properties of a bean are 1 and 2 respectively. assertBean (myBean ) .isType(MyBean.class ) .extract("foo,bar" ) .asJson().is("{foo:1,bar:2}" );See Fluent Assertions for general assertion usage and
BeanAssertion
for supported operations on this type.- Type Parameters:
T
- The value type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertBeanList
Performs an assertion on a list of Java beans.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that a bean list has 3 entries with 'foo' property values of 'bar','baz','qux'. assertBeanList (myListOfBeans ) .isSize(3) .property("foo" ) .is("bar" ,"baz" ,"qux" );See Fluent Assertions for general assertion usage and
BeanListAssertion
for supported operations on this type.- Type Parameters:
E
- The element type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertBoolean
Performs an assertion on a Boolean.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that a Boolean is not null and TRUE. assertBoolean (myBoolean ) .isTrue();See Fluent Assertions for general assertion usage and
BooleanAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertBooleanArray
Performs an assertion on a boolean array.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that a Boolean array has size of 3 and all entries are TRUE. assertBooleanArray (myBooleanArray ) .isSize(3) .all(x ->x ==true );See Fluent Assertions for general assertion usage and
PrimitiveArrayAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertByteArray
Performs an assertion on a byte array.The distinction between
assertByteArray(byte[])
andassertBytes(byte[])
is that the former returns an assertion more tied to general byte arrays and the latter returns an assertion more tied to dealing with binary streams that can be decoded or transformed into a string.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that a byte array has size of 3 and all bytes are larger than 10. assertByteArray (myByteArray ) .isSize(3) .all(x ->x > 10);See Fluent Assertions for general assertion usage and
PrimitiveArrayAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertBytes
Performs an assertion on a byte array.The distinction between
assertByteArray(byte[])
andassertBytes(byte[])
is that the former returns an assertion more tied to general byte arrays and the latter returns an assertion more tied to dealing with binary streams that can be decoded or transformed into a string.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that the byte array contains the string "foo". assertBytes (myBytes ) .asHex().is("666F6F" );See Fluent Assertions for general assertion usage and
ByteArrayAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertBytes
Performs an assertion on the contents of an input stream.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that the stream contains the string "foo". assertBytes (myStream ) .asHex().is("666F6F" );See Fluent Assertions for general assertion usage and
ByteArrayAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .
Stream is automatically closed.- Returns:
- A new assertion object.
Nevernull . - Throws:
IOException
- If thrown while reading contents from stream.
-
assertCharArray
Performs an assertion on a char array.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that the char array contains the string "foo". assertCharArray (myCharArray ) .asString().is("foo" );See Fluent Assertions for general assertion usage and
PrimitiveArrayAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertCollection
Performs an assertion on a collection of POJOs.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that a collection of strings has only one entry of 'foo'. assertCollection (myCollectionOfStrings ) .isSize(1) .contains("foo" );In general, use
assertList(List)
if you're performing an assertion on a list sinceListAssertion
provides more functionality thanCollectionAssertion
.See Fluent Assertions for general assertion usage and
CollectionAssertion
for supported operations on this type.- Type Parameters:
E
- The element type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertComparable
Performs an assertion on a Comparable.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts a comparable is less than another comparable. assertComparable (myComparable ) .isLt(anotherComparable );See Fluent Assertions for general assertion usage and
ComparableAssertion
for supported operations on this type.- Type Parameters:
T
- The value type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertDate
Performs an assertion on a Date.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts the specified date is after the current date. assertDate (myDate ) .isAfterNow();See Fluent Assertions for general assertion usage and
DateAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertDoubleArray
Performs an assertion on a double array.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that a double array is at least size 100 and all values are greater than 1000. assertDoubleArray (myDoubleArray ) .size().isGte(100f) .all(x ->x > 1000f);See Fluent Assertions for general assertion usage and
PrimitiveArrayAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertFloatArray
Performs an assertion on a float array.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that a float array is at least size 100 and all values are greater than 1000. assertFloatArray (myFloatArray ) .size().isGte(100f) .all(x ->x > 1000f);See Fluent Assertions for general assertion usage and
PrimitiveArrayAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertIntArray
Performs an assertion on an int array.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that a double array is at least size 100 and all values are greater than 1000. assertIntArray (myIntArray ) .size().isGte(100) .all(x ->x > 1000);See Fluent Assertions for general assertion usage and
PrimitiveArrayAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertInteger
Performs an assertion on an Integer.Example:
import static org.apache.juneau.assertions.Assertions.*;// Assert that an HTTP response status code is 200 or 404. assertInteger (httpReponse ) .isAny(200,404);See Fluent Assertions for general assertion usage and
IntegerAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertList
Performs an assertion on a list of POJOs.Example:
import static org.apache.juneau.assertions.Assertions.*;// Assert that the first entry in a list is "{foo:'bar'}" when serialized to simplified JSON. assertList (myList ) .item(0) .asJson().is("{foo:'bar'}" );See Fluent Assertions for general assertion usage and
ListAssertion
for supported operations on this type.- Type Parameters:
E
- The element type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertList
Performs an assertion on a stream of POJOs.Example:
import static org.apache.juneau.assertions.Assertions.*;// Assert that the first entry in a list is "{foo:'bar'}" when serialized to simplified JSON. assertList (myStream ) .item(0) .asJson().is("{foo:'bar'}" );See Fluent Assertions for general assertion usage and
ListAssertion
for supported operations on this type.- Type Parameters:
E
- The element type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertLong
Performs an assertion on a Long.Example:
import static org.apache.juneau.assertions.Assertions.*;// Throw a BadReqest if an HTTP response length is greater than 100k. assertLong (responseLength ) .throwable(BadRequest.class ) .msg("Request is too large" ) .isLt(100000);See Fluent Assertions for general assertion usage and
LongAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertLongArray
Performs an assertion on a long array.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that a long array is at least size 100 and all values are greater than 1000. assertLongArray (myLongArray ) .size().isGte(100) .all(x ->x > 1000);See Fluent Assertions for general assertion usage and
PrimitiveArrayAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertMap
Performs an assertion on a map.Example:
import static org.apache.juneau.assertions.Assertions.*;// Assert the specified map is a HashMap and contains the key "foo". assertMap (myMap ) .isType(HashMap.class ) .containsKey("foo" );See Fluent Assertions for general assertion usage and
MapAssertion
for supported operations on this type.- Type Parameters:
K
- The key type.V
- The value type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertObject
Performs an assertion on a Java Object.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts the specified POJO is of type MyBean and is "{foo:'bar'}" // when serialized to Simplified JSON. assertObject (myPojo ) .isType(MyBean.class ) .asJson().is("{foo:'bar'}" );See Fluent Assertions for general assertion usage and
ObjectAssertion
for supported operations on this type.- Type Parameters:
T
- The value type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertOptional
Performs an assertion on a Java Object wrapped in an Optional.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts the specified POJO is of type MyBean and is "{foo:'bar'}" // when serialized to Simplified JSON. assertOptional (opt ) .isType(MyBean.class ) .asJson().is("{foo:'bar'}" );See Fluent Assertions for general assertion usage and
AnyAssertion
for supported operations on this type.- Type Parameters:
T
- The value type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertReader
Performs an assertion on the contents of a Reader.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts the contents of the Reader contains "foo". assertReader (myReader ) .contains("foo" );See Fluent Assertions for general assertion usage and
StringAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .
Reader is automatically closed.- Returns:
- A new assertion object.
Nevernull . - Throws:
IOException
- If thrown while reading contents from reader.
-
assertShortArray
Performs an assertion on a short array.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that a float array is at least size 10 and all values are greater than 100. assertShortArray (myShortArray ) .size().isGte(10) .all(x ->x > 100);See Fluent Assertions for general assertion usage and
PrimitiveArrayAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertString
Performs an assertion on a String.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts a string is at least 100 characters long and contains "foo". assertString (myString ) .size().isGte(100) .contains("foo" );See Fluent Assertions for general assertion usage and
StringAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertStringList
Performs an assertion on a list of Strings.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts a list of strings contain "foo,bar,baz" after trimming all and joining. assertStringList (myListOfStrings ) .isSize(3) .trim() .join("," ) .is("foo,bar,baz" );See Fluent Assertions for general assertion usage and
StringListAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertThrowable
Performs an assertion on a Throwable.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts a throwable is a RuntimeException containing 'foobar' in the message. assertThrowable (throwable ) .isExactType(RuntimeException.class ) .message().contains("foobar" );See Fluent Assertions for general assertion usage and
ThrowableAssertion
for supported operations on this type.- Type Parameters:
T
- The value type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertVersion
Performs an assertion on a Version.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts the specified major version is at least 2. assertVersion (version ) .major().isGte(2);See Fluent Assertions for general assertion usage and
VersionAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertZonedDateTime
Performs an assertion on a ZonedDateTime.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts the specified date is after the current date. assertZonedDateTime (myZonedDateTime ) .isAfterNow();See Fluent Assertions for general assertion usage and
ZonedDateTimeAssertion
for supported operations on this type.- Parameters:
value
- The object being tested.
Can benull .- Returns:
- A new assertion object.
Nevernull .
-
assertThrown
Executes an arbitrary snippet of code and captures anything thrown from it as a Throwable assertion.Example:
import static org.apache.juneau.assertions.Assertions.*;// Asserts that the specified method throws a RuntimeException containing "foobar" in the message. assertThrown (()->foo .getBar()) .isType(RuntimeException.class ) .message().contains("foobar" );- Parameters:
snippet
- The snippet of code to execute.- Returns:
- A new assertion object. Never
null .
-