Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Pinpoint Code Problems Using Java's Assertion Facility  : Page 2

Java developers have had the capability to include assertions in their code only since version 1.4. This article explains why, when, and how to use Java assertions to help you find and fix problems with your code—before you deploy it.


WEBINAR: On-demand Event

Unleash Your DevOps Strategy by Synchronizing Application and Database Changes REGISTER >

Using Assertions
When an assertion fails, the execution engine throws an AssertionError onto the stack trace. Note that you're dealing with an error, not an exception. The currently executing function aborts when the error is thrown,

Assertions in Java take two forms:

assert booleanExpression; assert booleanExpression : aNonVoidExpression;

The first form takes a Boolean expression as an argument. The simple program shown below uses this flavor of the assertion to check that i>0.

public class AssertSimple { public static void main (String args[]) { System.out.println("Simple Assert Example"); int i = 3; assert i>0; System.out.println("Value of i: " + i); i = -4; assert i>0; System.out.println("Value of i: " + i); System.out.println("End of program"); } }

Figure 1 shows the output from executing the preceding code. Note that the program was run twice: the first time without enabling assertions (the default), and the second time with assertions enabled.

Figure 1: Execution of AssertSimple: The program was run twice: once with assertions disabled and then again with assertions enabled
The second flavor of the assertion assert booleanExpression : aNonVoidExpression is similar to the first, except that when the Boolean expression evaluates to false, the runtime creates an AssertionError object using the data returned from evaluating the expression following the colon. The returned data type is converted to a string and dumped onto the stack trace. The expression must return a value—in other words, it must be a non-void expression.

The sample file AssertSecondFormSimple.java defines the class AssertSecondFormSimple shown below, which exemplifies the second assertion form.

public class AssertSecondFormSimple { public static void main(String args[]) { System.out.println( "Another Simple Assert Example"); int i = 3; assert i > 0: "i Must be greater than " + "zero.".toUpperCase(); System.out.println("Value of i: " + i); i = -4; assert i > 0: "i Must be greater than " + "zero.".toUpperCase(); System.out.println("Value of i: " + i); System.out.println("End of program"); } }

The AssertSecondFormSimple class code could just as easily have used an uppercased clause after the assert, as in the following line.

assert i>0 : "I MUST BE GREATER THAN ZERO.";

Figure 2. Execution of AssertSecondFormSimple. The error message resulting from evaluating the expression after the colon in the second assertion form appears when the assertion fails.
However, I intentionally used the toUpperCase method to show that the expression is evaluated (not just a string copy operation) when the assertion fails. In this case, the String's toUpperCase() method returns a string object which is in turn passed to the AssertError class constructor. Figure 2 shows how the object ends up being presented to the developer when the program balks during execution.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date