Posts Java Assert
Post
Cancel

Java Assert

Assert is used to check if a condition is as you expected it to be and if the condition is not met then the prgoram should terminate.

  • Example
1
2
3
4
5
6
public void test(int x, int y){
    assert ( x > y );
    ..............
      code logic
    .............
}

The above method logic assumes that x will always be greater than y.

If we pass x smaller than y then the method will not work as expected.

So I added an asert to it because if x < y then some logical error occured in the flow that calls this method and there is no point to continue executing the program and it should terminate.

When assert fails it throws an AssertionError.

Since this is an error it will not be caught by an Exception.

You could catch it by catching errors but logically asser errors should never be handled as it defeats the purpose of using assertions.

If in the above program we pass x < y, it will simply fail and you will see an assertion error, no information associated with the error will be displayed.

in order to see add print custom output along with the assert error. We can write the same method like this.

1
2
3
4
5
6
public void test(int x, int y) {
      assert( x < y ):"value of x is "+x+" value of y is "+y;
      .............
 code logic 
      .............
}

If this method is fails we will see value of x and y on the console giving us more information.

The above lines after ‘:’ could be a string, boolean, method call that returns a boolean,int,float,double value.

assert always requires a boolean condition.

So the following conditions are legal.

1
2
3
4
5
6
7
8
9
10
int x = 1;
boolean b = true;
int returnSomething() { return 1; }

assert(x == 1);  //check results in a boolean expression
assert(b);       //check results in a boolean expression
assert true;     //checking a boolean condition
assert(x == 1) : x; //check results in a boolean expression and prints an integer
assert(x == 1) : returnSomething(); //check results in a boolean expression and method returns a value to be printed.
assert(x == 1) : new TestClass();   //check results in a boolean expression and new TestClass() object toString() method will be called to print the object information.

Following conditions are illegal:

1
2
3
4
5
6
assert(x = 1); //not a boolean condition
assert(x);     //not a boolean condition
assert 0;      //not a boolean condition
assert(x == 1) : ;  // missing failure information
assert(x == 1) : noReturn(); //method not returning anything, so missing failure information
assert(x == 1) : ValidAssert va; // variable declation does not returns anything

So logically assert expressions are of two types

assert :

expression1 :- always a boolean

expression2 :- not mandatory, but if present should be a value not a void.

assert is ignored by JVM by default so this has no impact on production code.

Inorder to run code with assert, we have to provide -ea or -enableassertions argument to java.

  • Example
1
2
3
4
5
$ java -ea com.x.TestClass

$ #Or 

$ java -enableassertions com.x.TestClass

Assertions are disabled by default but if we want we can disable them explicitly by passing -da or -disableassertions argument.

This is used when you want to enable assertions only all classes except come classes

So -ea and -da can be used

  • without any arguments

this enables or disables asserts on all classes

  • Class name

this enables or disables asserts only on the specified class

  • Package

this enables or disables asserts on a package and subpackages

  • Disable in system classes

this enables asserts for all except system classes

  • Enable for all except a class
1
java -ea -da:com.x.Test
  • Enable for all
1
java -ea Test
  • Disable for all (disabled by default)
1
java -da Test
  • Enable for all disable for some package and subpackages
1
java -ea -da:com.x.logger...

Note the three dots represent the package and sub packages in that package.

  • Disable in system classes
1
$ java -ea -dsa com.x.TestClass
This post is licensed under CC BY 4.0 by the author.