One, foreword
When the front and rear ends are separated from each other in development, the back-end staff needs to connect with the front-end staff to test and debug the interface and verify the correctness and availability of the interface after completing the development of the system interface. This requires that the progress of front-end development and back-end development be basically consistent. If the progress of either party fails to keep up, the test of functional modules cannot be completed in time. As a back-end developer, after an interface is independently developed, the developer needs to pass the test and then submit it to the tester for testing. Otherwise, the business process to the tester will not work at all, or there will be too many bugs. There are many excellent, well-developed interface testing tools on the market, such as SoapUI, Postman, etc., that can effectively help back-end developers test interfaces independently. The Postman interface test tool is used. The following uses the userLogin login interface of the POST request as an example.Copy the code
Install and use Postman
1. You can download this directly from the official website. We can use the free version to meet the basic interface test.Copy the code
Free version website link: www.getpostman.com/postman to download a good installer Postman – Win64 – XXX – Setup. Exe, double-click the install. You are advised to install it on a disk other than the system disk, that is, drive C. You are advised to use an English path instead of a Chinese path.
2. Open Postman as shown in Figure 2-1, close the page and skip the account registration. The tool can be used directly when the user is not logged in. Next comes the boot page, the boot page for development tools like IDEA, Android Studio, etc. Close here.Copy the code
Figure 2-1 Account registration
3. Go to the Main page of Postman and create a project interface set
Here's one I've used before, so there's a couple of folders on the left side. Select Collection under function menu New to create a Collection of project interfaces. Create A hundred million new project, write A Description of the project in the Description section.Copy the code
Figure 2-3Postman home page
Figure 2-4 Creating project interface set Figure 4. Create project module folder
Generally, a project will be developed in multiple management modules or functional modules. Subfolders can be created according to the specific situation of the project to facilitate interface management. Right-click on the "A Hundred Million" project and create the asset Management assets folder and user User Management folder. The tested interface instance can be saved under the management module.Copy the code
Figure 2-5 Creating an asset management folder 5. Background login interface demonstration
Develop the background interface, write the correct and rigorous logic code to handle the front-end request. Here the login interface code is as follows. Run the background development project to make the interface accessible. Interface request path is: http://localhost:8080/manage/app/user/loginCopy the code
@Controller @RequestMapping(“/manage/app/”) @Scope(“prototype”) public class AppUserController {
/** * log */ public static Logger customFile = loggerfactory.getLogger (“manageCustomFile”);
@Resource private AppUserService appUserService;
/** * User login * @param baseResult * @return */ @requestMapping (value = "user/login", produces = "application/json; /** * user login * @param baseResult * @return */ @requestMapping (value = "user/login", produces = "application/json; charset=UTF-8", method = RequestMethod.POST) @ResponseBody public BaseResult loginUser(@RequestBody BaseResult baseResult) { Customfile.info ("appId:"+ baseresult.gettreasureId ()+ "User login :"+ baseresult.toString ()); return appUserService.loginUser(baseResult); }Copy the code
} 6. Create an interface test
Click the '+' button in the Content section of Postman's home page to add an interface test. In the first part of Figure 2-6, enter the interface request path. Select A request mode from the drop-down list on the left. Here, select POST. Interface request path to http://localhost:8080/manage/app/user/login. The second part is the request Body, which contains the request parameters required by the interface. Here, select RAW data and use JSON data as the request data. Click the blue Send button to Send the request, and the background project interface receives the request and responds to return the execution result. Here the background processing data return result is still JSON data, this depends on the actual project, here only for demonstration. The third part is the interface response body, which is the data that the interface processes and returns in response to the request. Click Pretty to automatically match the data format returned by the interface and beautify the display of the data to make the data look more intuitive. If the interface test fails, you can use the PreView view to view the page information of the browser responding to the interface, and the Header to learn about parameters such as the request status. In general, if there is an exception, we should directly go to the background to see the exception information and project log of the console to handle and solve the exception, and then conduct interface test.Copy the code
Figure 2-6 Interface test diagram After the interface test passes, press Ctrl+S to save the interface test instance, enter the interface test name and description, and select a path for saving the test instance. Save the test instance in the user management module folder. After all the tests of the interfaces of the project pass, you can right-click Export in A Hundred Million project to Export the interface document and save it as A JSON interface test file. This file can be imported and viewed through Postman.
Three, the request method
For back-end interface development, there must be more than a POST request mode, Postman also supports a variety of request modes. When creating an interface test instance, you can select the request mode of the interface, as shown in Figure 3-1.Copy the code
Figure 3-1 Interface request mode The request type can simulate Form text submission, Form file submission, RAW raw data submission, and binary file submission. Raw supports different file formats, including JSON, TXT, HTML, and other file formats.
Figure 3-2 Data request types
Demonstration of capital record interface
The background development interface is as follows, and the server is deployed to run the background project. Interface request path is: http://localhost:8080/manage/app/user/asset/withdrawRecordCopy the code
@Controller @RequestMapping(“/manage/app/”) @Scope(“prototype”) public class AppUserAssetController {
/** * log */ public static Logger customFile = loggerfactory.getLogger ("manageCustomFile"); @Resource private AppUserAssetService appUserAssetService; / * * * for withdrawal record * the money in my page * @ param baseResult * @ return * / @ RequestMapping (value = "user/asset/withdrawRecord", produces = "application/json; charset=UTF-8", method = RequestMethod.POST) @ResponseBody public BaseResult getUserWithdrawRecord(@RequestBody BaseResult baseResult){ Customfile.info ("appId:"+ baseresult.gettreasureId () + "Get user withdrawal record "+ baseresult.toString ()); return appUserAssetService.getUserWithdrawRecord(baseResult); }Copy the code
} interface test requests for POST new funds, the request URL to http://localhost:8080/manage/app/user/asset/withdrawRecord for JSON request body types of raw data, click the Send request interface Figure 3-3 shows the response result of the interface
Finally: [Tutorials that may help you]
These materials should be the most comprehensive and complete preparation warehouse. This warehouse also accompanied me through the most difficult journey. I hope it can also help you! Everything must be early, especially in the technology industry, must improve the technical foundation.A private message that requires information