Unit test method and device for repeatedly multiplexing Spring container under Junit test framework

By configuring infinite loop mode and monitoring console components under the Junit test framework, multiple reuse of Spring containers is solved, and the problem of test cases in the existing technology needs to restart the container every time, improving the efficiency of unit testing and speed.

CN119046155APending Publication Date: 2024-11-29HANGZHOU PINGPONG INTELLIGENT TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411166352.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-23
Publication Date
2024-11-29

AI Technical Summary

Technical Problem

In the prior art, the Spring container needs to be started every time a test case is executed under the Junit test framework, which makes the container startup process time-consuming and unreusable, resulting in long unit testing time and low efficiency.

Method used

By configuring the test container to infinite loop mode under the Junit test framework, using the monitoring console components to listen to input instructions, multiple reuse of Spring containers are achieved, and custom instructions are used to save and restore the last test case, avoiding restarting every time container.

Benefits of technology

Multiple reuse of Spring containers is realized, which significantly saves unit testing time, improves testing efficiency, and reduces the time spent on container startup.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119046155A_ABST
    Figure CN119046155A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of testing, in particular to a unit testing method for repeatedly multiplexing a Spring container under a Junit testing framework, which comprises the following steps: starting the Spring container by calling a service application starting component of a testing container which at least comprises a testing container initializing component, a console monitoring component and a service application closing component; packaging into a jar package form; a Spring container is initialized through an initialization test container assembly, the test container is configured to be in an infinite loop mode, and an input instruction of a console is monitored through a monitoring console assembly to serve as an intermediate execution process of a loop body; and reading an input instruction of the console, when the input instruction is a test case instruction, executing a unit test case of the input instruction, and executing the unit test case based on the configured infinite loop mode. According to the method, the Spring container is started once in an infinite loop mode, a plurality of test cases can be executed, and the last test case can be quickly and conveniently executed by customizing the input instruction.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of testing technology, and in particular to a unit testing method, device, equipment and storage medium for multiple reuse of Spring containers under a JUnit testing framework. Background Art

[0002] Generally, the JUnit test framework is used for testing in the prior art, and the Spring container needs to be started during the test. If the current test case depends on the Spring container, the Spring container needs to be started once for each test case.

[0003] If multiple test cases need to be executed, the Spring container must be started before executing each test case. This Spring container startup process is repetitive and time-consuming. In summary, there are the following problems: Problem 1: Each time a test case is executed, the current thread ends and the container is destroyed, and the container cannot be reused. Most business applications take more than a minute to start a Spring container, but it only takes a few seconds to actually execute a test method. Therefore, a lot of time is spent on container startup, resulting in a lot of time required to execute a unit test. Problem 2: Each time a test case is executed, the entire context is cleared, and the most recently executed test case cannot be quickly repeated. Summary of the invention

[0004] The purpose of the present invention is to solve the shortcomings of the prior art and provide a unit testing method for multiple reuse of Spring containers under a Junit testing framework, comprising the following steps:

[0005] Start the Spring container by calling the startup business application component of the test container, wherein the test container at least includes the startup business application component, the initialization test container component, the monitoring console component and the shutdown business application component, and encapsulates the components into a jar package form;

[0006] Initialize the Spring container through the initialization test container component, wherein the test container is configured as an infinite loop mode, and monitor the input instructions of the console through the monitoring console component as the intermediate execution process of the loop body;

[0007] The input instruction of the console is read, and when the input instruction is a test case instruction, the unit test case of the input instruction is executed, and the execution is performed based on the configured infinite loop mode.

[0008] Preferably, calling the startup business application component of the test container to start the Spring container includes:

[0009] Get the class loader of the current thread;

[0010] Resource files in the directory specified by the current application loaded by the class loader;

[0011] Filter out class files with a file name suffix of .class from the resource files through the class loader;

[0012] Filter out the startup class based on whether the class file contains the @SpringBootApplication annotation;

[0013] Call the main() method of the startup class to start the Spring container.

[0014] Preferably, executing the unit test case of the input instruction and executing based on the configured infinite loop mode further includes:

[0015] Enter an infinite loop while method;

[0016] Each loop in the while method reads the input instruction based on the monitoring console component and executes the input instruction.

[0017] Preferably, the input instruction of the console is read, and if it is determined that the type of the input instruction is a preset custom instruction:

[0018] If the input instruction type is a custom instruction "r", the last executed test case instruction is retrieved from the global variable of the context, and then the last executed test case instruction is executed.

[0019] Preferably, when the Spring container needs to be closed when the test execution is completed or the test needs to be interrupted, the execution thread ends the current Spring container thread by calling the closing business application component.

[0020] Preferably, when the input instruction is a test case instruction, the test case instruction is also saved to the context:

[0021] Execute the test case instruction, and save the most recent test case instruction to a named instance of a subclass of the pre-constructed AbstractCommand abstract command class through a preset command saving mechanism;

[0022] The AbstractCommand abstract command class includes a preCmd variable and a curCmd variable, wherein the preCmd variable is used to save the previous command, and the curCmd variable is used to save the current command.

[0023] Preferably, saving the most recent test case instruction to a pre-constructed subclass named instance of the AbstractCommand abstract command class through a preset command saving mechanism further includes:

[0024] According to the pre-constructed subclass command instance of the AbstractCommand abstract command class, the value of the current curCmd variable is assigned to the preCmd variable to save the previous command;

[0025] The currently constructed subclass command instance is assigned to the curCmd variable until the test case instruction is saved to the global variable of the context and the next subclass command instance of the AbstractCommand abstract command class is no longer constructed.

[0026] Based on the same concept, the present invention also provides a unit testing device for multiple reuse of Spring containers under a Junit testing framework, including:

[0027] Start the Spring container module, which is used to start the Spring container by calling the startup business application component of the test container, wherein the test container at least includes the startup business application component, the initialization test container component, the monitoring console component and the shutdown business application component, and the components are packaged in the form of a jar package;

[0028] A configuration module, used to initialize the Spring container through the initialization test container component, wherein the test container is configured as an infinite loop mode, and the input instructions of the console are monitored by the monitoring console component as an intermediate execution process of the loop body;

[0029] The execution module is used to read the input instruction of the console, and when the input instruction is a test case instruction, execute the unit test case of the input instruction, and execute it based on the configured infinite loop mode.

[0030] Based on the same concept, the present invention also provides a computer device, including a memory and a processor, wherein the memory stores computer-readable instructions, and when the computer-readable instructions are executed by the processor, the processor executes the steps of the unit testing method of multiple reuse of Spring containers under the Junit testing framework as described in the embodiment.

[0031] Based on the same concept, the present invention also provides a storage medium storing computer-readable instructions. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the unit testing method of multiple reuse of Spring containers under the Junit testing framework as described in the embodiment.

[0032] Compared with the prior art, the present invention has the following beneficial effects:

[0033] 1) In the technical solution of the present invention, the input instruction is monitored by the monitoring console component, and as the intermediate execution process of the loop body of the while method, after a unit test is completed, the current thread executing the while method is not closed, and continues to wait for input instructions according to the monitoring console component. After obtaining a new unit test case instruction, the test is directly performed, and the Spring container is started once, which can be used to execute multiple test cases, thereby saving unit test time and improving unit test efficiency;

[0034] 2) The present invention implements saving the class name of the previous test case to the context through customized input instructions, the preCmd variable for saving the previous command and the curCmd variable for saving the current command in the subclass naming instance of the pre-constructed AbstractCommand abstract command class. After the monitoring console component receives the customized instruction "r", it obtains the full class name of the last executed test case from the context global variable, and the current thread quickly and conveniently executes the most recent test case based on this full name. BRIEF DESCRIPTION OF THE DRAWINGS

[0035] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the following detailed description of the preferred embodiment.The drawings are only for the purpose of illustrating the preferred embodiments and are not to be construed as limiting the invention.

[0036] Figure 1 This is a flow chart of a unit testing method for multiple reuse of Spring containers under a Junit testing framework of the present invention;

[0037] Figure 2 Another flow chart of a unit testing method for multiple reuse of Spring containers under a Junit testing framework of the present invention. DETAILED DESCRIPTION

[0038] In order to make the purpose, technical scheme and advantages of the present invention clearer, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention. Obviously, the described embodiments are part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of this application.

[0039] Those skilled in the art will appreciate that, unless otherwise stated, the singular forms "a", "an", and "the" used herein may also include plural forms. It should be further understood that the term "comprising" used in the specification of the present invention refers to the presence of the features, integers, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or groups thereof.

[0040] The technical terms involved in the embodiments of the present invention are defined as follows:

[0041] 1. Spring container: It is the core component of the Java technology framework Spring. Its main function is to maintain the objects required in the system.

[0042] 2. Junit framework: Junit is a unit testing framework for Java language, which is used to write and run Java test cases.

[0043] Example 1

[0044] See also Figure 1 and Figure 2 As shown, this embodiment provides a unit testing method for multiple reuse of Spring containers under a JUnit testing framework, including the following steps:

[0045] Start the Spring container by calling the startup business application component of the test container, where the test container at least includes starting the business application component, initializing the test container component, monitoring the console component, and closing the business application component, and encapsulating the component in the form of a jar package;

[0046] Preferably, calling the startup business application component of the test container to start the Spring container includes:

[0047] Get the class loader of the current thread. Specifically, in this embodiment, the currently executing thread can be obtained by the static method currentThread() of the Thread class, and then the class loader of the current thread can be obtained by calling the getContextClassLoader() method of the Thread class.

[0048] The resource files in the directory specified by the current application are loaded by the class loader. Specifically, in this embodiment, the current class loader inherits the ClassLoader class in the JDK and loads the path by overriding the loadClass method or overriding the findClass method. This embodiment implements a custom loading path by overriding the findClass method.

[0049] The class loader is used to filter out class files with the suffix .class in the resource file name. Specifically, when rewriting the findClass method, the byte stream in the .class file is brought in through the file stream byte stream FileInputStram, and then deserialized into a Class object through defineclass.

[0050] The startup class is filtered out according to whether the class file contains the @SpringBootApplication annotation. Specifically, in this embodiment, the Java reflection API can be used to check whether the class contains the @SpringBootApplication annotation.

[0051] The main() method of the startup class is called to start the Spring container. Specifically, in this embodiment, ConfigurableApplicationContextcontext=newAnnotationConfigApplicationContext(AppConfig.class) starts the Spring container by using the annotation configuration method.

[0052] Start the Spring application by calling the main method of the startup class, and configure and manage the application's dependency injection and components in a simple way without additional deployment or configuration steps, allowing developers to focus more on the business logic of the application rather than managing the underlying configuration details.

[0053] The Spring container is initialized by initializing the test container component, wherein the test container is configured as an infinite loop mode, and the input instructions of the console are listened to by the monitoring console component as the intermediate execution process of the loop body. Specifically, in this embodiment, the test container component is initialized to create a Spring application context. The creation process can be implemented programmatically or through a configuration file. In this embodiment, it is implemented programmatically, using a Java configuration class to initialize the Spring container, and creating a specific Spring application context by instantiating AnnotationConfigApplicationContext (at this time, beans can be obtained from the context).

[0054] The effect of startup completion is as follows:

[0055] [Service startup completed]

[0056] Service name:currents-payment-app

[0057] Port number: 8080

[0058] RPC port: 20881

[0059] Current environment: currentz-dev

[0060] Access path: http: / / 127.0.0.1:8080 /

[0061] [xxxx-04-0820:80:84.146][INF0][][main][franework.endpoint.checkHealth.:{database=MySqL,hello=1},refreshScope=UP{}}

[0062] Welcome to the lightweight test container

[0063] 1. To test a single method, enter the full name of the method in the console.

[0064] Read the input command of the console. When the input command is a test case command, execute the unit test case of the input command and execute it based on the configured infinite loop mode.

[0065] Preferably, executing the unit test case of the input instruction and executing based on the configured infinite loop mode further includes:

[0066] Entering an infinite loop while method, specifically, in this embodiment, after checking that Spring startup is completed, a new thread can be started or an asynchronous task can be used in the run method entering the CommandLineRunner or ApplicationRunner interface. In this embodiment, an infinite loop while method is run by creating a new thread, where the run method returns an implementation of CommandLineRunner.

[0067] In the while method, each loop reads input instructions based on the monitoring console component and executes the input instructions.

[0068] After a unit test is completed, the current thread executing the while method is not closed, and continues to wait for input instructions according to the monitoring console component. After obtaining new unit test case instructions, the test is directly carried out, thereby saving unit test time and improving unit test efficiency.

[0069] After executing the unit test case, the results obtained are as follows:

[0070] **For example, unit test case com.xxx.currents.payment.PayoutTest

[0071] 2. To test the entire test class, please enter the full name of the class in the console

[0072] **For example: com.xxxx.currents.payment.PayoutTest

[0073] 3. Repeat the previous test, just enter -"r" in the console

[0074] com.xxx.currents.payment.PayoutTest#caQueryTest

[0075] ===Runstart===com.xxx.currents.payment.PayoutTes#caQueryTest:

[0076] [xxxx-xx-xx20:34:00.202][INF0][][main][biz.biz.DubboBiz.:]CAQuerryrequest:{"source":"serach","txId":"currents-8602112010805181460-D"}

[0077] [xxxx-xx-xx20:34:00.202][WARA][][main][dubbox.filter.TLogDubboxFilter.:][TLOG]Local threadlocal variables are not passed traceld correctly, local calls do not pass traceld]

[0078] true

[0079] Runend

[0080] Preferably, the input command of the console is read, and if it is determined that the type of the input command is a preset custom command:

[0081] If the input instruction type is a custom instruction "r", the last executed test case instruction is retrieved from the global variable of the context, and then the last executed test case instruction is executed.

[0082] After receiving the custom instruction "r", the monitoring console component obtains the full class name of the last executed test case from the context global variable. The current thread executes the most recent test case quickly and conveniently based on this full class name.

[0083] Use the custom instruction "r" to repeat the last executed test case. The specific effect is as follows:

[0084] .2","isMonitored":0,"Location":"US","matchstatus":"no_match","modified":"166718623608","modify":"system ","name":"Xiao Xu","reviewResult"."NoMate""txId":"currents-8602112010805181460-D","types"":"[\"sanction\"]"}

[0085] true

[0086] ==Runend====

[0087] ===Runstart====

[0088] com.xxx.currents.payment.PayoutTest#caQueryTest

[0089] [xxxx-xx-xx20:36:02.844][INF0][][main][biz.biz.DubboBiz.:]CAQueryrequest:{"source":"search""txId":"currents-8602112010805181460-D"}

[0090] [xxxx-xx-xx20:36:02.844][WARN][][main][dubbox.filter.TLogDubboxFilter.:][TL0G] The local threadLocal variable does not pass the traceId correctly, so the traceId will not be passed in this call

[0091] true

[0092] ==Runend====

[0093] Preferably, when the Spring container needs to be closed when the test execution is completed or the test needs to be interrupted, the execution thread ends the current Spring container thread by calling to close the business application component.

[0094] Preferably, when the input instruction is a test case instruction, the test case instruction is also saved to the context:

[0095] Execute the test case instructions and save the most recent test case instructions to the pre-constructed named instance of the subclass of the AbstractCommand abstract command class through the preset command saving mechanism;

[0096] The AbstractCommand abstract command class includes preCmd and curCmd variables, where the preCmd variable is used to save the previous command and the curCmd variable is used to save the current command.

[0097] Preferably, saving the most recent test case instruction to a pre-constructed subclass named instance of the AbstractCommand abstract command class through a preset command saving mechanism further includes:

[0098] According to the pre-constructed subclass command instance of the AbstractCommand abstract command class, the value of the current curCmd variable is assigned to the preCmd variable to save the previous command;

[0099] Assign the currently constructed subclass command instance to the curCmd variable until the test case instruction is saved to the global variable of the context and the next subclass command instance of the AbstractCommand abstract command class is no longer constructed.

[0100] The class name of the previous test case is saved to the context through the preCmd variable that saves the previous command and the curCmd variable that saves the current command in the subclass named instance of the pre-constructed AbstractCommand abstract command class, so that the previous test case instruction can be quickly retrieved from the global variables of the context.

[0101] Example 2

[0102] This embodiment provides a unit testing device that reuses Spring containers multiple times under a Junit testing framework, which is used to execute the method in the above embodiment 1, specifically including:

[0103] Start the Spring container module, which is used to start the Spring container by calling the startup business application component of the test container, wherein the test container at least includes the startup business application component, the initialization test container component, the monitoring console component and the shutdown business application component, and the components are packaged in the form of a jar package;

[0104] An infinite loop module, used to initialize the Spring container through the initialization test container component, wherein the test container is configured as an infinite loop mode, and the input instructions of the console are monitored by the monitoring console component as the intermediate execution process of the loop body;

[0105] The unit test case execution module is used to read the input instruction of the console. When the input instruction is a test case instruction, the unit test case of the input instruction is executed and executed based on the configured infinite loop mode.

[0106] Example 3

[0107] In some embodiments of the present application, a computer device is also provided, including a memory and a processor, wherein the memory stores computer-readable instructions, and when the computer-readable instructions are executed by the processor, the processor executes the steps of the unit testing method of multiple reuse of the Spring container under the Junit testing framework as described in Example 1.

[0108] The present invention also provides a storage medium storing computer-readable instructions. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the unit testing method of multiple reuse of Spring containers under the Junit testing framework as described in Example 1.

[0109] It is understandable that, for the unit testing method of the aforementioned Junit test framework that reuses the Spring container multiple times, if they are all implemented in the form of software function modules and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer server, or a network device, etc.) to perform all or part of the steps of the various embodiments of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program code.

[0110] Computer readable storage media may include data signals propagated in baseband or as part of a carrier wave, wherein readable program codes are carried. Such propagated data signals may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. The readable storage medium may also be any readable medium other than a readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, device, or device. The program codes contained on the readable storage medium may be transmitted using any suitable medium, including but not limited to wireless, wired, optical cable, RF, etc., or any suitable combination thereof.

[0111] The above is only a preferred embodiment of the present invention, and the protection scope of the present invention is not limited to the above embodiments. All technical solutions under the concept of the present invention belong to the protection scope of the present invention. It should be pointed out that for ordinary technicians in this technical field, some improvements and modifications without departing from the principle of the present invention should also be regarded as the protection scope of the present invention.

Claims

1. A unit testing method for multiple reuse of Spring containers under the Junit testing framework, characterized in that: The following steps are involved: Start the Spring container by calling the startup business application component of the test container, wherein the test container at least includes the startup business application component, the initialization test container component, the monitoring console component and the shutdown business application component, and encapsulates the components into a jar package form; Initialize the Spring container through the initialization test container component, wherein the test container is configured as an infinite loop mode, and monitor the input instructions of the console through the monitoring console component as the intermediate execution process of the loop body; The input instruction of the console is read, and when the input instruction is a test case instruction, the unit test case of the input instruction is executed, and the execution is performed based on the configured infinite loop mode.

2. According to the unit testing method of multiple reuse of Spring container under the Junit testing framework according to claim 1, it is characterized in that: Calling the startup business application component of the test container to start the Spring container includes: Get the class loader of the current thread; Resource files in the directory specified by the current application loaded by the class loader; Filter the class files whose file names have the suffix .class of the resource files through the class loader; Filter out the startup class based on whether the class file contains the @SpringBootApplication annotation; Call the main() method of the startup class to start the Spring container.

3. The unit testing method for multiplexing Spring containers under the Junit testing framework according to claim 1 is characterized in that: Executing the unit test case of the input instruction and executing based on the configured infinite loop mode further includes: Enter an infinite loop while method; Each loop in the while method reads the input instruction based on the monitoring console component and executes the input instruction.

4. The unit testing method for multiplexing Spring containers under the Junit testing framework according to claim 1 is characterized in that: Read the input command of the console. If the type of the input command is determined to be a preset custom command: If the input instruction type is a custom instruction "r", the last executed test case instruction is retrieved from the global variable of the context, and then the last executed test case instruction is executed.

5. The unit testing method for multiplexing Spring containers under the Junit testing framework according to claim 1 is characterized in that: When the Spring container needs to be closed under the condition that the test execution is completed or the test needs to be interrupted, the execution thread ends the current Spring container thread by calling the closing business application component.

6. The unit testing method for multiplexing Spring containers under the Junit testing framework according to claim 1 is characterized in that: When the input instruction is a test case instruction, the test case instruction is also saved to the context: Execute the test case instruction, and save the most recent test case instruction to a named instance of a subclass of the pre-constructed AbstractCommand abstract command class through a preset command saving mechanism; The AbstractCommand abstract command class includes a preCmd variable and a curCmd variable, wherein the preCmd variable is used to save the previous command, and the curCmd variable is used to save the current command.

7. The unit testing method for multiplexing Spring containers under the Junit testing framework according to claim 6 is characterized in that: Saving the most recent test case instruction to a pre-constructed named instance of a subclass of the AbstractCommand abstract command class through a preset command saving mechanism further includes: According to the pre-constructed subclass command instance of the AbstractCommand abstract command class, the value of the current curCmd variable is assigned to the preCmd variable to save the previous command; The currently constructed subclass command instance is assigned to the curCmd variable until the test case instruction is saved to the global variable of the context and the next subclass command instance of the AbstractCommand abstract command class is no longer constructed.

8. A unit testing device that reuses Spring containers multiple times under a Junit testing framework, characterized in that: include: Start the Spring container module, which is used to start the Spring container by calling the start business application component of the test container, wherein the test container at least includes the start business application component, the initialization test container component, the monitoring console component and the shutdown business application component, and the components are packaged in the form of a jar package; A configuration module, used to initialize the Spring container through the initialization test container component, wherein the test container is configured as an infinite loop mode, and the input instructions of the console are monitored by the monitoring console component as an intermediate execution process of the loop body; The execution module is used to read the input instruction of the console, and when the input instruction is a test case instruction, execute the unit test case of the input instruction, and execute it based on the configured infinite loop mode.

9. A computer device, characterized in that: It includes a memory and a processor, wherein the memory stores computer-readable instructions, and when the computer-readable instructions are executed by the processor, the processor executes the steps of a unit testing method for multiple reuse of a Spring container under a Junit testing framework as described in any one of claims 1 to 7.

10. A storage medium storing computer-readable instructions, characterized in that: When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the unit testing method of multiple reuse of Spring container under the Junit testing framework as described in any one of claims 1 to 7.