Java Assertion or assert keyword in java is little unknown and not many programmer is familiar with this and it's been rarely used specially if you have not writing unit test using JUnit which extensively uses Java assertion to compare test result. Junit itself is the biggest manifestation of what assertion in Java can do and believe me by using assertion along with Exception you can write robust code. Assertion not only improve stability of code but also help you to become better programmer by forcing you to think about different scenario while writing production quality code and improving your think through ability.
Java Assertion tutorial - How Assertion works in Java
What is assert keyword in Java
assert keyword is used to implement assertion in java. we can use assert keyword in two format
assert booleanExpression : errorMessage;
(here errorMessage can not be an invocation to a method with return type void)
How Assertion works in Java
As shown above assert keyword in java has two form first form "assert booleanExpression " is used to test the boolean expression and if boolean expression is false then java throws AssertionError and your program terminates. you can use assert here to validate input or assumption for example for a method which calculates stock price for trading , name of stock should not be null or empty, but as java recommends we should not use assertion to check arguments of public method instead public method should always check its argument and throw appropriate exception e.g. IllegalArgumentException.
Second form of assert keyword "assert booleanExpression : errorMessage" is more useful and provides a mechanism to pass additional data when Java assertion fails and java throws AssertionError.
Benefit of using Assertion in Java
Assertion in Java offers several benefits to programmer if it used properly. even many of seasoned programmer has recommended using assertion in Java as good programming practice and good to add this point on your code review checklist ? let's figure out why using assert keyword is desired and what benefit assertion offers :
1) assertion is simply great for data validation. In my opinion there is no better way then using java assert keyword to validate data passed to method, classic example is a method which calculates interest rates here we know that amount, time can not be less than zero by using assert keyword in java we can validate this data at run-time and if by any chance your function is getting incorrect data your program will fail with AssertionError.
2) Assertion in Java guarantees that at a certain point on function your assumption or certain condition is true otherwise it would not have come to that point and would have been terminated with AssertionError, this makes debugging in Java lot easier.
3) Using Java assert keyword helps to detect bug early in development cycle which is very cost effective. Assertion in Java also makes debugging easier to me AssertionError is a best kind of error while debugging because its clearly tell source and reason of error. Also code written using assert keyword fails close to source of error and require less time to find out the cause as compared to code without assert keyword.
4) assert statement in java is similar to unit test directly integrated into your code and have more chances to test your code with real word data than Junit test case, so it always complement your Junit tests or integration test suite.
5) writing code with assert statement will help you to be better programmer and improve quality of code, yes this is true based on my experience when we write code using assert statement we think through hard, we think about possible input to a function, we think about boundary condition which eventually result in better discipline and quality code.
6) assertion in java gives you lot of confident while maintaining or refactoring code, you can create new code and use assertion to compare output with old method if your program works well then you can simply comment out your old method.
1) Assertion is introduced in JDK 1.4 and implemented using assert keyword in java.
2) assertion can be enable and disable at run time by using switch -da or -disableassertion
3) Always remember Assertion does not replace Exception but compliments it.
4) Another important point is Assertion also doesn't replace need of unit testing instead if you see JUnit it shows how assertion can be useful to validate conditions.
5) do not use assertion to validate arguments or parameters of public method.
6) you can compile java code where assert is a legal identifier instead of keyword because by passing -source 1.3 during compilation. if you are working in java version 1.2 you can compile your code with assertion as below "
javac -source 1.4 OnlineStockTradingDemo.java
That’s all on Java Assertion, benefits of assertion in Java and where to use assertion in Java. Key point is Assertion should be thought as replacement of unit testing or Exception rather it compliments both of them , with Java assertion you can have more real world data for testing than unit testing.