JAVA assertion - Assert

Brief introduction

Assertions are using Java in the assertstatement to achieve. When performing assertion, it is considered to be correct. If it fails, JVM will throw a called AssertionErrorwrong. It is mainly used for testing purposes during development.

assert Statement is used in conjunction with Boolean expressions can be written in two different ways:

assert expression;
assert expression1 : expression2;
复制代码

Examples

public class AssertTest {
    public static void main(String[] args) {
    
        int value = 15;
        assert value >= 20 : " Underweight";
        System.out.println("value is " + value);
    }
}
复制代码

Output:

value is 15
复制代码

We can see assertthe statement did not work, because when the Java program execution is not started by default assertion checking that all assert statements are ignored.

If you want to turn assertion checking, you need to use -enableassertionsor -eaJVM parameters to open; if you want to manually ignore the assertion checking, you can use -disableassertionsor -daJVM parameters to ignore the assertion statement.

Execute the command java -ea AssertTestoutput after opening assertion:

Exception in thread "main" java.lang.AssertionError:  Underweight
	at AssertTest.main(AssertTest.java:11)
复制代码

Using the first syntax error message prompted to write:

public class AssertTest {
    public static void main(String[] args) {

        int value = 15;
        assert value >= 20;
        System.out.println("value is " + value);
    }
}
复制代码

Output:

Exception in thread "main" java.lang.AssertionError
	at AssertTest.main(AssertTest.java:11)
复制代码

Why assertion

  1. Ensure that the code does not seem to actually reach the unreachable
  2. Ensure the correct assumption when
  3. To ensure that switch casethe defaultbranch can not reach
  4. Check the status of objects
  5. Began to test the method of
  6. After testing method call

Precautions

Assertion mainly used when the checking logic impossible. For example, they can be used to check the code of the desired operating state or the state before the start of the code after the end of the run. And general exception / error handling different assertions usually disabled at runtime.

Where the use of assertions

  1. Parameter private methods. Private parameters provided only by the code developers, developers may want to check their assumptions about the parameters
  2. Branch conditions, for example, switchthe statement
  3. Conditions at the beginning of the method

Where not to use assertions

  1. Assertion should not be used to replace the error / exception messages
  2. Assertions do not apply to publicthe parameters of the process, as they may be provided by the user
  3. Assertion should not be used on the command line parameter

summary

  1. Assertion is a debug mode, an assertion failure will throw AssertionErroronly in the development and testing phase enable assertions
  2. Recoverable errors assertion can not be used, but should throw an exception
  3. JAVA asserts rarely used, a better approach is to write unit tests


references:

~~~~~~~~GeeksforGeeks - Assertions in Java

~~~~~~~~Liao Xuefeng JAVA Tutorial - Using assertions

Guess you like

Origin juejin.im/post/5e0eaa0de51d4540e47ca159