Interface simulation method, apparatus, device, and storage medium
By obtaining interface information and response configuration items through interface simulation, the problem that interface simulation in existing technologies cannot meet the requirement of multiple calls with different response values is solved, thus improving testing efficiency and flexibility.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CHINA UNITED NETWORK COMM GRP CO LTD
- Filing Date
- 2022-11-04
- Publication Date
- 2026-05-29
AI Technical Summary
Existing technologies return a fixed, simulated response value for each API call during testing, which cannot meet the need for multiple calls with different response values, resulting in low testing efficiency.
The interface simulation method accepts a startup request carrying monitoring instructions, obtains the interface information and response configuration items of the target interface, determines whether the target interface to be called exists in the application to be simulated, and simulates it based on the configuration items, supporting the requirement of multiple calls with different response values.
It enables monitoring and simulation of multiple target interfaces, improving testing efficiency and supporting the same interface to return different response values in different test scenarios.
Smart Images

Figure CN115904949B_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of computer technology, and in particular to an interface simulation method, apparatus, device and storage medium. Background Technology
[0002] In daily testing, it's often necessary to mock certain interfaces or methods to return a specific response or result, allowing the business process to continue normally. For different application scenarios, there are several technical solutions for mocking applications. First, for interfaces initiated from the client, a proxy server can be added between the client and the application server. The proxy server directly mocks the interface's response content, preventing the interface request from being sent to the application server, thus achieving interface mocking. Second, for interface calls between application servers, developers can hardcode the interface's response return value without actually executing the interface's code logic, thus achieving interface mocking. Third, for interface calls between application servers, service monitoring tools can be used to monitor interface execution. When an interface is called, a fixed response content can be used instead of the actual interface response content, thus achieving interface mocking.
[0003] Because each API call during testing returns a fixed simulated response value, changing this value requires modifying the response content and then running the test again. In complex business processes where the same API needs to be called multiple times with different responses each time, or in two different test scenarios where two testers are performing different business processes and simultaneously calling the same API with different response values, testers are forced to repeatedly perform the test, modifying the response value before each test, significantly reducing testing efficiency. Summary of the Invention
[0004] This disclosure provides an interface simulation method, apparatus, device, and storage medium to improve testing efficiency.
[0005] To achieve the above objectives, the present disclosure adopts the following technical solution:
[0006] Firstly, an interface simulation method is provided, the method comprising:
[0007] Accept the startup request of the application to be simulated, the startup request carrying monitoring instructions;
[0008] The application to be simulated is started based on the startup request, and the interface information of the target interface and at least one interface response configuration item are obtained based on the monitoring instructions.
[0009] Based on the interface information, determine whether the target interface to be called exists in the already started application to be simulated;
[0010] In response to the existence of a target interface to be invoked in the already started application to be simulated, the target interface to be invoked is simulated based on the at least one interface response configuration item.
[0011] In conjunction with the first aspect above, in one possible implementation, determining whether the target interface to be called exists in the already started application to be simulated based on the interface information includes:
[0012] Obtain the interfaces and interface information that have been called in the application to be simulated that has been started;
[0013] If the interface information of the target interface to be monitored is identical to that of the interface to be called, it is determined that the target interface to be called exists in the application to be simulated.
[0014] In conjunction with the first aspect above, in one possible implementation, the interface response configuration item includes: simulated input parameter value, simulated response value, and remaining loop count;
[0015] The simulation of the called target interface based on the at least one interface response configuration item includes:
[0016] Obtain the actual input parameter value corresponding to the target interface being called, and determine whether the actual input parameter value includes each field of the simulated input parameter value;
[0017] In response to the fact that the actual input parameter value includes each field of the simulated input parameter value, determine whether the remaining number of loops is zero;
[0018] If the remaining loop count is not zero, decrease the remaining loop count by one, and send the simulated response value to the calling end that calls the target interface.
[0019] In conjunction with the first aspect above, in one possible implementation, before obtaining the interface information of the target interface and at least one interface response configuration item based on the monitoring command, the method further includes:
[0020] Obtain the configuration instructions sent by the user, and determine the interface information of the target interface to be monitored and at least one interface response configuration item based on the configuration instructions.
[0021] Secondly, an interface simulation device is provided, the device comprising:
[0022] The request receiving module is used to accept the startup request of the application to be simulated, wherein the startup request carries a monitoring instruction;
[0023] The request processing module is used to start the application to be simulated based on the startup request, and to obtain the interface information of the target interface and at least one interface response configuration item based on the monitoring instruction.
[0024] The interface call monitoring module is used to determine, based on the interface information, whether there is a target interface to be called in the already started application to be simulated;
[0025] An interface simulation module is used to simulate the target interface to be called based on the at least one interface response configuration item in the already started application to be simulated.
[0026] In conjunction with the second aspect above, in one possible implementation, the interface call monitoring module includes:
[0027] The interface information acquisition unit is used to acquire the interfaces and interface information called in the started application to be simulated.
[0028] The target interface determination unit is used to determine that a target interface to be called exists in the application to be simulated when the interface information of the target interface to be monitored contains the same interface information as the interface to be called.
[0029] In conjunction with the second aspect above, in one possible implementation, the interface response configuration item includes: simulated input parameter value, simulated response value, and remaining loop count;
[0030] The interface simulation module includes:
[0031] The input parameter value judgment unit is used to obtain the actual input parameter value corresponding to the target interface being called, and to determine whether the actual input parameter value includes each field of the simulated input parameter value;
[0032] The remaining loop count determination unit is used to determine whether the remaining loop count is zero in response to each field in the simulated input parameter value included in the actual input parameter value.
[0033] The simulated response unit is used to decrement the remaining loop count by one if the remaining loop count is not zero, and send the simulated response value to the calling end that calls the target interface.
[0034] In conjunction with the second aspect above, in one possible implementation, the apparatus further includes:
[0035] The configuration instruction acquisition module is used to acquire configuration instructions sent by the user, and determine the interface information of the target interface to be monitored and at least one interface response configuration item based on the configuration instructions.
[0036] Thirdly, an interface emulation device is provided, comprising: a processor and a memory; wherein the memory is used to store computer execution instructions, and when the interface emulation device is running, the processor executes the computer execution instructions stored in the memory to cause the interface emulation device to perform the interface emulation method as described in the first aspect and any possible implementation thereof.
[0037] Fourthly, this disclosure provides a computer-readable storage medium storing instructions that, when executed by a processor of an interface emulation device, enable the interface emulation device to perform the interface emulation method as described in the first aspect and any possible implementation thereof.
[0038] In this disclosure, the names of the aforementioned interface simulation devices do not limit the devices or functional modules themselves. In actual implementation, these devices or functional modules may appear under other names. As long as the functions of each device or functional module are similar to those of this disclosure, they fall within the scope of the claims of this disclosure and their equivalents.
[0039] These or other aspects of this disclosure will become more readily apparent in the following description.
[0040] The technical solution provided in this disclosure offers at least the following beneficial effects: This disclosure provides an interface simulation method, apparatus, device, and storage medium. The method includes: receiving a startup request for an application to be simulated, the startup request carrying monitoring instructions; starting the application to be simulated based on the startup request, and obtaining interface information of a target interface and at least one interface response configuration item based on the monitoring instructions; determining whether a called target interface exists in the started application to be simulated based on the interface information; and simulating the called target interface based on the at least one interface response configuration item in response to the existence of a called target interface in the started application to be simulated. Users can configure the interface information of the target interface and at least one interface response configuration item to monitor and simulate multiple target interfaces, or to perform different simulations on the same target interface by setting multiple interface response configuration items, thereby improving testing efficiency. Attached Figure Description
[0041] Figure 1 This is a schematic diagram of the hardware structure of an interface simulation device provided in this disclosure;
[0042] Figure 2 This is a flowchart illustrating an interface simulation method provided in this disclosure;
[0043] Figure 3This is a schematic diagram of the operation interface display in an interface simulation method provided in this disclosure;
[0044] Figure 4 A schematic diagram of the operation interface display in another interface simulation method provided in this disclosure;
[0045] Figure 5 A schematic diagram of the operation interface display in another interface simulation method provided in this disclosure;
[0046] Figure 6 A flowchart illustrating another interface simulation method provided in this disclosure;
[0047] Figure 7 A flowchart illustrating another interface simulation method provided in this disclosure;
[0048] Figure 8 This is a schematic diagram of the structure of an interface simulation device provided in this disclosure. Detailed Implementation
[0049] The interface simulation method, apparatus, device, and storage medium provided in the embodiments of this disclosure will now be described in detail with reference to the accompanying drawings.
[0050] The terms “first” and “second” in this disclosure and its accompanying drawings are used to distinguish different objects or to distinguish different treatments of the same object, rather than to describe a particular order of objects.
[0051] Furthermore, the terms “comprising” and “having”, and any variations thereof, used in the description of this disclosure are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or apparatus that includes a series of steps or units is not limited to the steps or units listed, but may optionally include other steps or units not listed, or may optionally include other steps or units inherent to such process, method, product, or apparatus.
[0052] It should be noted that in this disclosure, the terms "exemplary" or "for example" are used to indicate that something is an example, illustration, or description. Any embodiment or design described as "exemplary" or "for example" in this disclosure should not be construed as being more preferred or advantageous than other embodiments or designs. Specifically, the use of terms such as "exemplary" or "for example" is intended to present the relevant concepts in a concrete manner.
[0053] Figure 1 This is a schematic diagram of the structure of an interface simulation device provided in an embodiment of this disclosure. Figure 1As shown, the interface simulation device 100 includes at least one processor 101, a communication line 102, and at least one communication interface 104, and may also include a memory 103. The processor 101, memory 103, and communication interface 104 can be connected via the communication line 102.
[0054] The processor 101 may be a central processing unit (CPU), an application-specific integrated circuit (ASIC), or one or more integrated circuits configured to implement the embodiments of this disclosure, such as one or more digital signal processors (DSPs), or one or more field-programmable gate arrays (FPGAs).
[0055] Communication line 102 may include a path for transmitting information between the aforementioned components.
[0056] The communication interface 104 is used to communicate with other devices or communication networks. It can use any transceiver-like device, such as Ethernet, radio access network (RAN), wireless local area network (WLAN), etc.
[0057] The memory 103 may be a read-only memory (ROM) or other type of static storage device capable of storing static information and instructions, random access memory (RAM) or other type of dynamic storage device capable of storing information and instructions, or electrically erasable programmable read-only memory (EEPROM), compact disc read-only memory (CD-ROM) or other optical disc storage, optical disc storage (including compressed optical discs, laser discs, optical discs, digital universal optical discs, Blu-ray discs, etc.), magnetic disk storage media or other magnetic storage devices, or any other medium capable of including or storing desired program code having the form of instructions or data structures and accessible by a computer, but not limited thereto.
[0058] In a possible design, the memory 103 can exist independently of the processor 101, that is, the memory 103 can be a memory external to the processor 101. At this time, the memory 103 can be connected to the processor 101 through the communication line 102, used to store execution instructions or application program codes, and controlled by the processor 101 to execute, so as to implement the network quality determination method provided in the following embodiments of the present disclosure. In another possible design, the memory 103 can also be integrated with the processor 101, that is, the memory 103 can be an internal memory of the processor 101. For example, the memory 103 is a cache, which can be used to temporarily store some data and instruction information, etc.
[0059] As an implementable manner, the processor 101 can include one or more CPUs, such as Figure 1 CPU0 and CPU1 in Figure 1 As another implementable manner, the interface simulation device 100 can include multiple processors, such as
[0060] the processor 101 and the processor 107 in
[0061] As another implementable manner, the interface simulation device 100 can further include an output device 105 and an input device 106.
[0062] 1. JVM (Java Virtual Machine):
[0063] It is a specification for computing devices. It is a fictional computer that is implemented by emulating various computer functions on an actual computer.
[0064] 2. Sandbox (Chinese name: sandbox):
[0065] It is a solution framework for aspect-oriented programming (AOP), which can perform relevant operations on the programs in the JVM process without restarting and without invading the target JVM application. Among them, aspect-oriented programming is a technology that realizes the unified maintenance of program functions through pre-compilation and dynamic proxy during runtime.
[0066] In daily testing, it's often necessary to mock certain interfaces or methods to return a specific response or result, allowing the business process to continue normally. In the current testing process, each interface call returns a fixed mock response value. If the mock response value needs to be changed, the response content must be modified again before the next test.
[0067] In a specific business scenario, after a user logs in, a query API is called to check if the user is eligible to claim a prize. If eligible, the page displays the types of prizes the user can claim. The user selects a prize and clicks "Claim." This click triggers a call to the claim API. While the claim API is executing its logic, it internally calls the query API to check if the user has already claimed a prize this month. If not, the claim is successful. After successful claiming, the API returns a success page. The page automatically refreshes and re-requests the query API to retrieve the claimed prize information and displays it on the page. This entire business process is uninterrupted and continuous. During this process, the query API is called three times, each time returning different responses.
[0068] For the above business scenarios, the existing interface simulation technology can only return a fixed value, so it cannot guarantee that the simulation of all interfaces can be completed in a single test process.
[0069] To address the aforementioned technical problems, this application provides an interface simulation method. The method involves accepting a startup request for an application to be simulated, the startup request carrying monitoring instructions; starting the application to be simulated based on the startup request, and obtaining interface information of a target interface and at least one interface response configuration item based on the monitoring instructions; determining whether a target interface to be called exists in the already started application to be simulated based on the interface information; and simulating the called target interface based on the at least one interface response configuration item in response to the existence of a called target interface in the already started application to be simulated. By accepting the startup request, the application to be simulated can be started. Simultaneously, based on the monitoring instructions in the startup request, interface information of the target interface and at least one interface response configuration item can be obtained. This allows for the determination of whether a target interface to be called exists in the already started application to be simulated, thereby monitoring the interface in the application to be simulated. If a target interface to be called exists in the application to be simulated, the simulated interface continues to be simulated based on at least one interface response configuration item. Therefore, users can monitor and simulate multiple target interfaces with a single operation by configuring the interface information of the target interface and at least one interface response configuration item, or perform different simulations on the same target interface by setting multiple interface response configuration items.
[0070] Figure 2 This is a flowchart illustrating an interface simulation method provided in this disclosure. The interface simulation method can be implemented through the following steps (S210-S240):
[0071] Step S210: Accept the startup request of the application to be simulated, the startup request carrying monitoring instructions.
[0072] When sending a startup request with monitoring instructions to a simulated application, users can do so in the following ways.
[0073] In the first method, users can initiate a request to launch the application to be simulated through a graphical user interface, including monitoring instructions in the launch request. For example, the launch page of the application to be simulated can include a launch button and monitoring options. Before the user clicks the launch button, the monitoring options are checked, making them selected. Then, when the user clicks the launch button on the launch page, a launch request carrying monitoring instructions will be sent.
[0074] The second method allows users to send a startup request carrying monitoring instructions to the simulated application by typing commands in the command input box. For example, assuming the application is kcard-charge, the user could type the command to start the application as `java -javaagent: / root / .sandbox-module / projectB.jar -jar kcard-charge.jar`, where `-javaagent: / root / .sandbox-module / projectB.jar` represents the monitoring instructions added when starting the application.
[0075] Step S220: Start the application to be simulated based on the startup request, and obtain the interface information of the target interface and at least one interface response configuration item based on the monitoring instruction.
[0076] As one implementation, before obtaining the interface information of the target interface and at least one interface response configuration item based on the monitoring command, the method further includes:
[0077] Obtain the configuration instructions sent by the user, and determine the interface information of the target interface to be monitored and at least one interface response configuration item based on the configuration instructions.
[0078] The target interface is the interface that needs to be monitored and simulated. Therefore, the interface information of the target interface and at least one interface response configuration item obtained based on the monitoring command are pre-set. In one embodiment, the interface information of the target interface can be obtained from a cache database based on the monitoring command. The cache database includes user-configured interface information that needs to be monitored and simulated, as well as the corresponding interface response configuration items. The interface information and corresponding interface response configuration items in the cache database are obtained based on user operations, and the user can add, delete, modify, and perform other operations on the interface information and corresponding interface response configuration items in the cache database.
[0079] For example, please see Figure 3 Testers can Figure 3 The interface shown in the image displays the interface information to be monitored. This information includes the service name, package name, class name, and method name. After adding the interface information, the user clicks the "Monitor" button on the interface, and the entered service name, package name, class name, and method name are saved to the cache database. In other words, clicking the "Monitor" button after filling in the interface information completes the first request.
[0080] After the user clicks the "Monitor" button on the interface, the following will be displayed: Figure 4The interface shown includes the interface information that the user has entered and saved, as well as the corresponding operation buttons. The user can configure the corresponding interface response settings by clicking the "Configure Simulation" button on the interface. After clicking the "Configure Simulation" button, the following will be displayed: Figure 5 The interface shown allows users to input and edit / delete simulation input parameters, simulation response values, and remaining loop counts. If a user does not need to simulate responses to the entered and saved interface information, they can delete the information by clicking the "Delete" button.
[0081] Step S230: Based on the interface information, determine whether there is a target interface to be called in the already started application to be simulated.
[0082] In some embodiments, step S230, which determines whether the target interface to be called exists in the already started application to be simulated based on the interface information, includes the following steps (S231-S232), please refer to... Figure 6 :
[0083] Step S231: Obtain the interfaces and interface information that have been called in the started application to be simulated;
[0084] Step S232: When the interface information of the target interface to be monitored contains the same interface information as the called interface, it is determined that the target interface to be called exists in the application to be simulated.
[0085] When determining whether a target interface exists in a running application to be simulated based on interface information, the calling interface information of the interface to be called in the application to be simulated can be obtained first. If the calling interface information is the same as the interface information of the target interface, it indicates that a target interface exists in the running application to be simulated. For example, the interface information of the target interface may include the service name Kcard-auth, the package name kcard.src.java.controller, the class name Auth, and the method name login. If the interface information called in the application to be simulated includes the service name Kcard-auth, the package name kcard.src.java.controller, the class name Priority, and the method name getLevel, then it can be determined that the called interface is not the target interface.
[0086] Step S240: In response to the existence of a target interface to be called in the already started application to be simulated, simulate the target interface to be called based on the at least one interface response configuration item.
[0087] The interface response configuration item includes a simulated response value. When the target interface to be called exists in the application to be simulated that has been started, a response value can be returned to the calling end that calls the target interface based on the simulated response value, thereby realizing the simulation of the target interface.
[0088] As one implementation, the interface response configuration item includes: simulated input parameter value, simulated response value, and remaining loop count. The simulation of the called target interface based on at least one of the interface response configuration items, S240, includes the following steps (S241-S243), please refer to... Figure 7 :
[0089] Step S241: Obtain the actual input parameter value corresponding to the target interface being called, and determine whether the actual input parameter value includes each field of the simulated input parameter value.
[0090] For example, if the simulated input parameter values in the interface response configuration item include {"phone":"131XXXX3335","channel":"st"}, and the actual input parameter values corresponding to the called interface include {"phone":"131XXXX3335","channel":"wx","timestamp":"1662866446869","usertype":"newUser"}, then it can be seen that the actual input parameter values corresponding to the called interface do not include all fields in the simulated input parameter values in the interface response configuration item; if the simulated input parameter values in the interface response configuration item include {"phone":"131XXXX3335"}, and the actual input parameter values corresponding to the called interface include {"phone":"131XXXX3335"}, then it can be seen that the actual input parameter values corresponding to the called interface do not include all fields in the simulated input parameter values in the interface response configuration item. The actual input parameter values of the called interface include all fields in the simulated input parameter values in the interface response configuration items. The simulated input parameter values in the interface response configuration items include {"phone":"131XXXX3335","channel":"wx","usertype":"newUser"}, while the actual input parameter values of the called interface include {"phone":"131XXXX3335","channel":"wx"}. Therefore, the actual input parameter values of the called interface do not include all fields in the simulated input parameter values in the interface response configuration items.
[0091] As one implementation method, when determining whether the actual input parameter value includes each field of the simulated input parameter value, a regular expression can be used for matching. A regular expression is a logical formula for string manipulation. It uses some predefined specific characters and combinations of these specific characters to form a "rule string". This "rule string" is used to express a filtering logic for the string.
[0092] Step S242: In response to the fact that the actual input parameter value includes each field of the simulated input parameter value, determine whether the remaining number of loops is zero.
[0093] Step S243: If the remaining number of loops is not zero, then decrease the remaining number of loops by one and send the simulated response value to the calling end that calls the target interface.
[0094] like Figure 5 As shown, Figure 5 The first line of data is an interface response configuration item. In this interface response configuration item, the simulated input parameter value is {"phone":"131XXXX3335"}, and the actual input parameter value is also {"phone":"131XXXX3335"}. Whether the actual input parameter value includes each field of the simulated input parameter value is not specified. The loop count in this interface response configuration item is 1. Then, the simulated response value can be sent to the calling end of the called interface, and the loop count 1 is changed to 0.
[0095] If the target interface has multiple interface response configuration items, such as Figure 5As shown, one target interface corresponds to three rows of data, meaning there are three interface response configuration items. If the actual input parameter value is {"phone":"131XXXX3335","channel":"wx"}, iterating through all interface response configuration items, firstly, the simulated input parameter value in the first row of interface response configuration items is {"phone":"131XXXX3335"}. It can be seen that the actual input parameter value does not include each field of the simulated input parameter value. Then, continuing to read the simulated input parameter value in the second row of interface response configuration items, based on the actual input parameter value {"phone":"131XXXX3335","channel":"wx"} and the simulated input parameter value {"phone":"131XXXX3335","channel":"wx"} in the second row of interface response configuration items, it can be seen that the actual input parameter value includes each field of the simulated input parameter value, but the loop count is 0 at this point. Continuing to read the simulated input parameter values in the third line of the interface response configuration items, based on the actual input parameter value {"phone":"131XXXX3335","channel":"wx"} and the simulated input parameter value {"phone":"131XXXX3335","channel":"st"} in the third line, it can be seen that the actual input parameter value does not include each field of the simulated input parameter value. At this point, the traversal of all interface response configuration items has been completed. Therefore, the actual response value of the called interface is returned, without simulating it.
[0096] When an interface is called multiple times in a continuous, uninterrupted business process, the above method monitors and simulates each call. If the called interface is the target interface, the simulated processing of that target interface is achieved by iterating through the user-configured interface response settings. During this iteration, if the actual input parameter value in the call data corresponding to the called interface includes all fields from the simulated input parameter values in the user-configured interface response settings, and the loop count in that interface response settings is greater than 0, then the simulated response value in that interface response settings will be returned as the simulation result of the interface. Therefore, by configuring multiple interface response settings sequentially and setting the loop count for each setting, the testing requirements for interfaces being called multiple times consecutively in a continuous, uninterrupted business process, with each response needing to simulate different response values, can be met.
[0097] For testers who call the same target interface in different business scenarios and need to simulate the corresponding response content, this can be achieved simply by differentiating the input parameter values when configuring the interface response settings. For example, if tester A configures the interface response settings for a target interface M, and tester B also configures the interface response settings for the same target interface M, then tester A can set the simulated input parameter value to {"phone":"131XXXX3335"} and the simulated response value to {"code":"10000"}; tester B can set the simulated input parameter value to {"phone":"131XXX X3336"} and the simulated response value to {"code":"40000"}. Both testers only need to use their respective mobile phone numbers to perform business process testing to achieve their respective data simulations. Tester A calls the target interface M using the phone number 131XXXX3335. Therefore, the actual input parameter value for tester A when calling the interface is 131XXXX3335, and the tester will only receive the simulated response data {"code":"10000"} corresponding to {"phone":"131XXXX3335"}. Similarly, tester B calls the target interface M using the phone number 131XXXX3336. Therefore, the actual input parameter value for tester B when calling the interface is 131XXXX3336, and the tester will only receive the simulated response data {"code":"40000"} corresponding to {"phone":"131XXXX3336"}.
[0098] As one implementation method, when implementing the above interface simulation method, the startup request carries a monitoring instruction. A dynamic tracing and monitoring tool can be installed on the server where the application to be simulated belongs. This monitoring instruction can start the dynamic tracing and monitoring tool, causing it to execute steps S220-S240 of the interface simulation method. From the perspective of program execution, the simulated application runs in a JVM virtual machine environment after startup. Since the server where the application to be simulated belongs has a JVM-specific dynamic tracing and monitoring tool, such as jvm-sandbox, installed, the monitoring instruction carried in the startup request can cause jvm-sandbox to load the monitoring simulation processing package to obtain the processing logic within the monitoring simulation processing package. When the dynamic tracking and monitoring tool executes steps S220-S240 in the interface simulation method, it can package the processing logic of this step into a monitoring simulation processing package, copy the monitoring simulation processing package to the server to which the application to be simulated belongs, and when a monitoring instruction is received, the monitoring instruction causes the dynamic tracking and monitoring tool to start and load the monitoring simulation processing package to execute the processing logic in the monitoring simulation processing package, thereby realizing the process in the interface simulation method.
[0099] For example, if the dynamic tracing and monitoring tool installed on the server hosting the application to be simulated is jvm-sandbox, then the monitoring simulation processing package can be copied to the / root / .sandbox-module directory on that server. / root / .sandbox-module is the sandbox system module directory, used to store common management modules for the sandbox. The monitoring simulation processing package in this directory will be read and used by jvm-sandbox. That is, as long as the application being simulated has added a monitoring option at startup, the processing code in the monitoring simulation processing package can be activated to monitor and simulate all interfaces of the application. Specifically, when an interface is executed, the monitoring logic in the core source code file InterfaceMonitorModule.java of the monitoring simulation processing package is triggered.
[0100] Understandably, the application to be simulated can also have other monitoring tools installed on the server to which it belongs. Besides jvm-sandbox mentioned in the example above, there are also dynamic tracing and monitoring tools such as btrace and remote-debug-agent. Therefore, in the solution of this application, different dynamic tracing and monitoring tools can be used depending on the situation, and this application does not restrict the specific dynamic tracing and monitoring tools.
[0101] The following describes a process for obtaining the monitoring simulation processing package based on the above implementation scheme.
[0102] For example, the processing logic in the monitoring processing package of this application includes the following procedures:
[0103] The first step is to monitor all interfaces in the application to be simulated. When an interface is called, proceed to the second step before the corresponding interface method is executed.
[0104] The second step is to check if the target interface method exists in the interface information stored in the cache database. If it exists, proceed to the third step; otherwise, execute the interface method directly to return the actual response value of the interface.
[0105] The third step is to determine whether the cache database contains the corresponding interface response configuration item for the interface method. If it does, proceed to the fourth step; otherwise, execute the interface method directly.
[0106] The fourth step is to read the interface response configuration items corresponding to the interface method stored in the cache database, and to simulate the interface response configuration items and the call data when the target interface is called.
[0107] The interface response configuration items can include simulated input parameter values, simulated response values, and remaining loop counts. Therefore, in the fourth step above, we can iterate through all interface response configuration items and perform the following process for each item:
[0108] The first step is to perform a regular expression match on the value of each field in the simulated input parameter value of the interface response configuration item corresponding to the target interface and the actual input parameter content in the interface method that calls the interface. If all matches are successful, proceed to the next step; if no match is successful, no operation is performed on this interface response configuration item, and the process continues to traverse the next interface response configuration item.
[0109] The second step is to determine whether the remaining number of iterations in the interface response configuration item is 0. If it is not 0, proceed to the next step; if it is 0, do not perform any operation on this interface response configuration item and continue to traverse the next interface response configuration item.
[0110] The third step is to use the simulated response value in the interface response configuration item to replace the actual response content of the interface and return it to the caller, decrement the loop count of the interface response configuration item by 1, and then end the traversal of the interface response configuration item, that is, do not continue to traverse the next interface response configuration item.
[0111] When obtaining the monitoring simulation processing package through the above processing logic, you can first obtain the code written based on the above processing logic, create a project using Java based on the code, compile and package the project to generate an executable jar package, which is the monitoring simulation processing package. For example, after creating the project based on the code, the executable jar package will be generated by the compilation and packaging command mvninstall package.
[0112] The project can be a Spring project that integrates the relevant JAR files for the jvm-sandbox executable. In one implementation, the core source code file of this project can be InterfaceMonitorModule.java, which includes the processing logic for monitoring and simulating the interface. Spring is a lightweight open-source framework.
[0113] After obtaining the monitoring simulation processing package based on the above processing logic, the monitoring simulation processing package can be sent to the server to which the application to be simulated belongs. Specifically, the monitoring simulation processing package can be copied remotely to the system directory of the server to which the application to be simulated belongs. Upon receiving a monitoring command, the monitoring simulation processing package in the system directory can be loaded.
[0114] As can be seen, the above mainly describes the technical solutions provided by the embodiments of this disclosure from a methodological perspective. To achieve the above functions, it includes corresponding hardware structures and / or software modules for executing each function. Those skilled in the art should readily recognize that, in conjunction with the modules and algorithm steps of the various examples described in the embodiments disclosed herein, the embodiments of this disclosure can be implemented in hardware or a combination of hardware and computer software. Whether a function is executed in hardware or by computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this disclosure.
[0115] This application embodiment can divide the interface simulation device into functional modules or functional units according to the above method examples. For example, each function can be divided into a separate functional module or functional unit, or two or more functions can be integrated into one processing module. The integrated module can be implemented in hardware or in software functional modules or functional units. The module or unit division in this application embodiment is illustrative and only represents one logical functional division; other division methods may be used in actual implementation.
[0116] like Figure 8The diagram shown is a structural schematic of an interface simulation device provided in an embodiment of this application. The device includes:
[0117] The request receiving module 810 is used to accept a startup request for the application to be simulated, wherein the startup request carries a monitoring instruction.
[0118] The request processing module 820 is used to start the application to be simulated based on the startup request, and to obtain the interface information of the target interface and at least one interface response configuration item based on the monitoring instruction.
[0119] The interface call monitoring module 830 is used to determine, based on the interface information, whether there is a target interface to be called in the started application to be simulated;
[0120] The interface simulation module 840 is used to simulate the called target interface based on the at least one interface response configuration item in response to the existence of a called target interface in the already started application to be simulated.
[0121] Optionally, the interface call monitoring module includes:
[0122] The interface information acquisition unit is used to acquire the interfaces and interface information called in the application to be simulated that has been started.
[0123] The target interface determination unit is used to determine that a target interface to be called exists in the application to be simulated when the interface information of the target interface to be monitored contains the same interface information as the interface to be called.
[0124] Optionally, the interface response configuration items include: simulated input parameter values, simulated response values, and remaining loop counts;
[0125] The interface simulation module includes:
[0126] The input parameter value judgment unit is used to obtain the actual input parameter value corresponding to the target interface being called, and to determine whether the actual input parameter value includes each field of the simulated input parameter value;
[0127] The remaining loop count determination unit is used to determine whether the remaining loop count is zero in response to each field in the simulated input parameter value included in the actual input parameter value.
[0128] The simulated response unit is used to decrement the remaining loop count by one if the remaining loop count is not zero, and send the simulated response value to the calling end that calls the target interface.
[0129] Optionally, the device further includes:
[0130] The configuration instruction acquisition module is used to acquire configuration instructions sent by the user, and determine the interface information of the target interface to be monitored and at least one interface response configuration item based on the configuration instructions.
[0131] Based on this, the interface simulation device includes a request receiving module for accepting a startup request from an application to be simulated, the startup request carrying a monitoring instruction; a request processing module for starting the application to be simulated based on the startup request, and obtaining interface information of the target interface and at least one interface response configuration item based on the monitoring instruction; an interface call monitoring module for determining whether a target interface to be called exists in the already started application to be simulated based on the interface information; and an interface simulation module for simulating the target interface to be called based on the at least one interface response configuration item in response to the existence of a target interface to be called in the already started application to be simulated. Users can configure the interface information of the target interface and at least one interface response configuration item to monitor and simulate multiple target interfaces, or to perform different simulations of the same target interface by setting multiple interface response configuration items, thereby improving testing efficiency.
[0132] This disclosure also provides a computer-readable storage medium storing instructions that, when executed by a computer, perform each step of the method flow shown in the above method embodiments.
[0133] Embodiments of this disclosure provide a computer program product containing instructions that, when executed on a computer, cause the computer to perform the interface simulation method in the above method embodiments.
[0134] Embodiments of this disclosure provide a chip including a processor and a communication interface, the communication interface being coupled to the processor, the processor being used to run computer programs or instructions to implement the interface simulation method as described in the above method embodiments.
[0135] The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of computer-readable storage media (a non-exhaustive list) include: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM), registers, hard disks, optical fibers, portable compact disc read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing, or any other form of computer-readable storage medium in the art. An example
[0136] An example storage medium is coupled to a processor, enabling the processor to read information from and write information to the storage medium. Of course, the storage medium can also be a component of the processor.
[0137] The processor and storage medium may reside within an application-specific integrated circuit (ASIC). In embodiments of this disclosure, the computer-readable storage medium may be any tangible medium that contains or stores a program that may be used by or in connection with an instruction execution system, apparatus, or device.
[0138] 5. Since the apparatus, device, computer-readable storage medium, and computer program product in the embodiments of this disclosure can be applied to the above methods, the technical effects that can be obtained can also be referred to the above method embodiments. The embodiments of this disclosure will not be repeated here.
[0139] In the several embodiments provided in this application, it should be understood that the disclosed systems, devices, and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For instance, the division of modules and units is only a logical functional division, and there may be other division methods in actual implementation. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed.
[0140] In addition, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0141] The above description is merely a specific embodiment of this disclosure, but the scope of protection of this disclosure is not limited thereto. Any changes or substitutions within the technical scope disclosed in this disclosure should be included within the scope of protection of this disclosure. Therefore, the scope of protection of this disclosure should be determined by the scope of the claims.
Claims
1. An interface simulation method, characterized in that, The method includes: Accept the startup request of the application to be simulated, the startup request carrying monitoring instructions; The application to be simulated is started based on the startup request, and the interface information of the target interface and at least one interface response configuration item are obtained based on the monitoring instructions; the interface response configuration item includes: simulated input parameter value, simulated response value, and remaining loop count; Based on the interface information, determine whether the target interface to be called exists in the already started application to be simulated; In response to the existence of a target interface to be called in the already started application to be simulated, the actual input parameter value corresponding to the target interface to be called is obtained, and it is determined whether the actual input parameter value includes each field in the simulated input parameter value. In response to the fact that the actual input parameter value includes each field of the simulated input parameter value, determine whether the remaining number of loops is zero; If the remaining loop count is not zero, decrease the remaining loop count by one, and send the simulated response value to the calling end that calls the target interface.
2. The method according to claim 1, characterized in that, The step of determining whether the target interface to be called exists in the already started application to be simulated based on the interface information includes: Obtain the interfaces and interface information that have been called in the application to be simulated that has been started; If the interface information of the target interface to be monitored contains the same interface information as the interface to be called, it is determined that the target interface to be called exists in the application to be simulated.
3. The method according to claim 1, characterized in that, Before obtaining the interface information of the target interface and at least one interface response configuration item based on the monitoring command, the method further includes: Obtain the configuration instructions sent by the user, and determine the interface information of the target interface to be monitored and at least one interface response configuration item based on the configuration instructions.
4. An interface simulation device, characterized in that, The device includes: The request receiving module is used to accept the startup request of the application to be simulated, wherein the startup request carries a monitoring instruction; The request processing module is used to start the application to be simulated based on the startup request, and to obtain the interface information of the target interface and at least one interface response configuration item based on the monitoring instruction; the interface response configuration item includes: simulated input parameter value, simulated response value, and remaining loop count; The interface call monitoring module is used to determine, based on the interface information, whether there is a target interface to be called in the already started application to be simulated; An interface simulation module is used to simulate the target interface to be called based on the at least one interface response configuration item in the already started application to be simulated. The interface simulation module includes: The input parameter value judgment unit is used to obtain the actual input parameter value corresponding to the target interface being called, and to determine whether the actual input parameter value includes each field of the simulated input parameter value; The remaining loop count determination unit is used to determine whether the remaining loop count is zero in response to each field in the simulated input parameter value included in the actual input parameter value. The simulated response unit is used to decrement the remaining loop count by one if the remaining loop count is not zero, and send the simulated response value to the calling end that calls the target interface.
5. The apparatus according to claim 4, characterized in that, The interface call monitoring module includes: The interface information acquisition unit is used to acquire the interfaces and interface information called in the application to be simulated that has been started. The target interface determination unit is used to determine that a target interface to be called exists in the application to be simulated when the interface information of the target interface to be monitored contains the same interface information as the interface to be called.
6. The apparatus according to claim 4, characterized in that, The device further includes: The configuration instruction acquisition module is used to acquire configuration instructions sent by the user, and determine the interface information of the target interface to be monitored and at least one interface response configuration item based on the configuration instructions.
7. An interface simulation device, characterized in that, include: A processor and a memory; wherein the memory is used to store computer execution instructions, and when the interface simulation device is running, the processor executes the computer execution instructions stored in the memory to cause the interface simulation device to perform the interface simulation method according to any one of claims 1-3.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores instructions that, when executed by the processor of the interface emulation device, cause the interface emulation device to perform the interface emulation method according to any one of claims 1-3.