Witscale Test Center

5.6 Assertions > 5.6.2 The assert statement and AssertionError class


5.6.2 The assert statement and AssertionError class

Assertion simply asserts the truth. It is specified with the assert statement and a condition. If the condition is true, the code will run just fine. If not, the code throws an error. The error it may throw is an object of the AssertionError class.  The assert statement and the AssertionError class are the two backbones of Java’s assertion mechanism.

 

The assert statement has two forms:

 

1.       assert booleanExpression;

 

2.       assert booleanExpression: informationExpression;

 

 The first one is a simpler one; you only need to specify the condition you want to assert in a form of booleanExpression. This booleanExpression must always result into a value of boolean type. For instance, you can specify the condition as:

 

private void eligibleToVote(int age) {

   assert(age > 18)

   // implementation of other authentications

}

The booleanExpression (age > 18) is evaluated to either true or false, depending on the argument age. If it’s true, the method continues its normal execution. If false, the method is aborted and execution stops. The Java runtime throws an error of AssertionError type. Figure 5.6 shows a flow in the execution of a simple assert statement.


Figure 5.6 Flow of control during the execution of assert statement

 

The java.lang.AssertionError is a subclass of java.lang.Error. The Java runtime handles the AssertionError just like other errors, by printing the stack trace. The informationExpression in the second form of the assert statement is especially useful for debugging when the assertion condition fails. You can print additional debugging information deduced from the informationExpression on the stack trace. The following code calls the eligibleToVote() method passing 2 as the argument for age .

 

package example.voting;

public class VotingTerminal {

 public static void main(String[] args) {

    new VotingTerminal().eligibleToVote(2);

 }

 private void eligibleToVote(int age) {

  assert(age >= 18) : "The age " +age + " is less than the required age";

  // Implementation of other authentication

 }

}

 

The assertion for the condition (age > 18) fails for the age 2 and AssertionError is thrown at runtime. The string representation of the value of the informationExpression is “The age 2 is less than the required age”. It is printed as part of the stack trace as:

 

java.lang.AssertionError: The age 2 is less than the required age

 at example.elections.VotingTerminal.eligibleToVote(VotingTerminal.java:10)

 at example.elections.VotingTerminal.main(VotingTerminal.java:5)

Exception in thread "main"

 

The informationExpression must be evaluated to some value. Although what you see here is just a simple String object, the informationExpression can produce any kind of object. The value of informationExpression can be either a primitive value or any object reference. Table 5.9 summarizes some valid and invalid examples of the assert statement.

 

Table 5.9 Valid and invalid values in assert expressions

 

Valid assert Statements

Invalid assert Statements

assert(true);

assert(0);

assert false;

assert 1;

assert ( age==18);

assert ( age=18);

assert(age>18) : "Age is  + age";

assert (age>18) : void;

public int method(){return 0;}

assert (age>18) : method();

public void method(){}

assert (age>18) : method();

assert (age>18) : new Integer(age);