Class Assertions

java.lang.Object
org.apache.juneau.assertions.Assertions

public class Assertions extends Object
Main class for creation of assertions for stand-alone testing.

Provides assertions for various common POJO types.

Example:

import static org.apache.juneau.assertions.Assertions.*; // Assert string is greater than 100 characters and contains "foo". assertString(myString) .length().isGt(100) .contains("foo");

Provides simple testing that Throwables are being thrown correctly.

Example:

import static org.apache.juneau.assertions.Assertions.*; // Assert that calling doBadCall() causes a RuntimeException. assertThrown(() -> myPojo.doBadCall()) .isType(RuntimeException.class) .message().contains("Bad thing happened.");

Provides other assertion convenience methods such as asserting non-null method arguments.

Example:

import static org.apache.juneau.assertions.Assertions.*; public String getFoo(String bar) { assertArgNotNull("bar", bar); ... }

See Also:
  • Constructor Details

  • Method Details

    • assertAny

      public static final <T> AnyAssertion<T> assertAny(T value)
      Performs an assertion on an arbitrary POJO.

      The distinction between ObjectAssertion and AnyAssertion 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) and FluentBeanAssertion.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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertArray

      public static final <E> ArrayAssertion<E> assertArray(E[] value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertBean

      public static final <T> BeanAssertion<T> assertBean(T value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertBeanList

      public static final <E> BeanListAssertion<E> assertBeanList(List<E> value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertBoolean

      public static final BooleanAssertion assertBoolean(Boolean value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertBooleanArray

      public static final PrimitiveArrayAssertion<Boolean,boolean[]> assertBooleanArray(boolean[] value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertByteArray

      public static final PrimitiveArrayAssertion<Byte,byte[]> assertByteArray(byte[] value)
      Performs an assertion on a byte array.

      The distinction between assertByteArray(byte[]) and assertBytes(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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertBytes

      public static final ByteArrayAssertion assertBytes(byte[] value)
      Performs an assertion on a byte array.

      The distinction between assertByteArray(byte[]) and assertBytes(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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertBytes

      public static final ByteArrayAssertion assertBytes(InputStream value) throws IOException
      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 be null.
      Stream is automatically closed.
      Returns:
      A new assertion object.
      Never null.
      Throws:
      IOException - If thrown while reading contents from stream.
    • assertCharArray

      public static final PrimitiveArrayAssertion<Character,char[]> assertCharArray(char[] value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertCollection

      public static final <E> CollectionAssertion<E> assertCollection(Collection<E> value)
      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 since ListAssertion provides more functionality than CollectionAssertion.

      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertComparable

      public static final <T extends Comparable<T>> ComparableAssertion<T> assertComparable(T value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertDate

      public static final DateAssertion assertDate(Date value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertDoubleArray

      public static final PrimitiveArrayAssertion<Double,double[]> assertDoubleArray(double[] value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertFloatArray

      public static final PrimitiveArrayAssertion<Float,float[]> assertFloatArray(float[] value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertIntArray

      public static final PrimitiveArrayAssertion<Integer,int[]> assertIntArray(int[] value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertInteger

      public static final IntegerAssertion assertInteger(Integer value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertList

      public static final <E> ListAssertion<E> assertList(List<E> value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertList

      public static final <E> ListAssertion<E> assertList(Stream<E> value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertLong

      public static final LongAssertion assertLong(Long value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertLongArray

      public static final PrimitiveArrayAssertion<Long,long[]> assertLongArray(long[] value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertMap

      public static final <K, V> MapAssertion<K,V> assertMap(Map<K,V> value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertObject

      public static final <T> ObjectAssertion<T> assertObject(T value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertOptional

      public static final <T> AnyAssertion<T> assertOptional(Optional<T> value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertReader

      public static final StringAssertion assertReader(Reader value) throws IOException
      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 be null.
      Reader is automatically closed.
      Returns:
      A new assertion object.
      Never null.
      Throws:
      IOException - If thrown while reading contents from reader.
    • assertShortArray

      public static final PrimitiveArrayAssertion<Short,short[]> assertShortArray(short[] value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertString

      public static final StringAssertion assertString(Object value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertStringList

      public static final StringListAssertion assertStringList(List<String> value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertThrowable

      public static final <T extends Throwable> ThrowableAssertion<T> assertThrowable(T value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertVersion

      public static final VersionAssertion assertVersion(Version value)
      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 be null.
      Returns:
      A new assertion object.
      Never null.
    • 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 be null.
      Returns:
      A new assertion object.
      Never null.
    • assertThrown

      public static final ThrowableAssertion<Throwable> assertThrown(Snippet snippet)
      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.