CN113448853A - Unit testing method, unit testing device, testing equipment and storage medium - Google Patents

Unit testing method, unit testing device, testing equipment and storage medium Download PDF

Info

Publication number
CN113448853A
CN113448853A CN202110733564.6A CN202110733564A CN113448853A CN 113448853 A CN113448853 A CN 113448853A CN 202110733564 A CN202110733564 A CN 202110733564A CN 113448853 A CN113448853 A CN 113448853A
Authority
CN
China
Prior art keywords
test
bean
definition
simulated
original
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202110733564.6A
Other languages
Chinese (zh)
Inventor
欧燊
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Dongguan Dayi Industry Chain Service Co ltd
Original Assignee
Dongguan Dayi Industry Chain Service Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Dongguan Dayi Industry Chain Service Co ltd filed Critical Dongguan Dayi Industry Chain Service Co ltd
Priority to CN202110733564.6A priority Critical patent/CN113448853A/en
Publication of CN113448853A publication Critical patent/CN113448853A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3684Test management for test design, e.g. generating new test cases
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3688Test management for test execution, e.g. scheduling of test suites

Abstract

The embodiment of the invention discloses a unit testing method, a unit testing device and a storage medium. The method comprises the following steps: determining a simulation object feature set according to annotation configuration of a test class of a unit to be tested; traversing the original object definition registered by Spring according to the simulation object feature set to determine the original object definition required to be simulated; for the original object definition to be simulated, creating a corresponding simulated object definition, and replacing the original object definition to be simulated with the simulated object definition; and injecting the example defined by the simulation object into the test class through Spring so as to test the unit to be tested. According to the technical scheme, before the test method is executed, the original object conforming to the characteristics of the simulation object is assigned with dependence, and the instance creation and calling of the real dependence object are replaced, so that the Mock can be written and executed conveniently in the test process, and the support and the expansibility of injecting the simulation object instance are improved.

Description

Unit testing method, unit testing device, testing equipment and storage medium
Technical Field
The embodiment of the invention relates to the technical field of testing, in particular to a unit testing method, a unit testing device and a storage medium.
Background
Unit Testing (Unit Testing) refers to checking and verifying the smallest Unit of testability in software. The Spring framework is an application program framework of open source code and is a lightweight container for managing the life cycle of an object (Bean) in the unit testing process. The Spring Boot further simplifies the whole building and developing process of the Spring application by simplifying the configuration. Mockito support in Spring-Boot-Test is used for realizing dependent injection of a simulation (Mock) object in a Spring container. The prior art has poor support and expansibility for Mock object injection, and cannot be applied to object (Bean) definitions (such as Dubbo) directly registered by some frameworks in Java runtime.
Disclosure of Invention
The embodiment of the invention provides a unit testing method, a unit testing device and a storage medium, which are used for simplifying the simulation injection process of testing and improving the support and expansibility for injecting a simulation object instance.
In a first aspect, an embodiment of the present invention provides a unit testing method, including:
determining a simulation object feature set according to annotation configuration of a test class of a unit to be tested;
traversing the original object definition registered by Spring according to the simulation object feature set to determine the original object definition required to be simulated;
for the original object definition to be simulated, creating a corresponding simulated object definition, and replacing the original object definition to be simulated with the simulated object definition;
and injecting the example defined by the simulation object into the test class through Spring so as to test the unit to be tested.
Further, the method further comprises:
for the original object definition which does not need to be simulated, creating a corresponding object instance;
and injecting each object instance into the test class through Spring so as to test the unit to be tested.
Further, before injecting each of the object instances into the test class by Spring, the method further includes:
determining a monitoring object feature set according to annotation configuration of a test class of a unit to be tested;
determining an object instance to be monitored according to the monitored object feature set;
and returning a monitoring proxy object for the object instance needing monitoring.
Further, returning a monitoring proxy object for the object instance needing monitoring, including:
for the object instance needing to be monitored, acquiring a target class type according to an object type;
and creating a monitoring proxy object based on the target class type, wherein the monitoring proxy object refers to the object instance needing to be monitored.
Further, for the object instance to be monitored, obtaining the target class type according to the object type includes:
if the object instance to be monitored is proxied by a Code Generation Library (CGLIB), taking the original object type to which the object type of the object instance to be monitored belongs as the target class type;
and if the object instance to be monitored is a JDK (Java Development kit) dynamic proxy object, taking the original object type of the object instance to be monitored as the target class type.
Further, before traversing the original object definition registered by Spring according to the simulated object feature set, the method further includes:
through a Spring definition object factory post-processing module, an original object definition is created and registered based on an Inversion of Control (IOC) mechanism during initialization.
In a second aspect, an embodiment of the present invention provides a unit testing apparatus, including:
the test configuration module is used for determining a simulation object feature set according to the annotation configuration of the test class of the unit to be tested;
the simulation post-processing module is used for traversing the original object definition registered by Spring according to the simulation object feature set so as to determine the original object definition to be simulated; for the original object definition to be simulated, creating a corresponding simulated object definition, and replacing the original object definition to be simulated with the simulated object definition;
and the injection module is used for injecting the example defined by the simulation object into the test class through Spring so that the test module can test the unit to be tested.
Further, the apparatus further comprises:
the monitoring post-processing module is used for establishing a corresponding object instance for the original object definition which does not need to be simulated;
the injection module is further configured to inject each object instance into the test class through Spring, so as to test the unit to be tested.
In a third aspect, an embodiment of the present invention provides a test apparatus, including:
one or more processors;
storage means for storing one or more programs;
when executed by the one or more processors, cause the one or more processors to implement the unit testing method of the first aspect.
In a fourth aspect, the present invention further provides a computer-readable storage medium, on which a computer program is stored, which when executed by a processor implements the unit testing method according to the first aspect.
The embodiment of the invention provides a unit testing method, a unit testing device, testing equipment and a storage medium, wherein the method comprises the following steps: determining a simulation object feature set according to annotation configuration of a test class of a unit to be tested; traversing the original object definition registered by Spring according to the simulation object feature set to determine the original object definition required to be simulated; for the original object definition to be simulated, creating a corresponding simulated object definition, and replacing the original object definition to be simulated with the simulated object definition; and injecting the example defined by the simulation object into the test class through Spring so as to test the unit to be tested. According to the technical scheme, before the test method is executed, the original object conforming to the characteristics of the simulation object is assigned with dependence, and the instance creation and calling of the real dependence object are replaced, so that the Mock can be written and executed conveniently in the test process, and the support and the expansibility of injecting the simulation object instance are improved.
Drawings
FIG. 1 is a flowchart of a unit testing method according to an embodiment of the present invention;
FIG. 2 is a flowchart of a unit testing method according to a second embodiment of the present invention;
fig. 3 is a schematic diagram illustrating an implementation of a unit testing method according to a second embodiment of the present invention;
fig. 4 is a schematic structural diagram of a unit testing apparatus according to a third embodiment of the present invention;
fig. 5 is a schematic diagram of a hardware structure of a testing apparatus according to a fourth embodiment of the present invention;
Detailed Description
The present invention will be described in further detail with reference to the accompanying drawings and examples. It is to be understood that the specific embodiments described herein are merely illustrative of the invention and are not limiting of the invention. In addition, the embodiments and features of the embodiments in the present invention may be combined with each other without conflict. It should be further noted that, for the convenience of description, only some of the structures related to the present invention are shown in the drawings, not all of the structures.
Before discussing exemplary embodiments in more detail, it should be noted that some exemplary embodiments are described as processes or methods depicted as flowcharts. Although a flowchart may describe the steps as a sequential process, many of the steps can be performed in parallel, concurrently or simultaneously. In addition, the order of the steps may be rearranged. The process may be terminated when its operations are completed, but may have additional steps not included in the figure. The processes may correspond to methods, functions, procedures, subroutines, and the like.
It should be noted that the terms "first", "second", and the like in the embodiments of the present invention are only used for distinguishing different apparatuses, modules, units, or other objects, and are not used for limiting the order or interdependence relationship of the functions performed by these apparatuses, modules, units, or other objects.
For a better understanding of embodiments of the present invention, the following description is made with respect to terms.
Unit test (Unit Testing): refers to the inspection and verification of the smallest measurable unit in the software.
Spring Boot: a Java application framework.
Subject (Bean): among springs, an object that constitutes an application backbone and is managed by a Spring Control Inversion (IOC) container is called Bean. A Bean is an object instantiated, assembled, and managed by a Spring IOC container. The IOC container may create, assemble, and manage beans according to configuration information in an (Extensible Markup Language, XML) file, Java annotation, or Java config file, and Spring may directly call the functions of these beans by just declaring the required classes.
Mock object (Mock Bean): also referred to as mock objects, refer to fake objects that simulate real object behavior in a controlled manner. Users can often test other objects by creating a simulated object instead of a real object.
Monitored object (Spy Bean): a special Mock Bean, unlike a normal Mock, holds a reference to a real object, by default, to invoke a method of the real object. Spy refers to monitoring. Both Mock and Spy can be used to intercept objects and methods that have not been implemented or are not expected to be actually called, and set custom behavior for them; the difference between the two is that: the object declared by Mock, the call to the function all executes Mock (i.e., a dummy function), and does not execute the real part. The objects of the Spy declaration, the calls to the functions, all perform the real part.
Bean Definition (Bean Definition): and (4) description of the Bean instance by Spring, such as whether the Bean instance is singleton or not, a construction method, contained attributes and the like.
Control Inversion (IOC): the IOC is the core of a Spring framework and can be used for realizing a loose coupling relationship between objects and avoiding the problems of difficult development and maintenance and the like caused by too-dense coupling between the objects. IOC may also refer to handing over instantiation of a class, instantiation of dependencies, and introduction of dependencies, etc., all controlled by a Spring Bean container.
Example one
Fig. 1 is a flowchart of a unit testing method according to an embodiment of the present invention, which is applicable to a unit testing situation. In particular, the unit testing method may be performed by a unit testing apparatus, which may be implemented in software and/or hardware and integrated in a testing device. Further, the test equipment includes, but is not limited to: desktop computers, notebook computers, servers, industrial personal computers and other electronic equipment.
As shown in fig. 1, the method specifically includes the following steps:
s110, determining a simulation object feature set according to the annotation configuration of the test class of the unit to be tested.
Wherein a test class may refer to a class used to perform a test in a unit under test. Annotating a configuration may refer to characterizing a Bean in a test class and correspondingly configuring the Bean with a corresponding characterization. Features of beans in the test class can be determined according to the annotation configuration, for example, in this embodiment, the annotation configuration is added and Bean features to be simulated are extracted from the annotation configuration, and the features form a Mock Bean feature set.
And S120, traversing the original object definition registered by Spring according to the simulation object feature set to determine the original object definition required to be simulated.
The Bean definition may refer to a Bean definition object scanned and created by a Bean factory post-processing module inside Spring when Spring creates a Bean instance through an IOC mechanism. An original Bean definition may refer to a collection of Bean definitions created by Spring initial scan. And screening the original Bean definitions registered by Spring according to the Mock Bean feature set, so that the original Bean definitions needing Mock can be screened out.
S130, for the original object definition to be simulated, creating a corresponding simulated object definition, and replacing the original Bean definition to be simulated with the simulated object definition.
Because a large number of beans may need to be relied on in the unit to be tested, and method attributes of the relied-on beans need to be called, most of the relied-on beans and the method attributes thereof are not easy to be directly manufactured, the Mock Bean definition needs to be manufactured virtually, namely, the Mock Bean definition is created according to the corresponding original Bean definition. For example, the created Mock Bean definition can be used to virtually manufacture beans and their methods and attributes, and then the Mock Bean definition is replaced by the original Bean definition that requires Mock, which can facilitate subsequent calls to Bean methods and attributes.
S140, injecting the instances defined by the Mock beans into the test class through Spring so as to test the unit to be tested.
Instantiation can refer to a process of creating Bean by using classes in Bean-oriented programming; since classes are abstract, instantiation may also refer to the process of converting an abstraction into a concrete; where the Bean created by this process may be referred to as an instance. For example, the Mock Bean definition can be instantiated, and then the instance defined by the Mock Bean is injected into the test class through Spring, so that the test class can be reused in the process of testing the unit to be tested.
According to the unit testing method provided by the embodiment of the invention, before the testing method is executed, the characteristic set of the simulation object is determined according to the annotation configuration, so that a reliable basis is provided for screening the object needing Mock; by specifying dependence on an original object conforming to the characteristics of the simulation object and replacing instance creation and calling of a real dependent object, writing and execution of Mock in the test process are facilitated, the support degree of injection of the simulation object instance is improved, and implementation of Mock extension of various frames is facilitated, such as Mock injection supporting Dubbo, MyBatis, RocktMQ frames and the like.
Example two
Fig. 2 is a flowchart of a unit testing method according to a second embodiment of the present invention, which is optimized based on the second embodiment, and specifically describes an instantiation process of an object that does not need to be simulated and a process of creating a Spy proxy for the object that does not need to be simulated. It should be noted that technical details that are not described in detail in the present embodiment may be referred to any of the above embodiments.
Specifically, as shown in fig. 2, the method specifically includes the following steps:
s210, creating and registering an original Bean definition based on an IOC mechanism in an initialization process through a Spring definition Bean factory post-processing module.
The Bean factory post-processing module may refer to a processor intervening in an initialization process of a Bean factory, and the initialization of the Bean factory may include setting of attributes of the Bean factory, and completion of instantiation, initialization, and proxy of a Bean definition corresponding to each Bean. In this embodiment, the Bean factory post-processing module may refer to a Bean factory postprocessor interface or a Bean postprocessor interface, and the two interfaces may refer to expansion interfaces given by a Spring container in a process of instantiating a Bean; the attribute modification of the Bean, the attribute addition of the Bean, the instantiation logic modification of the Bean and the like can be realized in Spring through the two interfaces.
S220, determining a Mock Bean feature set and a Spy Bean feature set according to annotation configuration of the test class of the unit to be tested.
In order to describe the Bean features, a developer can add two types of annotations to the configuration in the test class, so that the original beans can be classified in the subsequent test process conveniently, for example, one type of annotation configuration can be used for determining a Mock Bean feature set and realizing screening of the beans requiring Mock, and the other type of annotation configuration can be used for determining a Spy Bean feature set and realizing screening of the beans requiring Spy.
And S230, traversing the original Bean definitions registered by Spring according to the Mock Bean feature set to determine the original Bean definitions needing Mock.
S240, for the original Bean definition needing Mock, creating a corresponding Mock Bean definition, and replacing the original Bean definition needing Mock with the Mock Bean definition.
And S250, injecting the instances defined by the Mock beans into a test class through Spring so as to test the unit to be tested.
And S260, creating a corresponding Bean instance for the original Bean definition which does not need Mock.
And then, creating corresponding Bean instances for all original Bean definitions which do not need Mock through Spring, and injecting the Bean instances into a test class for testing the test unit. And S270, determining a Bean instance needing the Spy according to the Spy Bean feature set.
In the Bean instances created by the original Bean definition without Mock, annotation configurations corresponding to some instances may accord with a span Bean feature set, namely the Bean instances need to be proxied by a span proxy object, under the condition, the Bean instances needing span can be screened from the created Bean instances according to the span Bean feature set; then, for the Bean instances needing to be monitored, creating corresponding Spy proxy objects; still other instances of beans that are not configured as a Mock or Spy do not require any processing or modification during the creation process.
And S280, returning a SPy proxy object to the Bean instance needing the SPy.
The Spy proxy object may refer to another access method for the target object, for example, the target object may be accessed through the Spy proxy object, and the target object may refer to a Bean instance requiring Spy.
S290, injecting each Bean instance into a test class through Spring so as to test the unit to be tested.
Among these, Bean instances include, but are not limited to, the Bean instances created above for the Mock Bean definition, the Bean instances created for the original Bean definition that does not require Mock, and the Bean instances that require Spy.
Further, S280 specifically includes:
s2810, for the Bean instances needing Spy, obtaining target class types according to object types;
when creating a Spy proxy object for a Bean instance requiring Spy, a target Class type of the created Spy proxy object needs to be determined first, where the target Class type may be an original type of the Bean instance not yet Spy, and may also be an original type to which a Bean instance subclass not yet Spy belongs.
S2820, creating a SPy proxy object based on the target class type, wherein the SPy proxy object refers to the Bean instance needing the SPy.
The proxy of the Bean object is realized by creating a Bean proxy object based on the target Class type and referring to the Bean instance needing the span, and the flexibility of the instantiation of the Bean is improved.
Further, S2810 specifically includes:
if the Bean instance needing the Spy is proxied by the CGLIB, taking the original Bean type to which the Bean type of the Bean instance needing the Spy belongs as a target type;
and if the Bean instance needing the Spy is the JDK dynamic proxy object, taking the original Bean type of the Bean instance needing the Spy as the target class type.
For a Bean instance requiring a Spy, before the Spy is performed (before the Bean instance is processed by a Spy post processor), the Bean instance may have been proxied by the CGLIB, in this case, the type of the Bean instance is substantially a subclass created by the CGLIB based on its original type, and then the original type is a target Class type requiring acquisition; and if the Bean instance to be processed is the JDK dynamic proxy object, the original type of the object can be directly obtained, and then the Spy proxy object of the original type is created by using the dynamic byte code technology.
It should be noted that the logic for obtaining the original type of Bean is to obtain the reference of the Spy proxy object and then obtain the type of the proxied object. For example, a Bean instance of type a $ $ EnhancerBySpringCGLIB $ $38b93679.Class, which is a subclass of a.class, requires the target Class type a.class to be obtained.
Fig. 3 is a schematic diagram illustrating an implementation of a unit testing method according to a second embodiment of the present invention. As shown in fig. 3, the implementation process is as follows:
firstly, writing a test class, describing original beans needing Mock in the test class in an annotation form, creating a test context through Spring, and scanning annotation configuration of the test class by using a test configuration module to determine a Mock Bean feature set and a Spy Bean feature set;
secondly, starting a Spring container, registering a Mock post processor and a Spring post processor, and finishing the registration of the original Bean definition through the Spring;
secondly, traversing original Bean definitions one by utilizing a Mock post processor, judging whether the original Bean definitions belong to a Mock Bean feature set, creating Mock Bean definitions for the original Bean definitions needing Mock, replacing the original Bean definitions in Spring by the Mock Bean definitions, further completing instantiation and registration of the Mock beans, and injecting the instances of the Mock beans into a test class through Spring on the basis for testing a unit to be tested;
for the original Bean definition which does not need Mock, creating a Bean instance according to the original Bean definition which does not need Mock through Spring, judging whether the Bean instance needs a Spy agent or not according to a Spy Bean feature set by utilizing a Spy post processor, if so, returning a Spy agent object which holds the reference of a real Bean object, and if not, directly finishing the instantiation of the Bean; and finally, automatically injecting the Bean examples into a test class through Spring so as to test the unit to be tested.
The unit testing method provided by the second embodiment of the invention is optimized on the basis of the second embodiment, the method determines the Mock Bean feature set through the test annotation configuration module, provides a basis for determining the object to be simulated, realizes the simulation and injection of the object on the basis, and greatly improves the expansibility of the Mock Bean example injection; the method can also realize batch Mock external service through the test annotation configuration module, avoid the problem of complicated steps caused by Mock one by one, and improve the test efficiency; furthermore, the target class type of the Bean is obtained through the post processor of the Spring, and a proper Spring proxy object is created according to the target class type, so that the condition that the Spring Bean instance can be accurately injected into the test class by the subsequent Spring is ensured. The method improves a comprehensive and reliable solution for instantiation and injection of various original Bean definitions.
EXAMPLE III
Fig. 4 is a schematic structural diagram of a unit testing apparatus according to a third embodiment of the present invention. The unit testing device provided by the embodiment comprises:
the test configuration module 310 is used for determining a Mock Bean feature set according to the annotation configuration of the test class of the unit to be tested;
the simulation post-processing module 320 is used for traversing the original Bean definitions registered by Spring according to the Mock Bean feature set so as to determine the original Bean definitions needing Mock; for the original Bean definition needing Mock, creating a corresponding Mock Bean definition, and replacing the original Bean definition needing Mock with the Mock Bean definition;
and the injection module 330 is configured to inject the instance defined by the Mock Bean into the test class through Spring, so that the test module tests the unit to be tested.
According to the unit testing device provided by the third embodiment of the invention, before the testing method is executed, the dependency is specified on the original object which accords with the Mock Bean characteristic, and the instance creation and calling of the real dependent object are replaced, so that the writing and the execution of Mock in the testing process are facilitated, and the support and the expansibility of the injection of the Mock Bean instance are improved.
On the basis of the above embodiment, the apparatus further includes:
the monitoring post-processing module is used for establishing a corresponding Bean instance for the original Bean definition which does not need Mock;
the injection module is further used for injecting each Bean instance into the test class through Spring so as to test the unit to be tested.
On the basis of the foregoing embodiment, before injecting each of the Bean instances into the test class by Spring, the test configuration module is further configured to: determining a monitoring object feature set according to annotation configuration of a test class of a unit to be tested;
the device also includes:
the monitoring post-processing module is used for determining a Bean instance needing Spy according to the monitoring object feature set; and returning a SPy proxy object for the Bean instance needing the SPy.
On the basis of the above embodiment, for the Bean instance requiring Spy, returning a Spy proxy object includes:
for the Bean instance needing Spy, acquiring a target Class type according to an object type;
and creating a Spy proxy object based on the target Class type, wherein the Spy proxy object refers to the Bean instance needing Spy.
On the basis of the above embodiment, for the Bean instance requiring Spy, obtaining a target Class type according to an object type includes:
if the Bean instance needing the Spy is subjected to code generation library proxy, taking the original object type to which the object type of the Bean instance needing the Spy belongs as the target Class type;
and if the Bean instance needing the Spy is the JDK dynamic proxy object, taking the original object type of the Bean instance needing the Spy as the target Class type.
On the basis of the above embodiment, the apparatus further includes:
and the initialization module is used for creating and registering the original Bean definition based on an IOC mechanism in the initialization process through the Spring definition object factory post-processing module before traversing the original Bean definition registered by the Spring according to the Mock Bean feature set.
The unit testing device provided by the third embodiment of the invention can be used for executing the unit testing method provided by any embodiment, and has corresponding functions and beneficial effects.
Example four
Fig. 5 is a schematic diagram of a hardware structure of a testing apparatus according to a fourth embodiment of the present invention. The test equipment includes, but is not limited to: as shown in fig. 5, the test apparatus provided in the present application includes a storage device 42, a processor 41, and a computer program stored in the storage device and operable on the processor, and when the processor 41 executes the computer program, the unit test method described above is implemented.
The test equipment may also include a storage device 42; the number of the processors 41 in the test device may be one or more, and one processor 41 is taken as an example in fig. 5; storage 42 is used to store one or more programs; the one or more programs are executed by the one or more processors 41 such that the one or more processors 41 implement the unit testing method as described in embodiments of the present application.
The test apparatus further comprises: a communication device 43, an input device 44 and an output device 45.
The processor 41, the storage device 42, the communication device 43, the input device 44 and the output device 45 in the test equipment may be connected by a bus or other means, and the connection by the bus is exemplified in fig. 5.
The input device 44 may be used to receive entered numeric or character information and to generate key signal inputs relating to user settings and functional control of the test apparatus. The output device 45 may include a display device such as a display screen.
The communication means 43 may comprise a receiver and a transmitter. The communication device 43 is configured to perform information transmission and reception communication in accordance with control of the processor 41.
The storage device 42, which is a computer-readable storage medium, may be configured to store software programs, computer-executable programs, and modules, such as program instructions/modules corresponding to the unit testing method described in the embodiments of the present application (e.g., the test configuration module 310, the simulation post-processing module 320, and the injection module 330 in the unit testing device). The storage device 42 may include a storage program area and a storage data area, wherein the storage program area may store an operating system, an application program required for at least one function; the storage data area may store data created according to use of the test equipment, and the like. Further, the storage 42 may include high speed random access storage and may also include non-volatile storage, such as at least one magnetic disk storage device, flash memory device, or other non-volatile solid state storage device. In some examples, storage 42 may further include storage remotely located from processor 41, which may be connected to the test equipment over a network. Examples of such networks include, but are not limited to, the internet, intranets, local area networks, mobile communication networks, and combinations thereof.
On the basis of the above-mentioned embodiments, the present embodiment further provides a computer-readable storage medium having stored thereon a computer program which, when executed by a unit testing apparatus, implements the unit testing method in any of the above-mentioned embodiments of the present invention, the method including: determining a simulation object feature set according to annotation configuration of a test class of a unit to be tested; traversing the original object definition registered by Spring according to the simulation object feature set to determine the original object definition required to be simulated; for the original object definition to be simulated, creating a corresponding simulated object definition, and replacing the original object definition to be simulated with the simulated object definition; and injecting the example defined by the simulation object into the test class through Spring so as to test the unit to be tested.
Embodiments of the present invention provide a storage medium including computer-executable instructions, which may take the form of any combination of one or more computer-readable media, such as a computer-readable signal medium or storage medium. The computer-readable storage medium may be, for example, but is not limited to: an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples (a non-exhaustive list) of the computer readable storage medium would include the following: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory device (RAM), a Read Only Memory device (ROM), an Erasable Programmable Read Only Memory device (EPROM), a flash Memory, an optical fiber, a portable CD-ROM, an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. A computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device.
A computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take a variety of forms, including, but not limited to: an electromagnetic signal, an optical signal, or any suitable combination of the foregoing. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device.
Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, Radio Frequency (RF), etc., or any suitable combination of the foregoing.
Computer program code for carrying out operations for aspects of the present application may be written in any combination of one or more programming languages, including an object oriented programming language such as Java, Smalltalk, C + +, and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or the connection may be made to an external computer (for example, through the Internet using an Internet service provider).
From the above description of the embodiments, it is obvious for those skilled in the art that the present invention can be implemented by software and necessary general hardware, and certainly, can also be implemented by hardware, but the former is a better embodiment in many cases. Based on such understanding, the technical solutions of the present invention may be embodied in the form of a software product, which can be stored in a computer-readable storage medium, such as a floppy disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a FLASH Memory (FLASH), a hard disk or an optical disk of a computer, and includes several instructions for enabling a computer device (which may be a personal computer, a server, or a network device) to execute the methods according to the embodiments of the present invention.
It is to be noted that the foregoing is only illustrative of the preferred embodiments of the present invention and the technical principles employed. It will be understood by those skilled in the art that the present invention is not limited to the particular embodiments described herein, but is capable of various obvious changes, rearrangements and substitutions as will now become apparent to those skilled in the art without departing from the scope of the invention. Therefore, although the present invention has been described in greater detail by the above embodiments, the present invention is not limited to the above embodiments, and may include other equivalent embodiments without departing from the spirit of the present invention, and the scope of the present invention is determined by the scope of the appended claims.

Claims (10)

1. A method of unit testing, comprising:
determining a simulation object feature set according to annotation configuration of a test class of a unit to be tested;
traversing the original object definition registered by Spring according to the simulation object feature set to determine the original object definition required to be simulated;
for the original object definition to be simulated, creating a corresponding simulated object definition, and replacing the original object definition to be simulated with the simulated object definition;
and injecting the example defined by the simulation object into the test class through Spring so as to test the unit to be tested.
2. The method of claim 1, further comprising:
for the original object definition which does not need to be simulated, creating a corresponding object instance;
and injecting each object instance into the test class through Spring so as to test the unit to be tested.
3. The method of claim 2, wherein prior to injecting each of the object instances into the test class via Spring, further comprising:
determining a monitoring object feature set according to annotation configuration of a test class of a unit to be tested;
determining an object instance to be monitored according to the monitored object feature set;
and returning a monitoring proxy object for the object instance needing monitoring.
4. The method of claim 3, wherein returning a monitoring agent object for the object instance to be monitored comprises:
for the object instance needing to be monitored, acquiring a target class type according to an object type;
and creating a monitoring proxy object based on the target class type, wherein the monitoring proxy object refers to the object instance needing to be monitored.
5. The method according to claim 4, wherein for the object instance to be monitored, obtaining the target class type according to the object type comprises:
if the object instance needing to be monitored is proxied by a code generation library CGLIB, taking the original object type to which the object type of the object instance needing to be monitored belongs as the target class type;
and if the object instance to be monitored is the JDK dynamic proxy object, taking the original object type of the object instance to be monitored as the target type.
6. The method of claim 1, further comprising, prior to traversing Spring-registered original object definitions according to the simulated object feature set:
the original object definition is created and registered based on the control reversal IOC mechanism during initialization by the Spring defined object factory post-processing module.
7. A unit testing apparatus, comprising:
the test configuration module is used for determining a simulation object feature set according to the annotation configuration of the test class of the unit to be tested;
the simulation post-processing module is used for traversing the original object definition registered by Spring according to the simulation object feature set so as to determine the original object definition to be simulated; for the original object definition to be simulated, creating a corresponding simulated object definition, and replacing the original object definition to be simulated with the simulated object definition;
and the injection module is used for injecting the example defined by the simulation object into the test class through Spring so that the test module can test the unit to be tested.
8. The apparatus of claim 7, further comprising:
the monitoring post-processing module is used for establishing a corresponding object instance for the original object definition which does not need to be simulated;
the injection module is further configured to inject each object instance into the test class through Spring, so as to test the unit to be tested.
9. A test apparatus, comprising:
one or more processors;
storage means for storing one or more programs;
when executed by the one or more processors, cause the one or more processors to implement the unit testing method of any one of claims 1-7.
10. A computer-readable storage medium, on which a computer program is stored which, when being executed by a processor, carries out the unit testing method according to any one of claims 1-7.
CN202110733564.6A 2021-06-30 2021-06-30 Unit testing method, unit testing device, testing equipment and storage medium Pending CN113448853A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110733564.6A CN113448853A (en) 2021-06-30 2021-06-30 Unit testing method, unit testing device, testing equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110733564.6A CN113448853A (en) 2021-06-30 2021-06-30 Unit testing method, unit testing device, testing equipment and storage medium

Publications (1)

Publication Number Publication Date
CN113448853A true CN113448853A (en) 2021-09-28

Family

ID=77814257

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110733564.6A Pending CN113448853A (en) 2021-06-30 2021-06-30 Unit testing method, unit testing device, testing equipment and storage medium

Country Status (1)

Country Link
CN (1) CN113448853A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113885873A (en) * 2021-09-29 2022-01-04 中国科学院软件研究所 Lightweight OpenHarmony operating system application development object management system and application method thereof

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20060167595A1 (en) * 1995-06-07 2006-07-27 Automotive Technologies International, Inc. Apparatus and Method for Determining Presence of Objects in a Vehicle
CN110806967A (en) * 2018-08-06 2020-02-18 北京京东尚科信息技术有限公司 Unit testing method and device
CN111752841A (en) * 2020-06-28 2020-10-09 深圳前海微众银行股份有限公司 Single test simulation method, device, equipment and computer readable storage medium

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20060167595A1 (en) * 1995-06-07 2006-07-27 Automotive Technologies International, Inc. Apparatus and Method for Determining Presence of Objects in a Vehicle
CN110806967A (en) * 2018-08-06 2020-02-18 北京京东尚科信息技术有限公司 Unit testing method and device
CN111752841A (en) * 2020-06-28 2020-10-09 深圳前海微众银行股份有限公司 Single test simulation method, device, equipment and computer readable storage medium

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113885873A (en) * 2021-09-29 2022-01-04 中国科学院软件研究所 Lightweight OpenHarmony operating system application development object management system and application method thereof

Similar Documents

Publication Publication Date Title
WO2021027615A1 (en) Test method and device for web service interface having authentication authorization
CN110147326B (en) Microservice automatic test method based on Redis database
CN110933095A (en) Message parsing method and device
CN110013672B (en) Method, device, apparatus and computer-readable storage medium for automated testing of machine-run games
CN109871312B (en) Interface testing method, device, equipment and readable storage medium
CN107391362A (en) Application testing method, mobile terminal and storage medium
CN111737073B (en) Automatic testing method, device, equipment and medium
CN113407327A (en) Modeling task and data analysis method, device, electronic equipment and system
CN111752841A (en) Single test simulation method, device, equipment and computer readable storage medium
CN113448853A (en) Unit testing method, unit testing device, testing equipment and storage medium
CN113360377B (en) Test method and device
CN108595331B (en) Asynchronous interface testing method, medium, device and computing equipment
CN115221033A (en) Interface protocol testing method and device, computer readable medium and electronic equipment
CN117493188A (en) Interface testing method and device, electronic equipment and storage medium
KR20120102967A (en) System and method for verifying application
CN110348226A (en) A kind of scan method of project file, device, electronic equipment and storage medium
CN115599683A (en) Automatic testing method, device, equipment and storage medium
CN111382058A (en) Service testing method and device, server and storage medium
CN111240992B (en) Test method and device, storage medium and electronic equipment
CN114036523A (en) Vulnerability shooting range automatic building method and device based on virtualization technology
CN107562629B (en) Method and device for testing
CN113704079A (en) Interface testing method and device based on Protobuf
CN112948232A (en) Game protocol testing method and device, electronic equipment and storage medium
CN111382056A (en) Service testing method and device, server and storage medium
Wang et al. Zero-Config Fuzzing for Microservices

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination