Site Search:

Test invariants by using assertions

Back>

assert
assert

The syntax for an assert statement has two forms:

  • assert boolean_expression;
  • assert boolean_expression: error_message;
The boolean expression must evaluate to true or false. It can be inside optional parenthesis. The optional error message is used as the message for the AssertionError that is thrown. Notice the programmer should not catch Errors, so assert false will halt the program.


Enabling and Disabling Assertions

By default, assertions are disabled at runtime. Two command-line switches allow you to selectively enable or disable assertions.
To enable assertions at various granularities, use the -enableassertions, or -ea, switch. To disable assertions at various granularities, use the -disableassertions, or -da, switch. You specify the granularity with the arguments that you provide to the switch:
  • no arguments
       Enables or disables assertions in all classes except system classes.
  • packageName...
       Enables or disables assertions in the named package and any subpackages.
  • ...
       Enables or disables assertions in the unnamed package in the current working directory.
  • className
       Enables or disables assertions in the named class
For example, the following command runs a program, BatTutor, with assertions enabled in only package com.wombat.fruitbat and its subpackages:
 java -ea:com.wombat.fruitbat... BatTutor
If a single command line contains multiple instances of these switches, they are processed in order before loading any classes. For example, the following command runs the BatTutor program with assertions enabled in package com.wombat.fruitbat but disabled in class com.wombat.fruitbat.Brickbat:
 java -ea:com.wombat.fruitbat... -da:com.wombat.fruitbat.Brickbat BatTutor 
The above switches apply to all class loaders. With one exception, they also apply to system classes (which do not have an explicit class loader). The exception concerns the switches with no arguments, which (as indicated above) do not apply to system classes.This behavior makes it easy to enable asserts in all classes except for system classes, which is commonly desirable.
To enable assertions in all system classes, use a different switch: -enablesystemassertions, or -esa. Similarly, to disable assertions in system classes, use -disablesystemassertions, or -dsa.
For example, the following command runs the BatTutor program with assertions enabled in system classes, as well as in the com.wombat.fruitbat package and its subpackages:
 java -esa -ea:com.wombat.fruitbat... 


OCPJP>cat AssertExample.java 
public class AssertExample {
    public static void main(String...args) {
        assert false: "error message in AssertionError";
    }
}
OCPJP>javac AssertExample.java 
OCPJP>java AssertExample
OCPJP>java -enableassertions AssertExample
Exception in thread "main" java.lang.AssertionError: error message in AssertionError
at AssertExample.main(AssertExample.java:3)
OCPJP>java -ea AssertExample
Exception in thread "main" java.lang.AssertionError: error message in AssertionError
at AssertExample.main(AssertExample.java:3)
OCPJP>java -da AssertExample

OCPJP>

assert statement allowing detecting program errors earlier in the programming develop-test-deployment process.