Java advanced JUnit, Annotation
JUnit, Annotation Part 1: JUnit1. Basic Concept: JUnit Software Testing Technology (tools): Build a dedicated user testing package structure in the project. In Junit, you can run a method using the @ Test annotation. 2. Junit Annotations indicate that the @ Test annotation must meet the following conditions:
1) it must be a non-static method without parameters.
2) the class added with @ Test annotation must have a public structure without Parameters
3. JUnit test example 1) after running, you can view the running time and result information in the Junit window.
2) The running result of the tested program appears on the Console. 4. other annotations in JUnit @ BeforeClass ---- run @ AfterClass ------- once After each test method is completed @ Before ----------- run @ After ------------ each test method Before it is completed run the command once. Non-static.Part 2: Annotation (Annotation)1. First, let's understand a concept --------Metadata. 1) metadata is data. That is to say, metadata describes data. Like a field in a data table, each field describes the meaning of the data under this field.
2) metadata can be used to create documents, track code dependencies, and even perform basic compile-time checks. Many metadata tools, such as XDoclet, add these functions to the core Java language and become part of the Java programming function for the moment.
3) In general, the benefits of metadata are divided into three types: Documentation, compiler check, and code analysis. Code-level documents are most often referenced. Metadata provides a useful method to indicate whether a method depends on other methods, whether they are complete, whether a specific class must reference other classes, and so on. 2. What is annotation? 1) The annotation in Java is the metadata of the Java source code, that is, the annotation is used to describe the Java source code. 2) The basic syntax is:@ Is followed by the annotation name.3. pre-defined annotation ------- in Java -------Three predefined annotations in the Java. lang Package1) Override: Specifies whether a method correctly overwrites its parent class.
2) Deprecated: indicates that this class member is no longer recommended. It is a tag annotation.
3) SuppressWarnings: used to suppress warning information. 4. Custom annotation: 1) the simplest custom annotation: 2) use this annotation: public @ interface MyAnno {@ MyAnno} public class UserModel {} 3) add a member for the annotation // Definition
Public @ interface MyAnno {
Public String schoolName (); // The schoolName here must be given when this annotation is used. If this annotation is not given, an error may occur, but you can add one.} // code segment: default Hunan urban College; in this case, if the user does not give a value, the default value is the // string following the default value.
// Use
@ MyAnno (schoolName = Hunan urban College)
Public class UserModel {
} 5. annotation 1) Specify the Target: before learning how to use the Target, you need to know another class. This class is called ElementType, which is actually an example. This enumeration defines different program elements that can be applied by the annotation type. @ Target ({ElementType. TYPE, ElementType. METHOD}) ----- This sentence indicates that the annotation can only be placed on the class and the METHOD. 2) Set persistence Retention: Three annotations are defined in the RetentionPolicy enumeration class, it determines the method in which the Java compiler handles the annotation. @ Retention (RetentionPolicy. SOURCE) ----- comments to be discarded by the compiler @ Retention (RetentionPolicy. class) ----- the compiler records the annotation in the Class file, but the VM does not need to keep the annotation at runtime. @ Retention (RetentionPolicy. RUNTIME) ----- the compiler records the comments in the class file, and the VM retains the comments at RUNTIME, so they can be read in a reflective manner. 3) Add a public document named annotation: by default, annotations will be ignored when a document is automatically generated using javadoc. If you want to include annotations in the document, you must use the annotation as the document annotation. 4) set inheritance Inherited: by default, the annotation of the parent class is not Inherited by the quilt class. To inherit, you must add the Inherited annotation. 6. reflection is required to read the annotation content. Note: To obtain comments using reflection, you must use @ Retention (RetentionPolicy. RUNTIME) for annotations.
Import java. lang. reflect. *; public class TestMyAnno {public static void main (String [] args) throws Exception {Class c = Class. forName ("anno. userModel); boolean flag = c. isAnnotationPresent (MyAnno. class); System. out. println (flag); if (flag) {MyAnno ma = (MyAnno) c. getAnnotation (MyAnno. class); System. out. println (school name: = + ma. schoolName (); // after obtaining the data, you can start processing the data }}}