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

voidassertStringNotEmpty(String str, String name)
Check if the string is empty.
assertObjectNotNull(str, name);
if (str.trim().length() == 0) {
    throw new IllegalArgumentException("the string " + name + " should not be empty.");
StringassertStringNotEmpty(String string, String fieldName)
Asserts that the string is not empty.
assertNotNull(string, fieldName);
if (string.isEmpty()) {
    throw new IllegalArgumentException(String.format("%s cannot be empty", fieldName));
return string;
voidassertTesting()
Assert that we are running JUnit tests.
try {
    Class.forName("junit.framework.TestCase");
} catch (ClassNotFoundException e) {
    throw new IllegalStateException("This method must only be used by JUnit tests.");
voidassertThrows(Class exception, Runnable action)
assert Throws
try {
    action.run();
    throw new AssertionError(
            "Expected exception: " + exception.getTypeName() + ", but no exception thrown");
} catch (Exception e) {
    if (!e.getClass().equals(exception)) {
        throw new AssertionError("Expected exception: " + exception.getTypeName() + ", but was "
                + e.getClass().getTypeName());
...
voidassertTwoParameters(Class clz, String method, double... args)
assert Two Parameters
if (args == null || args.length != 2)
    throw new IllegalArgumentException(clz.getName() + "." + method
            + "(...) is only supported for two parameters (got " + args.length + ")");
TassertUnreachable()
assert Unreachable
throw new AssertionError("Unreachable.");
voidassertValid(int piece)
assert Valid
if (!isValid(piece)) {
    throw new IllegalArgumentException("Invalid piece: " + piece);
voidassertValidBrowserType(int aBrowserType)
assert Valid Browser Type
switch (aBrowserType) {
case BROWSER_WAP:
case BROWSER_COMPANY:
case BROWSER_WIFI:
case BROWSER_UNITE:
    return;
throw new IllegalArgumentException("Not a valid browser type");
...
voidassertValidColumnName(String name)
assert Valid Column Name
for (char c : name.toCharArray()) {
    if (c == '.')
        continue;
    if (Character.isJavaIdentifierPart(c))
        continue;
    throw new IllegalArgumentException("invalid character in field name");
voidassertValidItemName(String itemName)
Ensures that the specified name of the item is valid.
if (!isValidItemName(itemName)) {
    throw new IllegalArgumentException("The specified name of the item '" + itemName + "' is not valid!");