# # Today’s content
1. Junit unit test 2. Reflection 3Copy the code

Junit unit Tests:

1. Black box test: no need to write code, give the input value, see if the program can output the expected value. 2. White box testing: You need to write code. Focus on the specific execution flow of the program. * Junit Use: White box Test * Steps: 1. Define a Test class (Test case) * Suggestion: * Test class name: Name of the class to be tested Test CalculatorTest * Package name: XXX.XXX.xx.test cn.itcast.test 2. Define test method: can run independently * Suggestion: * Method name: test Test method name testAdd() * Return value: void * Parameter list: empty parameter 3. Add @test to method 4. Import junit dependency environment * Determination result: * red: failed * Green: successful * Normally we use assertion action to handle result * assert. assertEquals; * Add: * @before: * Modified methods are executed automatically Before test methods * @after: * Modified methods are executed automatically After test methods are executedCopy the code

Reflection: The soul of frame design

* Framework: semi-finished software. * Reflection: Encapsulates the various components of a class into other objects. This is the reflection mechanism * Benefits: 1. These objects can be manipulated while the program is running. 2. Can be decoupled, improve the scalability of the program. 1. Class.forname (" full Class name ") : load the bytecode file into memory, return the Class object * for configuration files, define the Class name in the configuration file. Class: get * from the class name attribute * for passing parameters 3. Object. GetClass () : The getClass() method is defined in the Object class. Conclusion: The same bytecode file (*.class) is loaded only once during a program run, and the class object is the same regardless of which way it is retrieved. 1. Get member variables * Field[] getFields() : Field getField(String name) getDeclaredFields() getDeclaredFields() getDeclaredFields() getDeclaredFields() getDeclaredFields() Field getDeclaredField(String name) 2. Get constructors * Constructor<? >[] getConstructors() * Constructor<T> getConstructor(class <? >... ParameterTypes * Constructor<T> getDeclaredConstructor(class <? >... parameterTypes) * Constructor<? >[] getDeclare dConstructors() 3. GetMethod (String name, class <? >... ParameterTypes) * Method[] getDeclaredMethods() * Method getDeclaredMethod(String name, class <? >... ParameterTypes = String getName() * Field: member variable * * void set(Object obj, Object value) 2. * get(Object obj) 3. Ignore the security check of the access modifier * setAccessible(true): violence reflection * Constructor: creating an Object: * T newInstance(Object... Initargs) * If you use the null argument constructor to create an Object, the operation can be simplified: Class Object newInstance Method * Method: Method Object * Execution Method: * Object invoke(Object obj, Object... Args) * get method name: * String getName: get method name * Case: * Requirement: Write a "framework", can not change any code in the context of the class, can help us to create any object, and execute any of the methods * implementation: 1. Configuration file 2. Reflection * Steps: 1. Define the full class name of the object to be created and the method to be executed in the configuration file. 3. Use reflection technology to load class files into memory 4. Create an object 5. Execute a methodCopy the code

Comments:

* Concept: describes the program. Note: Used to describe a program in words. * Definition for programmers: Annotations, also known as metadata. A code level specification. It is a feature introduced in JDK1.5 and later and is on the same level as classes, interfaces, and enumerations. It can be declared in front of packages, classes, fields, methods, local variables, method parameters, etc., used to describe and comment these elements. * concept description: * JDK1.5 after the new features of * * use that program comments: @ annotation name * function classification: (1) document: through code identification annotations in the document (2) code generated document doc document 】 【 analysis: through the code identification of annotation to analyze code (3) compiled using reflection 】 【 check: Override: Checks whether the method marked by the annotation inherits from the parent class (interface) * @deprecated: * @suppresswarnings: SuppressWarnings * general pass parameter all @suppresswarnings ("all") * custom annotations * format: Meta annotation public @interface Annotation name {attribute list; } * essence: Annotation is essentially an interface, the interface inheritance Annotation interface by default * public interface MyAnno extends Java lang. The Annotation. The Annotation {} * attributes: Abstract methods in interfaces * requirements: 1. Property returns the following types of values * Basic data type * String * enumeration * Annotation * array of the above type 2. 1. If the default keyword is used to initialize the default value of an attribute when defining the attribute, you do not need to assign the attribute when using annotations. 2. If only one attribute needs to be assigned and its name is value, the value can be omitted and the value can be directly defined. 3. Array assignments are wrapped with {}. If there is only one value in the array, {} may omit * meta annotation: annotation used to describe the annotation * @target: describes where the annotation can be used * ElementType Values: * TYPE: can be used on classes * METHOD: can be used on methods * FIELD: * @retention: describes the phase in which annotations are retained * @retention (retentionPolicy.runtime) : Documented: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited: Inherited Get the object (Class, Method,Field) where the annotation is defined. Public Class ProImpl implements Pro{public String * getAnnotation(Class) // Implements a subclass of the annotation interface in memory className(){ return "cn.itcast.annotation.Demo1"; } public String methodName(){ return "show"; }} 3. Call the abstract method in the annotation to get the configured attribute value * Example: Simple testing framework * Summary: 1. Most of the time, we will use annotations instead of custom annotations. 2. 3. Annotations are not part of the program. We can interpret annotations as tagsCopy the code