Perform automated and live-interactive testing on 3000+ real desktop and mobile devices online.
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);
}
}
Test your websites, web-apps, or mobile apps seamlessly with LambdaTest.
Start Free TestingEarn resume-worthy Selenium certifications that help you land a top job.
Learn MoreTest orchestration and execution cloud of 3000+ browsers and OS
24/7 support
Enterprise grade security
Fastest test execution cloud