Java

How to Test Your Program in Java Using Assert4 min read

In Java, the assert keyword is used to test if a given boolean expression is true or false. If the expression is true, the program continues to execute normally. If the expression is false, an AssertionError is thrown.

To use assert, you can write an expression followed by a semicolon, like this:




You can also include a message to be displayed when the assertion fails:

For example:

You can also use multiple assertions in a program.

Note that assertions are not enabled by default in Java. To enable them, you can use the -ea command line option when you run your program.

It is generally recommended to use assertions only for testing and debugging and to not rely on them in production code.

Creating and Using Assertions:

Assertions can be created using the assert keyword. The general form of using assert keyword is as follows:

assert condition;

In this example, the first assertion passes because the value of x is greater than 0. The second assertion fails, and an AssertionError is thrown with the default error message “assertion failed”.

You can also include a message to be displayed when the assertion fails:

It is important to note that assertions are not enabled by default in Java. To enable them, you can use the -ea command line option when you run your program.

It is generally recommended to use assertions only for testing and debugging and to not rely on them in production code.

When the given condition becomes falseAssertionError is thrown by the Java run-time. The second form of assert is as follows:

assert condition : expr;

The assert statement in Java allows you to test a boolean condition and, if the condition is false, the program will throw an AssertionError. The assert statement takes the following form:

Where condition is a boolean expression that you want to test, and expression is a value or an expression that will be used as the error message.

For example:

In this example, the assertion passes because the value of x is greater than 0.

If the condition is false, an AssertionError is thrown with the given error message:

The expression that appears after the colon is optional, if it is not provided and the condition is false the AssertionError will be thrown without any message.

It is important to note that assertions are not enabled by default in Java. To enable them, you can use the -ea command line option when you run your program.

It is generally recommended to use assertions only for testing and debugging and to not rely on them in production code.

Enabling and Disabling Assertions:

For enabling them we have to use the following syntax while executing a Java program:

java -ea ClassName

Where -ea denotes enable assertion and similarly for disabling them we can use the following syntax:

java -da ClassName

Where -da denotes disable assertion.

For enabling or disabling assertion in a package we can use the following syntax:

java [-ea | -da] [:package-name… | :ClassName]

Example Program:

Let’s consider a Java program where the numbers entered by the user must not be negative values. To implement this we can use assert keyword as follows:

If the n value is given as -9, then output of the above program is:

As the above error message does not give much information we can use second form of assert as shown in the below program:

Now the output of the above program for -9 as n value is:

Take your time to comment on this article.

Leave a Comment