Java Assertions

Java Assertions: In this tutorial, we will find out about the Java assert statement (Java assertions)) with the help of examples.

assertions) in Java help to distinguish bugs by testing code we expect to be valid.

A declaration is made using the assert watchword.

Its syntax is:

assert condition;

Here, condition is a boolean expression that we assume to be true when the program executes.


Enabling Assertions

By default, assertions are disabled and ignored at runtime.

To enable assertions, we use:

java -ea:arguments

OR

java -enableassertions:arguments

When assertions are enabled and the condition is true, the program executes normally.

But if the condition evaluates to false while assertions are enabled, JVM throws an AssertionError, and the program stops immediately.


Example 1: Java assertion

class Main {
  public static void main(String args[]) {
    String[] weekends = {"Friday", "Saturday", "Sunday"};
    assert weekends.length == 2;
    System.out.println("There are " + weekends.length + "  weekends in a week");
  }
}

Output

There are 3 weekends in a week

We get the above output since this program has no gathering blunders and by default, statements are disabled.

After enabling assertions, we get the following output:

Exception in thread "main" java.lang.AssertionError

Another form of assertion statement

assert condition : expression;

In this form of assertion statement, an expression is passed to the constructor of the AssertionError object. This expression has a value that is displayed as the error’s detail message if the condition is false.

The detailed message is used to catch and transmit the data of the affirmation inability to help in debugging the issue.


Example 2: Java assertion with expression example

class Main {
  public static void main(String args[]) {
    String[] weekends = {"Friday", "Saturday", "Sunday"};
    assert weekends.length==2 : "There are only 2 weekends in a week";
    System.out.println("There are " + weekends.length + "  weekends in a week");
  }
}

Output

Exception in thread "main" java.lang.AssertionError: There are only 2 weekends in a week

As we see from the above example, the expression is passed to the constructor of the AssertionError object. If our assumption is false and assertions are enabled, an exception is thrown with an appropriate message.

This message helps in diagnosing and fixing the error that caused the assertion to fail.


Enabling assertion for specific classes and packages

If we do not provide any arguments to the assertion command-line switches,

java -ea

This enables declarations in all classes aside from framework classes.

We can likewise enable declaration for explicit classes and packages using contentions. The contentions that can be given to these command line switches are:

Enable assertion in class names

To enable assertion for all classes of our program Main,

java -ea Main

To enable only one class,

java -ea:AnimalClass Main

This enables assertion in only the AnimalClass in the Main program.

Enable assertion in package names

To enable assertions for package com.animal and its sub-packages only,

java -ea:com.animal... Main

Enable assertion in unnamed packages

To enable assertion in unnamed packages (when we don’t use a package statement) in the current working directory.

java -ea:... Main

Enable assertion in system classes

To enable assertion in system classes, we use a different command-line switch:

java -esa:arguments 

OR

java -enablesystemassertions:arguments

The arguments that can be provided to these switches are the same.


Disabling Assertions

To disable assertions, we use:

java -da arguments 

OR

java -disableassertions arguments

To disable assertion in system classes, we use:

java -dsa:arguments

OR

java -disablesystemassertions:arguments

The arguments that can be passed while disabling assertions are the same as while enabling them.


Advantages of Assertion

  • Quick and effective for recognizing and amending bugs.
  • Affirmation checks are done uniquely during improvement and testing. They are automatically taken out in the creation code at runtime so that it won’t slow the execution of the program.
  • It helps eliminate standard code and make code more comprehensible.
  • Refactors and improves code with expanded certainty that it capacities effectively.

When to use Assertions

1. Unreachable codes

Inaccessible codes will be codes that don’t execute when we attempt to run the program. Use declarations to ensure inaccessible codes are actually inaccessible.

Let’s take an example.

void unreachableCodeMethod() {
  System.out.println("Reachable code");
  return;
  // Unreachable code
  System.out.println("Unreachable code");
  assert false;
}

Let’s take another example of a switch statement without a default case.

switch (dayOfWeek) {
  case "Sunday":
    System.out.println("It’s Sunday!");
    break;
  case "Monday":
    System.out.println("It’s Monday!");
    break;
  case "Tuesday":
    System.out.println("It’s Tuesday!");
    break;
  case "Wednesday":
    System.out.println("It’s Wednesday!");
    break;
  case "Thursday":
    System.out.println("It’s Thursday!");
    break;
  case "Friday":
    System.out.println("It’s Friday!");
    break;
  case "Saturday":
    System.out.println("It’s Saturday!");
    break;
}

The above switch statement shows that the times of the week can be just one of the over 7 qualities. Having no default case implies that the programmer believes that one of these cases will consistently be executed.

However, there may be a few cases that have not yet been viewed as where the supposition that is in reality bogus.

This presumption ought to be checked utilizing an affirmation to ensure that the default switch case isn’t reached.

default:
    assert false: dayofWeek + " is invalid day";

If dayOfWeek has a value other than the valid days, an AssertionError is thrown.


2. Documenting assumptions

To document their underlying assumptions, many programmers use comments. Let’s take an example.

if (i % 2 == 0) {
    ...
} else { // We know (i % 2 == 1)
    ...
}

Use assertions instead.

Comments can get out-of-date and out-of-sync as the program grows. However, we will be forced to update the assert statements; otherwise, they might fail for valid conditions too.

if (i % 2 == 0) {
   ...
} else {
    assert i % 2 == 1 : i;
    ...
}

When not to use Assertions

1. Argument checking in public methods

Arguments in public methods may be provided by the user.

Thus, if a declaration is used to check these contentions, the conditions may fail and result in AssertionError.

Rather than using affirmations, let it result in the fitting runtime exemptions and handle these exceptions.


To evaluate expressions that affect the program operation

Try not to call strategies or evaluate exceptions that can later influence the program activity in declaration conditions.

Let us take an example of a list of weekdays that contains the names of all the days in a week.

ArrayList<String> weekdays = new ArrayList<>(Arrays.asList("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ));

ArrayList<String> weekends= new ArrayList<>(Arrays.asList("Sunday", "Saturday" ));

assert weekdays.removeAll(weekends);

Here, we are trying to remove elements Saturday and Sunday from the ArrayList weekdays.

If the assertion is enabled, the program works fine. However, if assertions are disabled, the elements from the list are not removed. This may result in program failure.

Instead, assign the result to a variable and then use that variable for assertion.

ArrayList<String> weekdays = new ArrayList<>(Arrays.asList("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ));

ArrayList<String> weekends= new ArrayList<>(Arrays.asList("Sunday", "Saturday" ));

boolean weekendsRemoved = weekdays.removeAll(weekends);
assert weekendsRemoved;

In this way, we can ensure that all the weekends are removed from the weekdays regardless of the assertion being enabled or disabled. As a result, it does not affect the program operation in the future.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.