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 SummaryConstructors
- 
Method SummaryModifier 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 BooleanAssertionassertBoolean(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 ByteArrayAssertionassertBytes(byte[] value) Performs an assertion on a byte array.static final ByteArrayAssertionassertBytes(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 DateAssertionassertDate(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 IntegerAssertionassertInteger(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 LongAssertionassertLong(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 StringAssertionassertReader(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 StringAssertionassertString(Object value) Performs an assertion on a String.static final StringListAssertionassertStringList(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 VersionAssertionassertVersion(Version value) Performs an assertion on a Version.static final ZonedDateTimeAssertionassertZonedDateTime(ZonedDateTime value) Performs an assertion on a ZonedDateTime.
- 
Constructor Details- 
Assertionsprotected Assertions()Constructor.
 
- 
- 
Method Details- 
assertAnyPerforms an assertion on an arbitrary POJO.The distinction between ObjectAssertionandAnyAssertionis 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 Juneau Ecosystem Overview for general assertion usage and AnyAssertionfor supported operations on this type.- Type Parameters:
- T- The value type.
- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertArrayPerforms 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 Juneau Ecosystem Overview for general assertion usage and ArrayAssertionfor supported operations on this type.- Type Parameters:
- E- The value element type.
- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertBeanPerforms 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 Juneau Ecosystem Overview for general assertion usage and BeanAssertionfor supported operations on this type.- Type Parameters:
- T- The value type.
- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertBeanListPerforms 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 Juneau Ecosystem Overview for general assertion usage and BeanListAssertionfor supported operations on this type.- Type Parameters:
- E- The element type.
- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertBooleanPerforms 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 Juneau Ecosystem Overview for general assertion usage and BooleanAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertBooleanArrayPerforms 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 Juneau Ecosystem Overview for general assertion usage and PrimitiveArrayAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertByteArrayPerforms 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 Juneau Ecosystem Overview for general assertion usage and PrimitiveArrayAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertBytesPerforms 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 Juneau Ecosystem Overview for general assertion usage and ByteArrayAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertBytesPerforms 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 Juneau Ecosystem Overview for general assertion usage and ByteArrayAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
 Stream is automatically closed.
- Returns:
- A new assertion object.
   
 Nevernull .
- Throws:
- IOException- If thrown while reading contents from stream.
 
- 
assertCharArrayPerforms 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 Juneau Ecosystem Overview for general assertion usage and PrimitiveArrayAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertCollectionPerforms 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 sinceListAssertionprovides more functionality thanCollectionAssertion.See Juneau Ecosystem Overview for general assertion usage and CollectionAssertionfor supported operations on this type.- Type Parameters:
- E- The element type.
- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertComparablePerforms 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 Juneau Ecosystem Overview for general assertion usage and ComparableAssertionfor supported operations on this type.- Type Parameters:
- T- The value type.
- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertDatePerforms 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 Juneau Ecosystem Overview for general assertion usage and DateAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertDoubleArrayPerforms 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 Juneau Ecosystem Overview for general assertion usage and PrimitiveArrayAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertFloatArrayPerforms 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 Juneau Ecosystem Overview for general assertion usage and PrimitiveArrayAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertIntArrayPerforms 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 Juneau Ecosystem Overview for general assertion usage and PrimitiveArrayAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertIntegerPerforms 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 Juneau Ecosystem Overview for general assertion usage and IntegerAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertListPerforms 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 Juneau Ecosystem Overview for general assertion usage and ListAssertionfor supported operations on this type.- Type Parameters:
- E- The element type.
- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertListPerforms 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 Juneau Ecosystem Overview for general assertion usage and ListAssertionfor supported operations on this type.- Type Parameters:
- E- The element type.
- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertLongPerforms 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 Juneau Ecosystem Overview for general assertion usage and LongAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertLongArrayPerforms 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 Juneau Ecosystem Overview for general assertion usage and PrimitiveArrayAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertMapPerforms 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 Juneau Ecosystem Overview for general assertion usage and MapAssertionfor supported operations on this type.- Type Parameters:
- K- The key type.
- V- The value type.
- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertObjectPerforms 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 Juneau Ecosystem Overview for general assertion usage and ObjectAssertionfor supported operations on this type.- Type Parameters:
- T- The value type.
- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertOptionalPerforms 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 Juneau Ecosystem Overview for general assertion usage and AnyAssertionfor supported operations on this type.- Type Parameters:
- T- The value type.
- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertReaderPerforms 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 Juneau Ecosystem Overview for general assertion usage and StringAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
 Reader is automatically closed.
- Returns:
- A new assertion object.
   
 Nevernull .
- Throws:
- IOException- If thrown while reading contents from reader.
 
- 
assertShortArrayPerforms 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 Juneau Ecosystem Overview for general assertion usage and PrimitiveArrayAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertStringPerforms 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 Juneau Ecosystem Overview for general assertion usage and StringAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertStringListPerforms 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 Juneau Ecosystem Overview for general assertion usage and StringListAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertThrowablePerforms 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 Juneau Ecosystem Overview for general assertion usage and ThrowableAssertionfor supported operations on this type.- Type Parameters:
- T- The value type.
- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertVersionPerforms 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 Juneau Ecosystem Overview for general assertion usage and VersionAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertZonedDateTimePerforms 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 Juneau Ecosystem Overview for general assertion usage and ZonedDateTimeAssertionfor supported operations on this type.- Parameters:
- value- The object being tested.
 Can be- null .
- Returns:
- A new assertion object.
   
 Nevernull .
 
- 
assertThrownExecutes 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 .
 
 
-