Java Tutorial : Assertion In Java
Assertions are the checks provided by the Java language to ensure that any assumption made at the start of a program is true throughout the program. The assumptions in a program can be simple facts, such as the range of a number is between 10 and 20; or a number cannot be greater than 100.
Assertions are statements in Java that enable you to test the assumptions made in a program during program execution. An assertion statement contains a boolean expression that the programmer believes to be true at the time the statement is executed. Assertions are used during testing of a program to ensure that the specified condition is true.
For example, a particular program may require the value being passed to a method, to be positive. You can test this by asserting that the value being passed to that particular method is greater than zero by using an assert statement. The following syntax shows how to declare an assertion using assert statement in Java:
assert expressionA;
In the preceding syntax, expressionA is a boolean expression. During the execution of program, the expressionA in the assert statement is tested. If the expressionA returns true, then assertion made in the program is true; and the program execution continues uninterruptedly; and no action takes place.
However, if the expressionA returns false, then the assertion made in the program fails. The program throws an AssertionError object and the program execution terminates and an AssertionError object is thrown. You can also declare assertion in
Java as:
assert expression1 : expression2;
In the preceding syntax, expression1 is a boolean expression and the expression2 is an error message value that is displayed if the assertion fails. The expression2 is passed to the AssertionError constructor of the AssertionError class if the expression1 returns false. The AssertionError constructor converts the value in expresion2 into string format and displays the message if the assertion fails. The syntax of one of the constructor of the AssertionError class is:
public AssertionError(char msg)
In the preceding syntax, constructor constructs an AssertionError object. The parameter msg is an error message that is passed to AssertionError object when assertion fails.
Compiling Files Using Assertions
The Java program that uses assert statement is compiled with an option, -source 1.4. The following syntax shows the compilation of the Java program named AssertDemo.java that is using assert statement:
javac -source 1.4 AssertDemo.java
In the preceding syntax, AssertDemo.java is the name of a Java program using assertion. You use –source 1.4 as a command-line option to make the Java compiler accept the code containing assertions.
Enabling and Disabling the Assertions
By default, assertions are disabled at run-time of a Java program. The command-line switch, -enableassertions or –ea allows you to selectively enable assertion during run-time. The command-line switch, -disableassertions or –da allows you to selectively disable assertion during run-time. The syntax to enable assertion in a specified class with –ea option is:
java –ea: AssertDemo
In the preceding syntax, AssertDemo is a file compiled by the Java compiler. During execution of the AssertDemo file assertion checking is enabled. The syntax to disable assertion in a specified class with –da option is:
java –da: AssertDemo
In the preceding syntax, AssertDemo is a file compiled by the Java compiler. During execution of the AssertDemo file assertion checking is disabled. The following code shows the use of assertions:
class BalanceAssertDemo
{
static float balance=1500;
static float transaction(float amt )
{
balance=balance-amt;
System.out.println(“The current balance is:$” + balance);
return balance;
}
public static void main(String args[])
{
float n;
for(int i=0;i<4;i++)
{
n = transaction(400);
assert balance >= 500.00 : ” Balance is below $500.”;
// checking for assertion
}
}
}
In the preceding code, the class BalanceAssertDemo class has balance as a variable data member, balance. The assert statement is used in the main() method to declare an assertion that the minimum balance is assumed to be always greater than or equal to $500. Therefore, the assumption is that balance cannot have value less than $500. The assumption is tested by assert statement. The transaction() method deducts $400 from the balance data member. After every transaction the value in the balance data member is tested against $500, the assertion.
Initially the value of the balance data member is $1500. After two transactions of $400 the balance becomes $700. After third transaction, the balance remains $300, which is less than $500. Therefore, the assertion fails. The assert statement throws an
AssertionError object with a message” that Balance is below $500”. The output of the preceding code is:


