Assert in Java

Here we have discussed assertion in Java in detail with its types, benefits, syntax and an example that will help beginners to understand its concept and use.

Ads

Tutorials   
Java final method Assert in Java Final method in java Autoboxing in Java assert In Java JDBC batch insert using Java bean class Java Singleton Pattern Java Collection API - Java Tutorials Stack Overflow - Java Tutorials End of Year Puzzle The link to the outer class,java tutorial,java tutorials Orientating Components Right to Left,java newsletter,java,tutorial Non-virtual Methods in Java - java tutorials Multicasting in Java - java tutorials,tutorial Creation Time Comparison of Multi Dimensional Array- Java Tutorials Making Exceptions Unchecked - java tutorial,java tutorials Making Enumerations Iterable - JDK 5 Example Which is Faster - LinkedList or ArrayList? LinkedHashMap - Java Tutorials HashMap and HashCode JDK 1.4 the NullPointerException - Java Tutorials Java HashMap - Java Tutorials Java GC java awt package tutorial Java Multi Dimensions Array - Java Tutorials Java HashSet  - Java Tutorials Java Field Initialisation - Java Tutorials Java Comments - Java Tutorials J2EE Singleton Pattern - Design Pattern Tutorials Java - JDK Tutorials garbage collection in java Final Methods - Java Tutorials Exceptional Constructors - Java Tutorials Double-checked locking,java tutorials,java tutorial Disassembling Java Classes - Java Tutorials Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java Commenting out your code - Java Tutorials Class names don't identify a class - Java Tutorials Causing Deadlocks in Swing Code Exception in Java - Java Tutorials Catching Exceptions in GUI Code - Java Tutorials boolean comparisons - tutorial Interthread Communication in Java BASIC Java - Java Tutorials Thread Deadlocks - Java Tutorials Autoboxing unboxing in Java - Java Tutorials Assertion in Java Appending Strings - Java Tutorials Anonymous Inner Classes - Anonymous Inner Classes tutorial Assertion in java
Ads

In this section we will discuss about what is assertion, how to use assert keyword and benefits of assertion in Java. We have also provided an example to help beginners understand the concept of assertion in Java. Assertion are assumption or predictions made at the beginning of the program so that to ensure the program is true, otherwise, it will not reach to that point and the program will be terminated and throw an AssertionException error.

Assertion is used to test assumptions in a program and make it valid by catching exceptions and logical errors. It makes make program more readable and user friendly. It validates data at run-time. Assertion contain a Boolean expression which is set to be true in the beginning of the program.

Assertion are of two types:

preconditions- invoked when a method is invoked
postconditions- invoked when a method is finished

Assertion can also be used in control flow, as invariants of a class.

Implementing Assertion in Java

Assertion is carried out in Java by using assert keyword. It can be expressed in two ways:

assert expression:

This statement specifies that if assertion expression becomes false, an AssertionError is thrown and program is terminated

assert expression1 : expression2 :

This statement means that the expression1 is evaluated first and if it is false then an AssertionError error with an additional message from expression2 is thrown.

Benefits of Assertion In Java:

  • Assertion in Java is used to review the code.
  • It makes debugging easier.
  • It helps to integrate unit testing as Junit testing.
  • It improves the quality of code.

Syntax:

assert statement1;

"statement1" is a Boolean expression, which is checked when program is executed. If it returns true, program runs without interruption. If statement1 returns false,, the program throw AssertionError and program gets terminated.

assert statement1:statement2;

"statement1" is an error message and statement2 is a Boolean expression. statement1 gives an error message if the statement 2 return false, then it is passed to the Assertion Error constructor of the respective error class. The constructor changes the value in statement1 into string format.

Example of Assertion in Java:

Here we have defined a public class 'Mark Assert Demo'. Inside it we have defined the static variable i.e. maximum marks to be 100, changes is another static variable, The main static ( ) method has assumption of maximum marks i.e. 40, if the marks come below to 40, the code will show you java.lang.AssertionError and display the Marks is below than 40 on the command prompt.

public class MarkAssertDemo
{
static float maximummarks=100;
static float changes(float mark)
{
maximummarks=maximummarks-mark;
System.out.println("The maximummark is:" + maximummarks);
return maximummarks;
}
public static void main(String args[])
{
float g;
for(int i=0;i<5;i++)
{
g=changes(15);
assert maximummarks>=40.00:"marks is below 40.";
}
}
}

Output:

C:\saurabh>javac -source 1.4 MarkAssertDemo.java

C:\saurabh>java -ea MarkAssertDemo

The maximummark is:85.0

The maximummark is:70.0

The maximummark is:55.0

The maximummark is:40.0

The maximummark is:25.0

Exception in thread "main" java.lang.AssertionError: marks is below 40.
at MarkAssertDemo.main(MarkAssertDemo.java:16)

Resource:

Advertisements

Ads
Share on Google+Share on Google+

Assert in Java

Posted on: October 8, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Assert in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads