Test method, device, storage medium and electronic equipment of Java interface

By scanning interfaces using Java reflection, generating test models, and automatically executing test cases, the problem of large coding workload and low efficiency in Java interface testing is solved, achieving highly efficient automated testing.

CN114328161BActive Publication Date: 2026-06-09HUAWEI DEVICE CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
HUAWEI DEVICE CO LTD
Filing Date
2020-09-29
Publication Date
2026-06-09

AI Technical Summary

Technical Problem

Existing Java interface testing methods suffer from problems such as large coding workload, high skill threshold, serious code redundancy, and low testing efficiency.

Method used

The Java reflection mechanism is used to scan interfaces, generate test models and test cases, and use a recursive algorithm to traverse paths, automatically execute function reflection calls and generate reports.

Benefits of technology

It implements automated testing of Java interfaces, automatically generates test models and test cases, and improves testing efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114328161B_ABST
    Figure CN114328161B_ABST
Patent Text Reader

Abstract

This application discloses a method, apparatus, storage medium, and electronic device for testing Java interfaces. The method includes the following steps: scanning the Java interface to obtain information about the function to be tested; generating a test model based on the function information; generating test cases based on the test model; using Java reflection to perform reflection calls on the function corresponding to the test cases; and generating a test report based on the reflection call results. By adopting the technical solution provided in this application, automated testing of Java interfaces can be achieved, automatically generating test models and test cases, automatically executing them, and outputting test reports, thus improving testing efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to a method, apparatus, storage medium, and electronic device for testing Java interfaces. Background Technology

[0002] Java (an object-oriented coding language) interface testing is used to verify whether the functions and performance indicators implemented by the external interface generated by Java coding are consistent with the interface design. The testing process usually requires a sufficient number of test cases.

[0003] Due to the large number of Java interfaces, the common testing method in existing technologies is to manually create objects and call the interfaces. The disadvantages of this method include: large coding workload, high skill threshold, serious code redundancy, high manpower and time cost, and low testing efficiency. Summary of the Invention

[0004] This application provides a method, apparatus, storage medium, and electronic device for testing Java interfaces, which can improve the testing efficiency of Java interfaces.

[0005] In a first aspect, embodiments of this application provide a method for testing a Java interface, the method comprising the following steps: scanning the Java interface to obtain information about the function to be tested; generating a test model based on the information about the function to be tested; generating test cases based on the test model; using Java reflection mechanism to perform reflection calls on the function corresponding to the test cases; and generating a test report based on the reflection call results.

[0006] The technical solution provided in this application can be used to automate the testing of Java interfaces, automatically generate test models and test cases, execute them automatically, and output test reports, thereby improving testing efficiency.

[0007] In some possible embodiments, scanning the Java interface to obtain the function information to be tested includes: parsing the interface information to be tested, using Java reflection to obtain the function information to be tested, and outputting the function list information.

[0008] In some possible embodiments, the method further includes: adding input parameters and expected results to the function list information; and / or, automatically filling in input parameters when there are no input parameters and expected results, for testing whether there are any anomalies during the call.

[0009] In some possible embodiments, generating a test model based on the function information to be tested includes: automatically generating a basic model as a test model based on the function information to be tested; and / or adjusting the basic model by adjusting the interface order, adding or deleting some interface nodes according to the interface call relationship, to generate a test model after adjusting the basic model.

[0010] In some possible embodiments, generating test cases based on the test model includes: traversing the test model using a recursive algorithm to obtain all possible paths, looping through the path list, and then generating test cases based on the verification scenario information.

[0011] In some possible embodiments, the step of using Java reflection to make reflection calls to functions based on the test cases includes: obtaining class objects and parameters using a recursive call algorithm, and making reflection calls to functions using the Java reflection call mechanism; traversing the test cases and executing the test case steps using an interface call algorithm.

[0012] In some possible embodiments, the method further includes: randomly determining a path based on the test model and performing a stability test.

[0013] Secondly, embodiments of this application provide a testing apparatus for a Java interface, comprising: a scanning unit for scanning the Java interface to obtain information about the function to be tested; a first generation unit for generating a test model based on the information about the function to be tested; a second generation unit for generating test cases based on the test model; a processing unit for using Java reflection to perform reflection calls on the function corresponding to the test cases; and a third generation unit for generating a test report based on the reflection call results.

[0014] The technical solution provided in this application can be used to automate the testing of Java interfaces, automatically generate test models and test cases, execute them automatically, and output test reports, thereby improving testing efficiency.

[0015] In some possible embodiments, the scanning unit is specifically used to parse the interface information to be tested, use Java reflection mechanism to obtain the function information to be tested, and output the function list information.

[0016] In some possible embodiments, the first generation unit is further configured to add input parameters and expected results to the function list information; and / or, when there are no input parameters and expected results, automatically fill in the input parameters to test whether there are any anomalies during the call.

[0017] In some possible embodiments, the first generation unit is specifically used to automatically generate a basic model as a test model based on the function information to be tested; and / or to adjust the basic model by adjusting the interface order, adding or deleting some interface nodes according to the interface call relationship, to generate a test model after adjusting the basic model.

[0018] In some possible embodiments, the second generation unit is specifically used to, based on the test model, traverse the test model using a recursive algorithm to obtain all possible paths and a list of looping paths, and then generate test cases based on the verification scenario information.

[0019] In some possible embodiments, the processing unit is specifically used to: obtain class objects and parameters using a recursive call algorithm, and to perform function reflection calls using the Java reflection call mechanism; traverse the test cases, and execute the test case steps using an interface call algorithm.

[0020] In some possible embodiments, the processing unit is further configured to: randomly determine a path based on the test model and perform a stability test.

[0021] Thirdly, embodiments of this application provide a computer storage medium including a computer program or instructions, which, when executed, perform some or all of the steps of the method described in the first aspect or any possible embodiment of the first aspect.

[0022] Fourthly, embodiments of this application provide a computer program product that, when run on a computer, causes the computer to perform the method described in the first aspect or any possible embodiment of the first aspect.

[0023] Fifthly, embodiments of this application provide an electronic device, including a processor and a memory; the processor is configured to execute a computer program or instructions stored in the memory, and when the computer program or instructions are executed, to perform some or all of the steps of the method described in the first aspect or any possible embodiment of the first aspect.

[0024] The technical solution provided in this application can be used to automate the testing of Java interfaces, automatically generate test models and test cases, execute them automatically, and output test reports, thereby improving testing efficiency. Attached Figure Description

[0025] Figure 1 This is a flowchart illustrating a testing method for a Java interface provided in one embodiment of this application.

[0026] Figure 2This is a schematic diagram of a test model generated in one embodiment of this application.

[0027] Figure 3 This is a schematic diagram of a test model generated by another embodiment of this application.

[0028] Figure 4 This is a schematic diagram of a function reflection call provided in one embodiment of this application. Detailed Implementation

[0029] Embodiments of this application will now be described in more detail with reference to the accompanying drawings. While some embodiments of this application are shown in the drawings, it should be understood that this application can be implemented in various forms and should not be construed as limited to the embodiments set forth herein. These embodiments are provided to provide a more thorough and complete understanding of this application. It should be understood that the drawings and embodiments of this application are for illustrative purposes only and are not intended to limit the scope of protection of this disclosure.

[0030] It should be understood that the steps described in the method embodiments of this application may be performed in different orders and / or in parallel. Furthermore, the method embodiments may include additional steps and / or omit the steps shown. The scope of this disclosure is not limited in this respect.

[0031] The term "comprising" and its variations as used in this application are open-ended, meaning "including but not limited to". The term "based on" means "at least partially based on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". Definitions of other terms will be given in the description below.

[0032] It should be noted that the concepts of "first" and "second" mentioned in this application are only used to distinguish different devices, modules or units, and are not used to limit the order of functions performed by these devices, modules or units or their interdependencies.

[0033] It should be noted that the terms "a" and "a plurality of" used in this disclosure are illustrative rather than restrictive, and those skilled in the art should understand that, unless otherwise expressly indicated in the context, they should be understood as "one or more".

[0034] The names of the messages or information exchanged between multiple devices in the embodiments of this application are for illustrative purposes only and are not intended to limit the scope of these messages or information.

[0035] like Figure 1 The diagram shown is a flowchart of a testing method for a Java interface provided in one embodiment of this application. Figure 1As shown, the method may include the following steps:

[0036] 101. Scan the Java interface to obtain information about the function to be tested.

[0037] In some possible implementations, this can be achieved by parsing the interface information to be tested, using Java reflection to obtain the function information to be tested, and outputting a list of functions.

[0038] Specifically, in some possible implementations, the class to be tested can be obtained by parsing the dex or jar package, and then the function information of the class to be tested can be obtained by using the Java reflection mechanism. The function information to be tested includes: function name, function modifier, parameter type, return type, etc. Then, the function list information is output based on the function information to be tested.

[0039] For example, if the code corresponding to a Java interface includes the following content.

[0040] public class ModelDemo{

[0041] public static ModelDemo getInstance(){

[0042] return new ModelDemo();

[0043] }

[0044] public int testInterface(ShowCase showCase,DemoInterface inter){

[0045] showCase.getValue();

[0046] inter.callBack();

[0047] return 1;

[0048] }

[0049] public int[]getOptionAge(){

[0050] int[]ret = {20, 30, 40};

[0051] return ret;

[0052] }

[0053] public String[]getOptionSex(){

[0054] String[]ret={"Male","Female"};

[0055] return ret;

[0056] }

[0057] public voidProcess(int age,String sex){

[0058] Log.d(tag:"Test",msg:"Process:Age="+age+",Sex="+sex);

[0059] }

[0060] public int getSum(int arg1,int arg2){

[0061] return arg1 + arg2;

[0062] }

[0063] public long getSum(int[]array){

[0064] long sum = 0;

[0065] for(int value:array){

[0066] sun+ = value;

[0067] }

[0068] raturn sum;

[0069] }

[0070] After parsing the Java interface information above, the function list information shown in Table 1 can be obtained. Specifically, the functions getInstance, getOptionSex, getOptionAge, getSum, testInstance, getSum, and Process can be parsed, along with information such as class name, modifiers, return type, and parameters, as detailed in Table 1.

[0071] Table 1

[0072] Class Name Modifier Return type function name parameter ModelDem publicstatic ModelDemo getlnstance ModelDem public String[] getOptionSex ModelDem public int[] getOptionAge ModelDem public int getSum int, int ModelDem public int testInterFace ShowCase.Demolnterface ModelDem public long getSum int[] ModelDem public void Process int, String

[0073] In some possible implementations, test scenarios can be added. For example, inputs and expected results can be added to the output function list to test the basic functionality of the function. When there are no input parameters or expected results, input parameters can be filled in during testing to check for any exceptions during the call. As shown in Table 2, for the function `getSum` with an int return value, normal test scenarios include: an expected result of 301 when the input is 100 and 200; an expected result of 10 when the input is 5 and 5; and an exception test scenario when the input is 1 and 0. For the interface function `getSum` with a long return value, normal test scenarios include: an input array, for example, the input array could contain 1, 2, 3, and 4, with an expected result of 10.

[0074] Table 2 Input Parameters and Expected Results

[0075]

[0076] 102. Generate a test model based on the information of the function to be tested.

[0077] In practice, a basic model can be automatically generated based on the function information to be tested. The model can be an N-ary tree and can be represented using a UML component diagram. Elements included can include nodes, groups, and connections. The UML diagram corresponding to Table 1 can be shown below. Figure 2 As shown.

[0078] In some possible implementations, the model can be adjusted according to the design. For example, if the software code is adjusted as shown below, a function information table similar to Table 1 can be generated based on the code below, and then a function information table can be generated based on the generated function information table. Figure 3 The test model shown.

[0079] Start --> [RootNode]

[0080] [RootNode]-->[ModelDemo-ModelDEMO_getInstance]

[0081] package "ModelDemo"{

[0082] [ModelDemo-intArray_getOptionAge]

[0083] [ModelDemo-StringArray_getOptionSex]

[0084] ()ModelDemo_End

[0085] [ModelDemo-ModelDemo_getInstance]-->ModelDemo_Begin

[0086] }

[0087] [ModelDemo_End]-->[ModelDemo-void_Process_int_string]

[0088] [RootNode]#LightBlue

[0089] @enduml

[0090] SetProperty:

[0091] RootNode:NodeType=AssistOperation;

[0092] ModelDemo:RelationType=Coexist;

[0093] ModelDemo-intArray_getOptionAge:NodeType=Query;OutParameter=age;

[0094] ModelDemo-StringArray_getOptionSex:NodeType=Query; OutParameter=sex;

[0095] ModelDemo-void_Process_int_String:InParameter=age,sex;

[0096] end

[0097] 103. Generate test cases based on the test model.

[0098] In some possible implementations, based on the test model, a recursive algorithm is used to traverse the model to obtain all possible paths; the path list is looped, and then, based on the verification scenario information, parameters are combined to finally generate test cases.

[0099] The test model generated from the adjusted code in the previous steps can produce the following test models:

[0100] The query interface test cases include:

[0101] Action=RootNode; Action=ModelDemo-ModelDemo_getInstance; Action=ModelDemo-intArray_getOptionAge; QueryExpect=age:[20,30,40]

[0102] Action=RootNode; Action=ModelDemo-ModelDemo_getInstance; Action=ModelDemo-StringArray_getOptionSex; QueryExpect=sex:[Female,Male]

[0103] Expected results are automatically saved and, after manual review, can be directly used to verify those results. Partial list of functional interface test cases includes:

[0104] Action=RootNode; Action=ModelDemo-ModelDemo_getInstance; age=20; sex=Female; Action=ModelDemo-void_Process_int_String;

[0105] Action=RootNode; Action=ModelDemo-ModelDemo_getInstance; age=30; sex=Male; Action=Model Demo-void_Process_int_String;

[0106] When generating test cases, various combination methods are supported, such as: single choice combination, basic choice combination, full combination, and pair-wise combination.

[0107] 104. Using Java reflection, perform reflection calls on the functions corresponding to the test cases.

[0108] In some possible implementations, a recursive call algorithm is used to obtain the class object and parameters, and then the Java reflection call mechanism is used to perform function reflection calls.

[0109] It should be noted that, Figure 4 The meanings of the related words in the text include:

[0110] No parameters means that the function to be called has no parameters, for example: public int[] getOptionAge().

[0111] The given values ​​mean that the values ​​of the function to be called have been set, for example: age = 20; sex = Female; Action = ModelDemo-void_Process_int_String, the function Process, with parameters 20 and Female respectively.

[0112] Constructing an object refers to creating a Java object instance, such as an int type instance, int age = 20.

[0113] The default value constructor means that if no value is given, the system default value is used, and the current model value is the maximum value of the basic type.

[0114] Simple types refer to the basic types in the Java language, such as: integers: byte, short, int, long; character: char; floating-point types: float, double; boolean: boolean, etc.

[0115] Instance retrieval method: refers to calling Figure 4 The logic of the area corresponding to the dashed box on the left.

[0116] like Figure 4 As shown, function reflection invocation may include the following steps: (1) Determine whether the function is a static function. If so, set the function object to null; otherwise, call the instance acquisition method to obtain the object of the class to which the function belongs, such as... Figure 4 The process corresponding to the dashed box on the left: (2) Get the function; (3) Use Java reflection mechanism to reflectively call the function. If the function returns a non-simple type, the returned object will be inserted into the resource pool for subsequent use.

[0117] as well as Figure 4 The dashed box on the right shows the process for obtaining the parameters required by the function.

[0118] It should be noted that, as Figure 4 As shown, the instance acquisition method may include the following steps:

[0119] (1) Check if there is an instance of the class in the resource pool. If there is, return the instance directly.

[0120] (2) Search the scanned list of functions to determine if there is a function that can return an instance of the type. If so, iterate through the list of functions and use function reflection to obtain an instance of the type.

[0121] (3) If the class instance still cannot be obtained, then construct the class instance through reflection using the class constructor;

[0122] (4) If the instance obtained by all the previous methods is null, an error will be reported.

[0123] The parameter acquisition method may include the following steps:

[0124] (1) Determine if there are any input parameters. If not, return directly. If there are, further determine if a value is given.

[0125] (2) Determine whether a value is given. If so, construct a specific object based on the value.

[0126] (3) Determine if it is a simple type. If it is, construct a simple type parameter; if it is a complex type, call the instance acquisition method to obtain the instance.

[0127] In some possible embodiments, the functionality of the function can also be verified. When performing functional verification, the following steps may be included: traversing the test cases generated by the model, executing the test case steps using the interface call algorithm, capturing the verification results or exception information during the execution process, and outputting the test execution results to the final test report.

[0128] In some possible embodiments, the stability of the function can also be verified. The stability verification can include the following steps: based on the test model, randomly select the path and parameters for execution to complete the stability verification.

[0129] 105. Generate a test report based on the reflection call results.

[0130] For example, when testing the example given in step 1 above, you can complete the test by only providing the test package name or class name, and you can directly output the test report, as shown in Tables 3, 4 and 5.

[0131] Table 3 Functional Test Table (The default value for the basic type is the maximum value for that type)

[0132]

[0133] Table 4 shows the specific tests (where Repeat corresponds to repeated call tests, Press corresponds to concurrent call tests, Fuzz corresponds to input parameter tests, and QueryModify corresponds to deep copy verification).

[0134]

[0135]

[0136] Table 5 Performance Statistics

[0137] Interface function (time unit: ms) Average time Minimum time Maximum time Execution count ModelDemo-int_getSum_int_int 0 0 1 303 ModelDemo-int_testInterface_ShowCase_Demolnterface 2 1 3 303 ModelDemo-intArray_getOptionAge 0 0 1 303

[0138] In some possible implementations, trend charts can be generated based on performance statistics tables.

[0139] Using the technical solution provided in this application, the input parameters and expected results in some possible implementations are shown in Table 6.

[0140] Table 6 Input Parameters and Prediction Results

[0141]

[0142] Using the solution provided in the embodiments of this application, the output test report can be as shown in Table 7.

[0143] Table 7 Test Report

[0144]

[0145]

[0146] for Figure 3 The corresponding test model and the output functional test report are shown in Table 8.

[0147] Table 8 Functional Test Report

[0148]

[0149] When performing Java interface testing using the technical solution provided in the embodiments of this application, test models and test cases can be automatically generated based on function information, and tests can be automatically executed and test reports can be output, thereby improving testing efficiency.

[0150] This application embodiment also provides a testing device for a Java interface, including: a scanning unit, a first generation unit, a second generation unit, a processing unit, and a third generation unit. The scanning unit is used to scan the Java interface to obtain information about the function to be tested; the first generation unit is used to generate a test model based on the information about the function to be tested; the second generation unit is used to generate test cases based on the test model; the processing unit is used to use Java reflection to perform reflection calls on the function corresponding to the test cases; and the third generation unit is used to generate a test report based on the reflection call results. The execution process of each module is described in the preceding method embodiments and will not be repeated here.

[0151] When performing Java interface testing using the technical solution provided in the embodiments of this application, test models and test cases can be automatically generated based on function information, and tests can be automatically executed and test reports can be output, thereby improving testing efficiency.

[0152] In some possible implementations, the scanning unit is specifically used to parse the interface information to be tested, use Java reflection mechanism to obtain the function information to be tested, and output the function list information.

[0153] In some possible implementations, the first generation unit is also used to add input parameters and expected results to the function list information; and / or, when there are no input parameters and expected results, automatically fill in the input parameters to test whether there are any anomalies during the call.

[0154] The first generation unit is specifically used to automatically generate a basic model as a test model based on the function information to be tested; and / or to adjust the basic model by adjusting the interface order, adding or deleting some interface nodes according to the interface call relationship, to generate a test model after adjusting the basic model.

[0155] In some possible implementations, the second generation unit is specifically used to, based on the test model, traverse the test model using a recursive algorithm to obtain all possible paths, a list of looping paths, and then generate test cases based on the verification scenario information.

[0156] In some possible implementations, the processing unit is specifically used to: obtain class objects and parameters using a recursive call algorithm, and to perform function reflection calls using the Java reflection call mechanism; traverse the test cases, and execute the test case steps using an interface call algorithm.

[0157] In some possible implementations, the processing unit is further configured to: randomly determine a path based on the test model and perform a stability test.

[0158] This application also provides a computer storage medium, including a computer program or instructions, which, when executed, performs a test method for a Java interface. The method includes:

[0159] Scan the Java interface to obtain information about the function to be tested;

[0160] Based on the information of the function to be tested, a test model is generated;

[0161] Test cases are generated based on the test model;

[0162] The Java reflection mechanism is used to perform reflection calls on the functions corresponding to the test cases;

[0163] A test report is generated based on the reflection call results.

[0164] When performing Java interface testing using the technical solution provided in the embodiments of this application, test models and test cases can be automatically generated based on function information, and tests can be automatically executed and test reports can be output, thereby improving testing efficiency.

[0165] In some possible implementations, scanning the Java interface to obtain information about the function to be tested includes:

[0166] The interface information to be tested is parsed, and the Java reflection mechanism is used to obtain the function information to be tested, and the function list information is output.

[0167] In some possible implementations, the method further includes: adding input parameters and expected results to the function list information; and / or, automatically filling in input parameters when there are no input parameters and expected results, for testing whether there are any anomalies during the call.

[0168] In some possible implementations, generating a test model based on the function information to be tested includes:

[0169] Based on the information of the function to be tested, a basic model is automatically generated as a test model;

[0170] And / or, adjust the base model by adjusting the interface order, adding or deleting some interface nodes according to the interface call relationship, and generate a test model after adjusting the base model.

[0171] In some possible implementations, generating test cases based on the test model includes:

[0172] Based on the test model, a recursive algorithm is used to traverse the test model to obtain all possible paths, a list of looping paths, and then test cases are generated based on the verification scenario information.

[0173] In some possible implementations, the step of using Java reflection to make a reflection call on the function based on the test case includes:

[0174] The algorithm uses recursive calls to obtain class objects and parameters, and Java reflection calls to invoke functions.

[0175] The test cases are traversed, and the steps of the test cases are executed using the interface call algorithm.

[0176] In some possible implementations, the method further includes: randomly determining a path based on the test model and conducting a stability test.

[0177] This application also provides a computer program product, which, when run on a computer, enables the computer to execute a Java interface testing method. The method includes:

[0178] Scan the Java interface to obtain information about the function to be tested;

[0179] Based on the information of the function to be tested, a test model is generated;

[0180] Test cases are generated based on the test model;

[0181] The Java reflection mechanism is used to perform reflection calls on the functions corresponding to the test cases;

[0182] A test report is generated based on the reflection call results.

[0183] When performing Java interface testing using the technical solution provided in the embodiments of this application, test models and test cases can be automatically generated based on function information, and tests can be automatically executed and test reports can be output, thereby improving testing efficiency.

[0184] In some possible implementations, scanning the Java interface to obtain information about the function to be tested includes:

[0185] The interface information to be tested is parsed, and the Java reflection mechanism is used to obtain the function information to be tested, and the function list information is output.

[0186] In some possible implementations, the method further includes: adding input parameters and expected results to the function list information; and / or, automatically filling in input parameters when there are no input parameters and expected results, for testing whether there are any anomalies during the call.

[0187] In some possible implementations, generating a test model based on the function information to be tested includes:

[0188] Based on the information of the function to be tested, a basic model is automatically generated as a test model;

[0189] And / or, adjust the base model by adjusting the interface order, adding or deleting some interface nodes according to the interface call relationship, and generate a test model after adjusting the base model.

[0190] In some possible implementations, generating test cases based on the test model includes:

[0191] Based on the test model, a recursive algorithm is used to traverse the test model to obtain all possible paths, a list of looping paths, and then test cases are generated based on the verification scenario information.

[0192] In some possible implementations, the step of using Java reflection to make a reflection call on the function based on the test case includes:

[0193] The algorithm uses recursive calls to obtain class objects and parameters, and Java reflection calls to invoke functions.

[0194] The test cases are traversed, and the steps of the test cases are executed using the interface call algorithm.

[0195] In some possible implementations, the method further includes: randomly determining a path based on the test model and conducting a stability test.

[0196] This application also provides an electronic device, including a processor and a memory; the processor is configured to execute a computer program or instructions stored in the memory, and when the computer program or instructions are executed, to execute a test method for a Java interface. The method includes:

[0197] Scan the Java interface to obtain information about the function to be tested;

[0198] Based on the information of the function to be tested, a test model is generated;

[0199] Test cases are generated based on the test model;

[0200] The Java reflection mechanism is used to perform reflection calls on the functions corresponding to the test cases;

[0201] A test report is generated based on the reflection call results.

[0202] When performing Java interface testing using the technical solution provided in the embodiments of this application, test models and test cases can be automatically generated based on function information, and tests can be automatically executed and test reports can be output, thereby improving testing efficiency.

[0203] In some possible implementations, scanning the Java interface to obtain information about the function to be tested includes:

[0204] The interface information to be tested is parsed, and the Java reflection mechanism is used to obtain the function information to be tested, and the function list information is output.

[0205] In some possible implementations, the method further includes: adding input parameters and expected results to the function list information; and / or, automatically filling in input parameters when there are no input parameters and expected results, for testing whether there are any anomalies during the call.

[0206] In some possible implementations, generating a test model based on the function information to be tested includes:

[0207] Based on the information of the function to be tested, a basic model is automatically generated as a test model;

[0208] And / or, adjust the base model by adjusting the interface order, adding or deleting some interface nodes according to the interface call relationship, and generate a test model after adjusting the base model.

[0209] In some possible implementations, generating test cases based on the test model includes:

[0210] Based on the test model, a recursive algorithm is used to traverse the test model to obtain all possible paths, a list of looping paths, and then test cases are generated based on the verification scenario information.

[0211] In some possible implementations, the step of using Java reflection to make a reflection call on the function based on the test case includes:

[0212] The algorithm uses recursive calls to obtain class objects and parameters, and Java reflection calls to invoke functions.

[0213] The test cases are traversed, and the steps of the test cases are executed using the interface call algorithm.

[0214] In some possible implementations, the method further includes: randomly determining a path based on the test model and conducting a stability test.

[0215] The specific method embodiments described above, as well as the explanations, descriptions, and extensions of the technical features in the embodiments, are also applicable to the execution of the method in the device, and will not be elaborated upon in the device embodiments.

[0216] It should be understood that the division of the various modules in the above device is merely a logical functional division. In actual implementation, they can be fully or partially integrated into a single physical entity, or they can be physically separated. For example, each module can be a separate processing element, or it can be integrated into a chip in the terminal. Alternatively, it can be stored as program code in the controller's storage element, and called and executed by a processing element of the processor. Furthermore, the modules can be integrated together or implemented independently. The processing element mentioned here can be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method or each module can be completed through the integrated logic circuit in the hardware of the processor element or through software instructions. This processing element can be a general-purpose processor, such as a central processing unit (CPU), or one or more integrated circuits configured to implement the above method, such as one or more application-specific integrated circuits (ASICs), one or more digital signal processors (DSPs), or one or more field-programmable gate arrays (FPGAs), etc.

[0217] It should be understood that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data used can be interchanged where appropriate so that the embodiments described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or modules is not necessarily limited to those steps or modules explicitly listed, but may include other steps or modules not explicitly listed or inherent to such processes, methods, products, or apparatus.

[0218] The above description discloses only one preferred embodiment of the present invention, and should not be construed as limiting the scope of the present invention. Those skilled in the art will understand that all or part of the processes of the above embodiments can be implemented, and equivalent changes made in accordance with the claims of the present invention are still within the scope of the invention.

Claims

1. A method for testing a Java interface, characterized in that, The method includes: Scan the Java interface to obtain information about the function to be tested. The information about the function to be tested includes at least one of the following: function name, function modifier, parameter type, or return type. Based on the information of the function to be tested, a test model is generated; Add input parameters and expected results to the function list information; and / or, When there are no input parameters or expected results, the input parameters are automatically filled in to test whether there are any exceptions during the call; Test cases are generated based on the test model; The Java reflection mechanism is used to perform reflection calls on the functions corresponding to the test cases; A test report is generated based on the reflection call results; The step of generating a test model based on the function information to be tested includes: Based on the information of the function to be tested, a basic model is automatically generated as a test model; And / or, the basic model is adjusted by adjusting the interface order, adding or deleting some interface nodes according to the interface call relationship, to generate a test model after adjusting the basic model; wherein, the test model adopts an N-ary tree; The generation of test cases based on the test model includes: Based on the test model, a recursive algorithm is used to traverse the test model to obtain all possible paths, a list of looping paths, and then test cases are generated based on the verification scenario information.

2. The method according to claim 1, characterized in that, The scanning of the Java interface yields information about the function to be tested, including: The interface information to be tested is parsed, and the Java reflection mechanism is used to obtain the function information to be tested, and the function list information is output.

3. The method according to claim 1, characterized in that, The step of using Java reflection to invoke functions based on the test cases includes: The algorithm uses recursive calls to obtain class objects and parameters, and Java reflection calls to invoke functions. The test cases are traversed, and the steps of the test cases are executed using the interface call algorithm.

4. The method according to any one of claims 1 to 3, characterized in that, Also includes: Based on the test model, a path is randomly determined, and a stability test is conducted.

5. A testing device for a Java interface, characterized in that, include: The scanning unit is used to scan the Java interface to obtain the function information to be tested. The function information to be tested includes at least one of the following: function name, function modifier, parameter type, or return type. The first generation unit is used to generate a test model based on the function information to be tested; The first generation unit is further configured to add input parameters and expected results to the function list information; And / or, automatically fill in the input parameters when there are no input parameters and expected results, to test whether there are any exceptions during the call; The second generation unit is used to generate test cases based on the test model; The processing unit is used to perform reflection calls on the function corresponding to the test case using the Java reflection mechanism; The third generation unit is used to generate a test report based on the reflection call result; The first generation unit is specifically used to automatically generate a basic model as a test model based on the function information to be tested; and / or to adjust the basic model by adjusting the interface order, adding or deleting some interface nodes according to the interface call relationship, to generate a test model after adjusting the basic model; wherein the test model adopts an N-ary tree; The second generation unit is specifically used to, based on the test model, use a recursive algorithm to traverse the test model to obtain all possible paths, a list of looping paths, and then generate test cases based on the verification scenario information.

6. The testing apparatus according to claim 5, characterized in that, The scanning unit is specifically used to parse the interface information to be tested, use Java reflection mechanism to obtain the function information to be tested, and output the function list information.

7. The testing apparatus according to claim 5, characterized in that, The processing unit is specifically used to obtain class objects and parameters using a recursive call algorithm, and to perform function reflection calls using the Java reflection call mechanism; to traverse the test cases, and to execute the test case steps using an interface call algorithm.

8. The testing apparatus according to any one of claims 5 to 7, characterized in that, The processing unit is also used to: randomly determine a path based on the test model and perform stability testing.

9. A computer storage medium, characterized in that, It includes a computer program or instructions, which, when executed, perform the method as described in any one of claims 1 to 4.

10. A computer program product, characterized in that, When the computer program product is run on a computer, it causes the computer to perform the method as described in any one of claims 1 to 4.

11. An electronic device, characterized in that, It includes a processor and a memory; the processor is configured to execute a computer program or instructions stored in the memory, wherein when the computer program or instructions are executed, the method described in any one of claims 1 to 4 is performed.