This is the 7th day of my participation in the August Text Challenge.More challenges in August
Content preview
- Junit unit tests
- reflection
- annotations
Junit unit Tests:
- Test categories:
- Black box testing: Without writing code, give input values to see if the program can output the desired values.
- White-box testing: You need to write code. Focus on the specific execution flow of the program.
- Junit use: white box test
-
Steps:
-
Defining a test class (Test case)
- Advice:
- Test Class name: Name of the tested class Test CalculatorTest
- Package name: xxx.xxx.xx.test cn. Shenli. test
- Advice:
-
Define test methods: can be run independently
- Advice:
- Method name: test Method name of the test testAdd()
- Return value: void
- Parameter list: empty parameter
- Advice:
-
So let’s give this method at sign Test
-
Import junit dependencies
-
-
Judgment result:
- Red: Failure
- Green: Success
- Typically we use assertion operations to process results
- AssertEquals (desired result, result of an operation); assert.assertequals
-
Supplement:
- @Before:
- Decorated methods are executed automatically before testing methods
- @After:
- Decorated methods are executed automatically after the test method is executed
- @Before:
-
Reflection: The soul of frame design
-
Framework: semi-finished software. Software development can be carried out on the basis of the framework, simplifying the coding
-
Reflection: Encapsulating the components of a class into other objects is the mechanism of reflection
- Benefits:
- These objects can be manipulated while the program is running.
- Can be decoupled, improve the scalability of the program.
- Benefits:
-
How to get a Class object:
- Class.forname (” full Class name “) : Loads the bytecode file into memory and returns a Class object
- The class name is defined in the configuration file. Read the file and load the class
- Class: obtained from the class attribute of the class name
- Mostly used for passing parameters
- Object. GetClass () : The getClass() method is defined in the Object class.
- A method of obtaining bytecode for an object
- 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.
- Class.forname (” full Class name “) : Loads the bytecode file into memory and returns a Class object
-
Class object features:
- Access function:
-
Get the member variables
-
Field[] getFields() : Gets all public decorated member variables
-
Field getField(String name) Gets the public decorated member variable of the specified name
-
Field[] getDeclaredFields() retrieves all member variables, regardless of modifiers
-
Field getDeclaredField(String name)
-
-
Get the constructors
-
Constructor<? >[] getConstructors()
-
Constructor getConstructor (class <? >… parameterTypes)
-
Constructor getDeclaredConstructor (class <? >… parameterTypes)
-
Constructor<? >[] getDeclaredConstructors()
-
-
Get member methods:
-
Method[] getMethods()
-
Method getMethod(String name, class <? >… parameterTypes)
-
Method[] getDeclaredMethods()
-
Method getDeclaredMethod(String name, class <? >… parameterTypes)
-
-
Gets the full class name
- String getName()
-
- Access function:
-
Field: member variable
- Operation:
-
Set the value
- void set(Object obj, Object value)
-
Get the value
- get(Object obj)
-
Ignore security checks for access modifier
- SetAccessible (True): Violent reflex
-
- Operation:
-
(4) Constructor
- Create an object:
-
T newInstance(Object… initargs)
-
If you use the null parameter constructor to create an object, the operation can be simplified: newInstance on a Class object
-
- Create an object:
-
Method: Method object
-
Execution method:
- Object invoke(Object obj, Object… args)
-
Obtain method name:
- String getName: Gets the method name
-
-
Case study:
- Requirement: Write a “framework” that can help us create objects of any class and execute any of its methods without changing any code of the class
- Implementation:
- The configuration file
- reflection
- Steps:
- Define the full class name of the object you want to create and the method you want to execute in the configuration file
- Load the read configuration file in the program
- Use reflection to load class files into memory
- Create an object
- Execution method
- Implementation:
- Requirement: Write a “framework” that can help us create objects of any class and execute any of its methods without changing any code of the class
Comments:
-
Concept: Describing a procedure. It’s for the computer
-
Note: Describing a program in words. For programmers
-
Definition: Annotations, also called 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:
- New features after JDK1.5
- Illustrative of procedure
- Use annotation: @ annotation name
-
Code analysis: Analyze code by using annotations identified in the code. Compile check: Enable the compiler to implement basic compile checks by using annotations identified in the code.
-
Predefined annotations in the JDK
- Override: tests whether the method marked by this annotation inherits from the parent class (interface)
- Deprecated: Indicates the Deprecated content of this annotation
- @suppresswarnings: SuppressWarnings
- General pass parameter all@suppressWarnings (“all”)
-
Custom annotations
-
Format: meta annotation public @interface annotation name {attribute list; }
-
Essence: An Annotation is essentially an interface that inherits the Annotation interface by default
- public interface MyAnno extends java.lang.annotation.Annotation {}
-
Properties: Abstract methods in an interface
- Requirements:
-
The return value type of the property has the following values
- Basic data types
- String
- The enumeration
- annotations
- An array of the above types
-
Defines attributes that need to be assigned when used
- If the default keyword is used to define the default initialization value of an attribute, the attribute assignment can be omitted when annotations are used.
- If only one attribute needs to be assigned and the attribute name is value, the value can be omitted and the value can be defined directly.
- When an array is assigned, the value is wrapped in {}. If there is only one value in the array, {} can be omitted
-
- Requirements:
-
Meta-annotation: a annotation used to describe the annotation
- @target: Describes where the annotation can be used
- ElementType values:
- TYPE: applies to classes
- METHOD: Applies to methods
- FIELD: Can be applied to member variables
- ElementType values:
- @Retention: Describes the phase in which annotations are retained
- @Retention(retentionPolicy.runtime) : The currently described annotations are retained in the class bytecode file and read by the JVM
- Documented: Describe whether an annotation is extracted into an API document
- Inherited: Describes whether an annotation is Inherited by a child class
- @target: Describes where the annotation can be used
-
-
Use (parse) annotations in the program: Get the values of the attributes defined in the annotations
-
Gets the object (Class, Method,Field) where the annotation is defined
-
Gets the specified annotation
- getAnnotation(Class)
// A subclass of the annotation interface is generated in memory
public class ProImpl implements Pro{ public String className(a){ return "cn.itcast.annotation.Demo1"; } public String methodName(a){ return "show"; }}Copy the code
-
Call the abstract method in the annotation to get the configured property values
-
-
Example: A simple testing framework
-
Summary:
- Most of the time, we will use annotations rather than custom annotations
- Who are the notes for?
- The compiler
- For the parser
- Annotations are not part of the program, and can be interpreted as a tag