Wednesday, August 21, 2024

How to enable or disable Assertion in Java? Example Tutorial

The good thing about Assertion in Java is that you can enable or disable Assertion at runtime. The designer of Java are really very thoughtful, they have thought of many situations while designing Java and its API. One of the gems is the ability to enable or disable assertion in runtime without any change on code and no new deployment. You can just change a JVM parameter and enable or disable assertions. While most of you have used Assertions with JUnit assert API and familiar with methods like assertEquals(), the Java assertions is bit different than that. 

In last article we see how Assertion works in Java and now we will see some step by step guide on how to disable or enable Assertion in Java By default assertions in Java are disabled at runtime and JVM provides following options to deal with assertion in Java :

Enable or disable Assertion in Java

--ea[:...|:] or enableassertions  (to enable assertion at a particular package and class level)
--da[:...|:] or disableassertions (to disable assertion on a package or class level)
--esa or enablesystemassertions  (for enabling system assertion)
--dsa or disablesystemassertions (for disabling system assertion)

1) if we use -enableassertions or -disableassertions without arguments it enables or disable assertion in all classes except System classes

2) if we use -ea or -da with "packageName..." it enables or disable assertions in the named package and any sub package.

3) -enableassersions with "..." only enable assertions in the unnamed package in current working directory.

4) -enableassertions with "classname" only enable assertion in the named class in Java.




Example of Enable and Disable Assertion in Java

how to enable and disable assertions in Java example guideHere is an example of enabling assertion in java: in below example assertions will be enabled for package  "com.onlinestockbroker.electronicTrading" and it sub package.

java -ea:com.onlinestockbroker.electronicTrading... EquityStocks

and here is another example of disabling Assertion in the same class

java -da:com.onlinestockbroker.electronicTrading... EquityStocks


Also, here is a nice Java Assertion cheat sheet you can use to remember or revise essential Assertion conception in Java:

Java Assertion cheat sheet




That’s all on how to enable and disable Java Assertion. Important point is that you don’t need to change anything on code and can disable assertion at runtime with just changing some JVM options, which gives you the flexibility to run the same code on development and production environment. 

While in development you can enable assertion, you can disable assertion in production with just JVM switch.


Java Tutorials you may like

3 comments :

Preeti said...

Can you please let us know how to enable or disable assertions on Netbeans and Eclipse IDE ? I have a project which uses Assertions in various part of code and I want to enable them while working in Eclipse IDE.

Anand Vijayakumar said...

More details on what assertions are and how they are useful can be found by clicking here

Anand

Anonymous said...

How can we enable assertion in Eclipse ? I want to keep assertions enable by default for every Java project, anybody knows Eclipse settings for that?

Post a Comment