Useful for identifying programmer errors early and clearly at runtime.
For example, if the contract of a public method states it does not
allow null arguments, Assert can be used to validate that
contract. Doing this clearly indicates a contract violation when it
occurs and protects the class's invariants.
Typically used to validate method arguments rather than configuration properties, to check for cases that are usually programmer errors rather than configuration errors. In contrast to configuration initialization code, there is usually no point in falling back to defaults in such methods.
This class is similar to JUnit's assertion library. If an argument value is
deemed invalid, an IllegalArgumentException is thrown (typically).
For example:
Assert.notNull(clazz, "The class must not be null"); Assert.isTrue(i > 0, "The value must be greater than zero");
Mainly for internal use within the framework; for a more comprehensive suite
of assertion utilities consider org.apache.commons.lang3.Validate from
Apache Commons Lang,
Google Guava's
Preconditions,
or similar third-party libraries.
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private static void | |
| public static void | doesNotContain(String
the text to search textToSearch, String the substring to find within the text substring, String the exception message to use if the assertion fails message)Assert that the given text does not contain the given substring. |
| public static void | doesNotContain(String
the text to search textToSearch, String the substring to find within the text substring, Supplier<String> a supplier for the exception message to use if the
assertion fails messageSupplier)Assert that the given text does not contain the given substring. |
| private static boolean | |
| public static void | |
| public static void | |
| public static void | |
| public static void | |
| private static void | |
| public static void | isAssignable(Class<?>
the supertype to check against superType, Class<?> the subtype to check subType, String a message which will be prepended to provide further context.
If it is empty or ends in ":" or ";" or "," or ".", a full exception message
will be appended. If it ends in a space, the name of the offending subtype
will be appended. In any other case, a ":" with a space and the name of the
offending subtype will be appended. message)Assert that |
| public static void | isAssignable(Class<?>
the supertype to check against superType, Class<?> the subtype to check subType, Supplier<String> a supplier for the exception message to use if the
assertion fails. See messageSupplier)isAssignable(Class, Class, String) for details.Assert that |
| public static void | isAssignable(Class<?>
the supertype to check superType, Class<?> the subtype to check subType)Assert that |
| public static void | isInstanceOf(Class<?>
the type to check against type, Object the object to check obj, String a message which will be prepended to provide further context.
If it is empty or ends in ":" or ";" or "," or ".", a full exception message
will be appended. If it ends in a space, the name of the offending object's
type will be appended. In any other case, a ":" with a space and the name
of the offending object's type will be appended. message)Assert that the provided object is an instance of the provided class. |
| public static void | isInstanceOf(Class<?>
the type to check against type, Object the object to check obj, Supplier<String> a supplier for the exception message to use if the
assertion fails. See messageSupplier)isInstanceOf(Class, Object, String) for details.Assert that the provided object is an instance of the provided class. |
| public static void | isInstanceOf(Class<?>
the type to check against type, Object the object to check obj)Assert that the provided object is an instance of the provided class. |
| public static void | |
| public static void | |
| public static void | |
| public static void | |
| private static String | |
| public static void | noNullElements(Object[]
the array to check array, String the exception message to use if the assertion fails message)Assert that an array contains no |
| public static void | noNullElements(Object[]
the array to check array, Supplier<String> a supplier for the exception message to use if the
assertion fails messageSupplier)Assert that an array contains no |
| public static void | noNullElements(Collection<?>
the collection to check collection, String the exception message to use if the assertion fails message)Assert that a collection contains no |
| public static void | noNullElements(Collection<?>
the collection to check collection, Supplier<String> a supplier for the exception message to use if the
assertion fails messageSupplier)Assert that a collection contains no |
| public static void | |
| public static void | |
| public static void | notEmpty(Collection<?>
the collection to check collection, String the exception message to use if the assertion fails message)Assert that a collection contains elements; that is, it must not be
|
| public static void | notEmpty(Collection<?>
the collection to check collection, Supplier<String> a supplier for the exception message to use if the
assertion fails messageSupplier)Assert that a collection contains elements; that is, it must not be
|
| public static void | |
| public static void | |
| public static void | |
| public static void | |
| private static String | |
| public static void | |
| public static void |
| Assert | back to summary |
|---|---|
| public Assert() | |
| assignableCheckFailed | back to summary |
|---|---|
| private static void assignableCheckFailed(Class<?> superType, Class<?> subType, String msg) | |
| doesNotContain | back to summary |
|---|---|
| public static void doesNotContain(String textToSearch, String substring, String message) Assert that the given text does not contain the given substring. Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");
| |
| doesNotContain | back to summary |
|---|---|
| public static void doesNotContain(String textToSearch, String substring, Supplier<String> messageSupplier) Assert that the given text does not contain the given substring. Assert.doesNotContain(name, forbidden, () -> "Name must not contain '" + forbidden + "'");
| |
| endsWithSeparator | back to summary |
|---|---|
| private static boolean endsWithSeparator(String msg) | |
| hasLength | back to summary |
|---|---|
| public static void hasLength(String text, String message) Assert that the given String is not empty; that is,
it must not be Assert.hasLength(name, "Name must not be empty");
| |
| hasLength | back to summary |
|---|---|
| public static void hasLength(String text, Supplier<String> messageSupplier) Assert that the given String is not empty; that is,
it must not be
Assert.hasLength(account.getName(),
() -> "Name for account '" + account.getId() + "' must not be empty");
| |
| hasText | back to summary |
|---|---|
| public static void hasText(String text, String message) Assert that the given String contains valid text content; that is, it must not
be Assert.hasText(name, "'name' must not be empty");
| |
| hasText | back to summary |
|---|---|
| public static void hasText(String text, Supplier<String> messageSupplier) Assert that the given String contains valid text content; that is, it must not
be
Assert.hasText(account.getName(),
() -> "Name for account '" + account.getId() + "' must not be empty");
| |
| instanceCheckFailed | back to summary |
|---|---|
| private static void instanceCheckFailed(Class<?> type, Object obj, String msg) | |
| isAssignable | back to summary |
|---|---|
| public static void isAssignable(Class<?> superType, Class<?> subType, String message) Assert that Assert.isAssignable(Number.class, myClass, "Number expected");
| |
| isAssignable | back to summary |
|---|---|
| public static void isAssignable(Class<?> superType, Class<?> subType, Supplier<String> messageSupplier) Assert that Assert.isAssignable(Number.class, myClass, () -> "Processing " + myAttributeName + ":");
| |
| isAssignable | back to summary |
|---|---|
| public static void isAssignable(Class<?> superType, Class<?> subType) Assert that Assert.isAssignable(Number.class, myClass);
| |
| isInstanceOf | back to summary |
|---|---|
| public static void isInstanceOf(Class<?> type, Object obj, String message) Assert that the provided object is an instance of the provided class. Assert.instanceOf(Foo.class, foo, "Foo expected");
| |
| isInstanceOf | back to summary |
|---|---|
| public static void isInstanceOf(Class<?> type, Object obj, Supplier<String> messageSupplier) Assert that the provided object is an instance of the provided class. Assert.instanceOf(Foo.class, foo, () -> "Processing " + Foo.class.getSimpleName() + ":");
| |
| isInstanceOf | back to summary |
|---|---|
| public static void isInstanceOf(Class<?> type, Object obj) Assert that the provided object is an instance of the provided class. Assert.instanceOf(Foo.class, foo);
| |
| isNull | back to summary |
|---|---|
| public static void isNull(Object object, String message) Assert that an object is Assert.isNull(value, "The value must be null");
| |
| isNull | back to summary |
|---|---|
| public static void isNull(Object object, Supplier<String> messageSupplier) Assert that an object is Assert.isNull(value, () -> "The value '" + value + "' must be null");
| |
| isTrue | back to summary |
|---|---|
| public static void isTrue(boolean expression, String message) Assert a boolean expression, throwing an Assert.isTrue(i > 0, "The value must be greater than zero");
| |
| isTrue | back to summary |
|---|---|
| public static void isTrue(boolean expression, Supplier<String> messageSupplier) Assert a boolean expression, throwing an Assert.isTrue(i > 0, () -> "The value '" + i + "' must be greater than zero");
| |
| messageWithTypeName | back to summary |
|---|---|
| private static String messageWithTypeName(String msg, Object typeName) | |
| noNullElements | back to summary |
|---|---|
| public static void noNullElements(Object[] array, String message) Assert that an array contains no Note Does not complain if the array is empty! Assert.noNullElements(array, "The array must contain non-null elements");
| |
| noNullElements | back to summary |
|---|---|
| public static void noNullElements(Object[] array, Supplier<String> messageSupplier) Assert that an array contains no Note Does not complain if the array is empty! Assert.noNullElements(array, () -> "The " + arrayType + " array must contain non-null elements");
| |
| noNullElements | back to summary |
|---|---|
| public static void noNullElements(Collection<?> collection, String message) Assert that a collection contains no Note Does not complain if the collection is empty! Assert.noNullElements(collection, "Collection must contain non-null elements");
| |
| noNullElements | back to summary |
|---|---|
| public static void noNullElements(Collection<?> collection, Supplier<String> messageSupplier) Assert that a collection contains no Note Does not complain if the collection is empty! Assert.noNullElements(collection, () -> "Collection " + collectionName + " must contain non-null elements");
| |
| notEmpty | back to summary |
|---|---|
| public static void notEmpty(Object[] array, String message) Assert that an array contains elements; that is, it must not be
Assert.notEmpty(array, "The array must contain elements");
| |
| notEmpty | back to summary |
|---|---|
| public static void notEmpty(Object[] array, Supplier<String> messageSupplier) Assert that an array contains elements; that is, it must not be
Assert.notEmpty(array, () -> "The " + arrayType + " array must contain elements");
| |
| notEmpty | back to summary |
|---|---|
| public static void notEmpty(Collection<?> collection, String message) Assert that a collection contains elements; that is, it must not be
Assert.notEmpty(collection, "Collection must contain elements");
| |
| notEmpty | back to summary |
|---|---|
| public static void notEmpty(Collection<?> collection, Supplier<String> messageSupplier) Assert that a collection contains elements; that is, it must not be
Assert.notEmpty(collection, () -> "The " + collectionType + " collection must contain elements");
| |
| notEmpty | back to summary |
|---|---|
| public static void notEmpty(Map<?, ?> map, String message) Assert that a Map contains entries; that is, it must not be Assert.notEmpty(map, "Map must contain entries");
| |
| notEmpty | back to summary |
|---|---|
| public static void notEmpty(Map<?, ?> map, Supplier<String> messageSupplier) Assert that a Map contains entries; that is, it must not be Assert.notEmpty(map, () -> "The " + mapType + " map must contain entries");
| |
| notNull | back to summary |
|---|---|
| public static void notNull(Object object, String message) Assert that an object is not Assert.notNull(clazz, "The class must not be null");
| |
| notNull | back to summary |
|---|---|
| public static void notNull(Object object, Supplier<String> messageSupplier) Assert that an object is not
Assert.notNull(entity.getId(),
() -> "ID for entity " + entity.getName() + " must not be null");
| |
| nullSafeGet | back to summary |
|---|---|
private static String nullSafeGet(Supplier<String> messageSupplier)
| |
| state | back to summary |
|---|---|
| public static void state(boolean expression, String message) Assert a boolean expression, throwing an Call Assert.state(id == null, "The id property must not already be initialized");
| |
| state | back to summary |
|---|---|
| public static void state(boolean expression, Supplier<String> messageSupplier) Assert a boolean expression, throwing an Call
Assert.state(entity.getId() == null,
() -> "ID for entity " + entity.getName() + " must not already be initialized");
| |