public final class Assert extends Object
Internal class for making application logic related assertions.
Utility methods provided by this class may be used in the application (however, extending this class itself is not allowed) for making clean assertions and avoiding condition blocks. Designed for making application logic look clean and work fast.
However, it is undesirable to instantiate (and not required too) this class anywhere in the application.
Further, extension is also not allowed as it may have side effects (not to mention, methods of this class use reflective code to perform some difficult operations).
| Constructor and Description |
|---|
Assert() |
| Modifier and Type | Method and Description |
|---|---|
static void |
assertEquals(Object... args)
Tests the given arguments for equality based upon the
Object.equals(Object) method of the
class in focus and throws an IllegalArgumentException with a default message if the
arguments are not equal. |
static void |
assertEquals(Object arg1,
Object arg2)
Tests the given arguments for equality based upon the
Object.equals(Object) method of the
class in focus and throws an IllegalArgumentException with a default message if the
arguments are not equal. |
static void |
assertEquals(Object arg1,
Object arg2,
String message)
Tests the given arguments for equality based upon the
Object.equals(Object) method of the
class in focus and throws an IllegalArgumentException with the provided message if
the arguments are not equal. |
static void |
assertNotEmpty(Object... args)
Tests the given arguments of type
String, StringBuilder or
StringBuffer for empty (but not null) value and throws an
IllegalArgumentException with a default message if any argument is an empty string
literal or representation. |
static void |
assertNotEmpty(Object arg)
Tests the given argument of type
String, StringBuilder or
StringBuffer for empty (but not null) value and throws an
IllegalArgumentException with a default message if the argument is an empty string
literal or representation. |
static void |
assertNotEmpty(Object arg,
String message)
Tests the given argument of type
String, StringBuilder or
StringBuffer for empty (but not null) value and throws an
IllegalArgumentException with the provided message if the argument is an empty string
literal or representation. |
static void |
assertNotNull(Object... args)
Tests the given arguments for
null identity and throws an
IllegalArgumentException with a default message if any argument is null. |
static void |
assertNotNull(Object arg)
Tests the given argument for
null identity and throws an
IllegalArgumentException with a default message if the argument is null. |
static void |
assertNotNull(Object arg,
String message)
Tests the given argument for
null identity and throws an
IllegalArgumentException with the provided message if the argument is
null. |
static void |
assertTrue(boolean... conditions)
Tests the given conditions for truth and throws an
IllegalArgumentException with a
default message if any condition is false. |
static void |
assertTrue(boolean condition)
Tests the given condition for truth and throws an
IllegalArgumentException with a
default message if the condition is false. |
static void |
assertTrue(boolean condition,
String message)
Tests the given condition for truth and throws an
IllegalArgumentException with the
provided message if the condition is false. |
public static void assertNotNull(Object arg, String message) throws IllegalArgumentException
Tests the given argument for null identity and throws an
IllegalArgumentException with the provided message if the argument is
null.
arg - the object for null assertionmessage - the message to be returned with exception if assertion failsIllegalArgumentException - exception thrown if assertion failspublic static void assertNotNull(Object arg) throws IllegalArgumentException
Tests the given argument for null identity and throws an
IllegalArgumentException with a default message if the argument is null.
arg - the object for null assertionIllegalArgumentException - exception thrown if assertion failspublic static void assertNotNull(Object... args)
Tests the given arguments for null identity and throws an
IllegalArgumentException with a default message if any argument is null.
args - the object for null assertionIllegalArgumentException - exception thrown if assertion failspublic static void assertEquals(Object arg1, Object arg2, String message)
Tests the given arguments for equality based upon the Object.equals(Object) method of the
class in focus and throws an IllegalArgumentException with the provided message if
the arguments are not equal. An IllegalArgumentException exception is also thrown
when any of the arguments are null or the objects' classes are not assignable from each
other.
It is highly recommended to override the Object.equals(Object) and Object.hashCode()
methods in the class for which object comparison is desired as Object class'
Object.equals(Object) method returns equality on the basis of equal identity only; which
may not be always desirable.
arg1 - the first object argument for equality assertionarg2 - the second object argument for equality assertionmessage - the message to be returned with the exception if assertion failsIllegalArgumentException - exception thrown if assertion failspublic static void assertEquals(Object arg1, Object arg2)
Tests the given arguments for equality based upon the Object.equals(Object) method of the
class in focus and throws an IllegalArgumentException with a default message if the
arguments are not equal. An IllegalArgumentException exception is also thrown when
any of the arguments are null or the objects' classes are not assignable from each other.
It is highly recommended to override the Object.equals(Object) and Object.hashCode()
methods in the class for which object comparison is desired as Object class'
Object.equals(Object) method returns equality on the basis of equal identity only; which
may not be always desirable.
arg1 - the first object argument for equality assertionarg2 - the second object argument for equality assertionIllegalArgumentException - exception thrown if assertion failspublic static void assertEquals(Object... args)
Tests the given arguments for equality based upon the Object.equals(Object) method of the
class in focus and throws an IllegalArgumentException with a default message if the
arguments are not equal. An IllegalArgumentException exception is also thrown when
any of the arguments are null or the objects' classes are not assignable from each other.
It is highly recommended to override the Object.equals(Object) and Object.hashCode()
methods in the class for which object comparison is desired as Object class'
Object.equals(Object) method returns equality on the basis of equal identity only; which
may not be always desirable.
args - the object arguments for equality assertionIllegalArgumentException - exception thrown if assertion failspublic static void assertNotEmpty(Object arg, String message)
Tests the given argument of type String, StringBuilder or
StringBuffer for empty (but not null) value and throws an
IllegalArgumentException with the provided message if the argument is an empty string
literal or representation. An IllegalArgumentException exception is also thrown if
the argument is null or the object is not an instance of any one of the three
popular string representations in Java.
arg - the object arguments for not empty assertionmessage - the message to be returned with the exception if assertion failsIllegalArgumentException - exception thrown if assertion failspublic static void assertNotEmpty(Object arg)
Tests the given argument of type String, StringBuilder or
StringBuffer for empty (but not null) value and throws an
IllegalArgumentException with a default message if the argument is an empty string
literal or representation. An IllegalArgumentException exception is also thrown if
the argument is null or the object is not an instance of any one of the three
popular string representations in Java.
arg - the object argument for not empty assertionIllegalArgumentException - exception thrown if assertion failspublic static void assertNotEmpty(Object... args)
Tests the given arguments of type String, StringBuilder or
StringBuffer for empty (but not null) value and throws an
IllegalArgumentException with a default message if any argument is an empty string
literal or representation. An IllegalArgumentException exception is also thrown if
any argument is null or the object is not an instance of any one of the three
popular string representations in Java.
args - the object arguments for not empty assertionIllegalArgumentException - exception thrown if assertion failspublic static void assertTrue(boolean condition,
String message)
IllegalArgumentException with the
provided message if the condition is false.condition - the condition for truth assertionmessage - the message to be returned with the exception if assertion failsIllegalArgumentException - exception thrown if assertion failspublic static void assertTrue(boolean condition)
IllegalArgumentException with a
default message if the condition is false.condition - the condition for truth assertionIllegalArgumentException - exception thrown if assertion failspublic static void assertTrue(boolean... conditions)
IllegalArgumentException with a
default message if any condition is false.conditions - the conditions for truth assertionIllegalArgumentException - exception thrown if assertion failsCopyright © 2015. All rights reserved.