Continuous Test Orchestration And Execution Platform Online

Perform automated and live-interactive testing on 3000+ real desktop and mobile devices online.

What is an assertion in Java?

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.

  • assert expression;
  • assert expression1 : expression2

Java disables assertions by default. To enable them, use either of the following commands.

java -ea DemoTest


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.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 Testing

Earn resume-worthy Selenium certifications that help you land a top job.

Learn More

Test your web or mobile apps

Test orchestration and execution cloud of 3000+ browsers and OS


24/7 support


Enterprise grade security


Fastest test execution cloud