Next-Gen App & Browser
Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles
An assertion is a statement in Java to test your assumptions about the software. It ensures that any assumptions made in the software are correct. While running an assertion, it is assumed to be true. If the assertion fails, the JVM will throw an error referred to as AssertionError.
Assertion mainly helps in testing purposes. Its statements are used along with boolean expressions. It is an effective way to identify and fix programming errors. You can run assertions in Java using the assert keyword.
Following are the ways to use an assert statement.
Java disables assertions by default. To enable them, use either of the following commands.
java -ea DemoTest
or
java -enableassertions DemoTest
Following is an example of an Assertion in Java.
import java.util.Scanner;
class AssertionDemo{
public static void main( String args[] ){
Scanner scanner = new Scanner( System.in );
System.out.print("Please enter your age ");
int val = scanner.nextInt();
assert val>=20:" Invalid";
System.out.println("Your age is "+val);
}
}
KaneAI - Testing Assistant
World’s first AI-Native E2E testing agent.