Java Utililty Methods Assert

List of utility methods to do Assert

Description

The list of methods to do Assert are organized into topic(s).

Method

voidassertInstanceOf(Object obj, Class classType)
assert Instance Of
if (obj == null) {
    throw new IllegalArgumentException("Argument is null!");
if (!classType.isInstance(obj)) {
    throw new IllegalArgumentException("Argument should be instance of " + classType.getName()
            + ", but now is " + obj.getClass().getName());
voidassertInstanceOf(Object obj, Class expClass)
Checks if the passed object is of the class specified, null values are ignored
if (obj == null)
    return;
if (!(expClass.isAssignableFrom(obj.getClass()))) {
    throw new AssertionError(
            "Expected class: " + expClass.getName() + " but object has class: " + obj.getClass().getName());
voidassertInstanceOf(Object object, Class expectClass)
assert Instance Of
boolean b = expectClass.isInstance(object);
if (b == false) {
    throw new AssertionError(String.format("The expect class is %s, but actually %s.",
            expectClass.getName(), object.getClass().getName()));
voidassertIntegerGreaterThanZero(long number, String name)
Check if the integer is greater than zero.
if (number <= 0) {
    throw new IllegalArgumentException(name + " must be positive.");
voidassertIntegerNotNegative(String message, int num)
assert Integer Not Negative
if (num < 0) {
    throw new IllegalArgumentException(message);
voidassertion(boolean isTrue, String reason)
If false, throw an assertException, and give a reason
if (!isTrue) {
    throw new RuntimeException(reason);
voidassertion(boolean value)
assertion
if (!value)
    throw new RuntimeException("Assertion failed.");
Stringassertion(String operator, String expr)
assertion
return "(assert (" + operator + " " + toBool(expr) + "))\n";
RuntimeExceptionassertionError(String message, Object... args)
Returns a RuntimeException with a formatted error message.
return assertionError(null, message, args);
AssertionErrorassertionError(String message, Throwable cause)
assertion Error
AssertionError error = new AssertionError(message);
error.initCause(cause);
throw error;