Today’s sharing started, please give us more advice ~

1.1 Unit Tests

1. What are unit tests?

A: Unit test refers to the test code written by the programmer to verify the expected correctness of the method in his class. Once the unit test code is written, it can be used all the time, which can realize automatic testing to a certain extent. Unit testing is typically done using a framework.

2. What is a framework?

A: Framework is predecessors or some awesome technology companies in the actual combat or r & D design of some excellent design or molding code functions, as a complete technical system issued called the framework

The classic framework for unit testing: Junit

1. What is Junit?

A:

  • Junit is a third-party unit testing framework written in the Java language

  • The Junit framework allows us to easily and quickly test the correctness of our code

2. What are unit tests?

A:

  • Unit: In Java, a class is a unit

  • Unit test: A small piece of code written by a programmer in Junit to test the functionality or business logic of a method in a class

1.2Junit framework usage steps

1. Download the framework

Frameworks are generally in the form of JAR packages, jar packages are in the class file, the class file is the core code we call

2. Test your code directly with Junit

Write Test classes: start with Test and end with the business class name

  • Service name: UserService

  • The test class to test this business class: TestUserService

Write test methods in test classes

  • Naming conventions for Test methods: start with Test and end with the business method name

  • The business method under test: login

  • Test method name: testLogin

Test method Precautions

  • Must be public, no return value, no arguments

  • You must use the @test annotation

1.3Junit Common Notes (4. Xx version)

@test Test method!

@before: Modifies instance methods that are executed once Before each test method is executed

@after: Decorates instance methods that are executed once After each test method execution

@beforeClass: Used to statically modify methods that are executed only once before all test methods

AfterClass: Used to statically modify a method that will be executed only once after all the test methods

1.4 common Junit Annotations (5. Xx version)

Test Test method

@beforeeach: decorates instance methods that are executed once BeforeEach test method is executed

AfterEach: modifies instance methods that are executed once AfterEach test method execution

BeforeAll: used to statically modify methods that are executed only once BeforeAll test methods

AfterAll: used to statically modify methods that are executed only once AfterAll test methods

1.5 reflection

Reflection, annotations, proxies, and generics are advanced Java technologies that must be used in the backend of future frameworks

1. What is reflection?

A: Reflection means that for any class, all the components of that class are directly available “at run time.

  • At runtime, you can get the Constructor object for this class directly.

  • At run time, you can get the member variable object (Field) of this class directly.

  • At run time, you can get the member Method object of this class directly.

2. The core idea and key of reflection is to get: the compiled class file object

Reflection is the bytecode file object of the class at run time: it can then parse all the components of the class

1.6 Reflection Obtains Class objects

Introduction:

Reflection is done by first getting the compiled Class object, the bytecode file, and then getting the full composition of the Class and doing some functional design

Reflection designs a type for all components of a class to represent that object

  • Class: type of the bytecode file

  • Constructor: The type of the Constructor

  • Field: Type of a member variable

  • Method: indicates the Method type

Reflection gets Class objects:

1. The first step in reflection technology is always to get the Class object first: there are three ways to get it

  • The name of the class. The class

  • Through the class object. GetClass () method

  • Class.forname (” Full name of Class “)

public static Class<? > forName(String className)

2.Class methods

  • String getSimpleName(): Gets the class name String: class name

  • String getName(): Get the full class name: package name + class name

1.7 Reflection gets the Constructor Constructor

The reflection Class type fetch constructor provides a number of apis:

Constructor getDeclaredConstructor(Class… parameterTypes)

  • Gets a constructor based on parameter matches, regardless of permission modifiers

Constructor[] getDeclaredConstructors()

  • Gets the constructor for all claims

1.8 Reflection gets the constructor and initializes the object

Reflection gets the Constructor Constructor and then initializes the object through this Constructor

Constructor of the API:

  • T newInstance(Object… Initargs): Creates the object and injects the data needed by the constructor

  • Void setAccessible(true): Changes the access permission. True indicates that the access permission is breached by force. False indicates that the access permission is reserved

You can do this by locating the constructor object of the class

If the constructor object does not have access, it can be opened with: void setAccessible(true)

The constructor can pass T newInstance(Object… Initargs) calls itself, passing in arguments

1.9 Reflection retrieves a Field member variable object

Field getDeclaredField(String name): Obtains the corresponding Field object according to the name of the member variable

Field[] getDeclaredFields(): Retrieves all member variables corresponding to the Field object

1.10 Member variable assignments and values

Field methods: Assign and value to member variables

Void set(Object obj,Object value): Injects some member variable data into an Object

Object GET (Object obj): Gets the value of an Object’s member variable

Void setAccessible(True): Violent reflection, set to allow direct access to properties of private types

Class getType(): Gets the type of the property and returns the Class object

String getName(): Gets the name of the property

1.11 Obtaining Method Method Objects by Reflection

Reflection gets the Method object of the class:

  • Method getDeclaredMethod(String name,Class… args)

Gets the corresponding method object based on the method name and parameter type

  • Method[] getDeclaredMethods()

Gets all member method objects in the class, returning an array

Method execution:

  • Object invoke(Object obj,Object… args)

  • Parameter 1: which object is triggered for method execution

  • Parameter 2: args: the actual parameter v passed when the method is called

1.12 Violent attack on collection generics

— Reflection can break object-oriented encapsulation (violent reflection)

It can also break the constraints of generics

1.13 The Role of reflection

  • You can get all the components of a class at run time and manipulate them

  • Encapsulation can be broken

  • You can break the constraints of generics

  • More important uses: Suitable for high-level Java frameworks, the mainstream frameworks are based on reflection to design some common technical functions

Today’s share has ended, please forgive and give advice!