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

voidassertParameterWithinBounds(String name, long lower, long upper, long parameter)
assert Parameter Within Bounds
if (parameter < lower || parameter > upper) {
    throw new IllegalArgumentException(
            String.format("Invalid %s: %d (expected: %d <= n < %d)", name, parameter, lower, upper + 1));
voidassertPasswordMeetsRequirements(String password, boolean expected)
assert Password Meets Requirements
boolean actual = passwordMeetsRequirements(password);
String quotedPassword = (password == null) ? "null" : "\"" + password + "\"";
String output = "passwordMeetsRequirements(" + quotedPassword + ") = " + actual;
if (actual == expected) {
    output += " (expected)";
} else {
    output += ", expected " + expected + ". NOOOOOOOOOOOOOOOOOOOOOOOOOO!";
System.out.println(output);
voidassertPositive(int num, String err)
assert Positive
if (num <= 0) {
    throw new RuntimeException(err);
voidassertPositive(int val, String msg)
Checks what value is positive.
if (val < 0) {
    throw new IllegalArgumentException(msg);
voidassertPositive(int value)
throws an IllegalArgumentException if the value is smaller then 0
if (value < 0) {
    throw new IllegalArgumentException("value is smaller then 0");
voidassertPrecondition(boolean b)
Checks that a precondition (declared using the javadoc @pre tag) is satisfied.
assertTrue(b);
voidassertQualifiedString(String str, String argName)
assert Qualified String
if (!isQualifiedString(str)) {
    throw new IllegalArgumentException(
            "Null or zero length string found. str[" + str + "] argumentName[" + argName + "]");
doubleassertRange(String label, double value, double min, double max)
assert Range
if (value < min || value > max) {
    throw new IllegalArgumentException(
            String.format("%s must be between %f and %f. Value was: %f", label, min, max, value));
return value;
voidassertRequiredArgs(Object[] methodArgs, int requiredArgs, String methodName)
assert Required Args
if (methodArgs == null) {
    throw new IllegalArgumentException(
            String.format("Expected %d arguments for %s, got 0", requiredArgs, methodName));
} else if (methodArgs.length < requiredArgs) {
    throw new IllegalArgumentException(String.format("Expected %d arguments for %s, got %d", requiredArgs,
            methodName, methodArgs.length));
voidassertSame(Object expected, Object actual)
assert Same
if (expected != actual)
    throw new AssertionError();