Assertion
On the internet to find a lot of information on the assertion of the blog Knowledge Point Summary, coupled with the Java Core Technology book content, organized the study blog
This blog is very detailed, see http://blog.csdn.net/silentbalanceyh/article/details/4564884 content
1, the concept of assertions
The assertion mechanism allows you to insert some survey statements into your code during testing. These plug-in detection statements are automatically removed when the code is released.
Assertion (assertion) is a new feature introduced by Java1.4 , the purpose of this feature is to assist the developers debugging and testing , is a more common debugging, testing scheme.
Assertion is a kind of common debugging method in the process of software development, and not only that, the use of assertion can prove the correctness of the program during the development process, but this usage will have a great challenge to the overall design of the system.
, and is seldom put into practical use, so the purpose of using assertion in general is to debug and test .
The Java language introduces the keyword assert.
There are two forms of this keyword:
assert conditions;
Assert condition: an expression;
Both of these forms detect the condition and throw a Assertionerror exception if the result is false.
• In the second form, the expression will be assertionerror to the constructor of the descendant and converted to a message string.
Example:
1 /**2 * Assertion using the concept description code3 **/4 Public classAssertiondriver {5 Public Static voidMain (String args[]) {6Employee Employee =NewEmployee ();7Employee.setname ("Tian Lang");8Employee.setemail ("[Email protected]");9 businessprocess (employee);Ten } One A Public Static voidbusinessprocess (Employee employee) { - Try{ - assertEmployee.getname ()! =NULL&& theEmployee.getemail ()! =NULL&& -Employee.getpassword ()! =NULL: - employee; -}Catch(assertionerror error) { + System.out.println (error); - } + } A } at - classemployee{ - PrivateString name; - PrivateString Email; - PrivateString password; - PublicString GetName () { in returnname; - } to Public voidsetName (String name) { + This. Name =name; - } the PublicString Getemail () { * returnemail; $ }Panax Notoginseng Public voidsetemail (String email) { - This. email =email; the } + PublicString GetPassword () { A returnpassword; the } + Public voidSetPassword (String password) { - This. Password =password; $ } $ @Override - PublicString toString () { - return"/nname:" + name + "/n" + "email:" + email + "/n" + "Password:" +password; the } -}
The upper snippet uses the second expression, but the complex here is that the expression 2 is not a string , but an instance of an object defined by the employee class, which means that expression 2 is an object instance . Then compile the code, run the time to open the assertion , you can get the output below:
Java.lang.AssertionError:Name:Tian langemail:2433758932@qq. Compassword:null
2. How to use assertions in Eclipse (i) First clear:
Java assertion assert is introduced by jdk1.4.
The JVM assertion is turned off by default.
Assertions apply only to complex debugging procedures.
Assertions are generally used to judge the results of program execution, and do not let assertions handle business processes.
(ii) Determine if Eclipse has an assertion open,
The code is as follows:
1 Public classAsserttest {2 3 Public Static voidMain (string[] args) {4 BooleanIsOpen =false; 5 assertisopen=true;//If an assertion is turned on, the value of IsOpen is changed to True6System.out.println (IsOpen);//Print whether the assertion is turned on7 }8}
Execute the above code if the print true indicates that the assertion has been enabled, and if False the assertion is not enabled.
If the assertion is not enabled, press to open the assertion as follows.
(iii) Open the assertion selection menu in Eclipse:
Run---> Run configurations ...---> select Arguments tab
In the VM arguments text box, enter:-ea Note: There is no space in the middle, if the input-da indicates a forbidden assertion.
Then close the window, and then save it to open the assertion.
3, about the assertion of thinking The advent of the Java assertion mechanism, whose main purpose is to assistdebugging, testing, and development in programmer developmentSuch operations, such as, in the formal business process can not be abused, with bad may lead to serious problems, there are three points need to explain: "1" Please remember that developers,assertion is off by default, all the main Ides, including Eclipse and NetBeans, do not open assertions when debugging code, and if you want to open assertions, you need to make the relevant settings, so when you add assert in the code, you must remember to open the assertion function when looking for key aspects of the system. "2"You cannot use assertions to replace normal business logic: This is the case in contrast to assert and if statements, both of which have intrinsic conceptual differences in the process of program development. If the most typical explanation is that the condition is fulfilled and the condition is not fulfilled, and the assert is generally placed in the critical position of the system, assert is to ensure that the conditional expression is correct, and if the system has an error incorrectly, the two are conceptually inherently different, so the developer must You must remember that you cannot use Assert to replace the original if statement. Also, in the case of release of the final code, assertions are generally closed, and if such an alternative is used, it means that the process of non-release and release versions is not the same as in industrial production. The presence of the "3" assert means that if the system appears assertionerror in this case, it proves that the system has already had an unrecoverable error, so assert cannot be used for formal business logic and can only be used for development and debugging, which cannot be Recovery errors in industrial production and engineering projects can lead to serious problems, so it is important to carefully consider the use and location of the assert.
Added, specific assertions can be viewed at the beginning of the blog in the directory, more detailed specific!
Java Fundamentals-Assertions