Enable assertion in java

Java assert keyword example

The assert keyword is used in assertion statement which is a feature of the Java programming language since Java 1.4. Assertion enables developers to test assumptions in their programs as a way to defect and fix bugs.

1. Syntax of assert statement

assert expression1;

assertexpression1 : expression2;

  • expression1 must be a boolean expression.
  • expression2 must return a value (must not return void).
    • If assertion is enabled, then the assert statement will be evaluated. Otherwise, it does not get executed.
    • If expression1 is evaluated to false , an AssertionError error is thrown which causes the program stops immediately. And depending on existence of expression2:
        • If expression2 does not exist, then the AssertionError is thrown with no detail error message.
        • If expression2 does exist, then a String representation of expression2’s return value is used as detail error message.
          • If expression1 is evaluate to true , then the program continues normally.

          2. Enable assertion in Java

          By default, assertion is disabled at runtime. To enable assertion, specify the switch –enableassertions or — ea at command line of java program. For example, to enable assertion for the program called CarManager :

          java –enableassertions CarManager

          java –ea CarManager

          Assertion can be enabled or disable specifically for named classes or packages. For more information on how to enable and disable assertion, read this article.

          3. Java Assertion examples

          public class AssertionExample < public static void main(String[] args) < // get a number in the first argument int number = Integer.parseInt(args[0]); assert number 10 System.out.println("Pass"); > >

          java -ea AssertionExample 15

          A java.lang.AssertionError error will be thrown:

          Exception in thread «main» java.lang.AssertionError

          But the program will continue and print out “Pass” if we pass a number less than 10, in this command:

          java -ea AssertionExample 8

          And the following example is using the full version of assert statement:

          public class AssertionExample2 < public static void main(String[] args) < int argCount = args.length; assert argCount == 5 : "The number of arguments must be 5"; System.out.println("OK"); >>

          java -ea AssertionExample2 1 2 3 4

          Exception in thread «main» java.lang.AssertionError: The number of arguments must be 5

          Generally, assertion is enabled during development time to defect and fix bugs, and is disabled at deployment or production to increase performance.

          About the Author:

          Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.

          Add comment

          Comments

          Hello Nam Ha Minh,
          This was helpful. Thank you. You really mean the word «detect» instead of «defect», I think.
          Carol Clark

          Источник

          Java — how to enable assertions?

          Root-ssh

          In this article we whould like to show how to enable assertions in Java.

          The article describle 2 ways how to do it:

          Read below sections to see details.

          1. Java with command line example

          Running program from console it is necessary to add -ea or -enableassertions paramter for java virtual machine to use assertions.

          1.1. Compilation form console:

          1.2. Running from console:

          $ java -enableassertions Program
          Exception in thread "main" java.lang.AssertionError: Variable can not be null at Program.main(Program.java:12)

          2. IntelliJ IDEA compilation example

          IntelliJ requires to add in Run/Debug Configurations window, -ea or -enableassertions paramter VM Options field.

          2.1. Step 1 — create Java project

          Java project with IntelliJ IDEA

          2.2. Step 2 — open Run / Debug configurations

          Opening Run / Debug configurations with IntelliJ IDEA

          2.3. Step 3 — add -ea parameter for VM options

          Enable assertions parameter for IntelliJ IDEA (-ea or -enableassertions paramter)

          2.4. Step 4 — running program

          Running program with enabled assertions with IntelliJ IDEA

          Alternative titles

          Источник

          AVAJAVA Web Tutorials

          Assertions can be used in JavaSW to test for the validity of certain conditions in a Java application. If a particular assertion condition is found to be false, an AssertionError will be thrown. Notice that this is an Error and not an Exception. Errors are unchecked, so you don’t need to catch them. Typically with assertions, you don’t catch them and you let the application terminate as a result of throwing the AssertionError. However, the AssertTest class catches the AssertionError.

          AssertTest.java

          package test; public class AssertTest < public static void main(String[] args) < try < System.out.println("testing. "); assert true : "Condition is true, so we won't see this"; assert false : "If assertions are on, we'll see this"; > catch (AssertionError e) < e.printStackTrace(); >> >

          Normally, assertions are off when we have the Java interpreter execute a class. So, the assert code is basically ignored when we execute a class normally:

          java test.AssertTest

          C:\projects\workspace\testing\bin>java test.AssertTest testing.

          Assertions can be enabled by including the -ea option (or -enableassertions option) to the Java interpreter. If we include this option when we execute AssertTest, we see the following:

          java -ea test.AssertTest

          C:\projects\workspace\testing\bin>java -ea test.AssertTest testing. java.lang.AssertionError: If assertions are on, we'll see this at test.AssertTest.main(AssertTest.java:9)

          Let’s look at a couple more examples. To do this, I’ll use the Assert2Test class and the Assert3Test class.

          Assert2Test.java

          package test; import test.three.Assert3Test; public class Assert2Test < public static void main(String[] args) < assert false : "Message from Assert2Test"; Assert3Test.howdy(); > >

          Assert3Test.java

          package test.three; public class Assert3Test < public static void howdy() < assert false : "Message from Assert3Test"; > >

          We can enable assertions for a package. Here, we say to enable assertions for the test package by using the -ea option followed by a colon followed by the name of the package followed by ellipses (. ). This enables assertions for all classes in that package and all classes in all subpackages of test. We can see that we first hit the assertion in Assert2Test, and we exit.

          java -ea:test. test.Assert2Test

          C:\projects\workspace\testing\bin>java -ea:test. test.Assert2Test Exception in thread "main" java.lang.AssertionError: Message from Assert2Test at test.Assert2Test.main(Assert2Test.java:8)

          Next, I’ll enable assertions in the test.three package. Since assertions are by default disabled in the test package, we don’t hit the assertion in test.Assert2Test. However, we do hit the assertion in test.three.Assert3Test.

          java -ea:test.three. test.Assert2Test

          C:\projects\workspace\testing\bin>java -ea:test.three. test.Assert2Test Exception in thread "main" java.lang.AssertionError: Message from Assert3Test at test.three.Assert3Test.howdy(Assert3Test.java:6) at test.Assert2Test.main(Assert2Test.java:9)

          It’s possible to disable assertions for a particular package by using the same ellipses form above but with the -da (or -disableassertions) option. This can be useful if they’ve been enabled for a particular package, but you want them disabled for a particular subpackage of that package.

          We can also enable assertions for a particular class. This is done by specifying -ea followed by a colon followed by the fully qualified class name.

          java -ea:test.three.Assert3Test test.Assert2Test

          C:\projects\workspace\testing\bin>java -ea:test.three.Assert3Test test.Assert2Test Exception in thread "main" java.lang.AssertionError: Message from Assert3Test at test.three.Assert3Test.howdy(Assert3Test.java:5) at test.Assert2Test.main(Assert2Test.java:8)

          Most Java web developers that I know don’t use assertions, but it’s good to know that they exist. They probably are useful in particular fields of programming.

          Источник

          Читайте также:  List html li width
Оцените статью