基于Spring Boot的API测试
互联网产品的测试策略现在很多都会存在API测试、轻量级GUI测试、轻量级单元测试等。API测试其实我们一开始想得最多的图形化工具应该是postman、jmeter等。如果使用最简单的get方法,还可以直接通过使用CURL命令(即命令行工具cURL)。1.API测试的基本步骤不管使用什么API测试工具,API测试的基本步骤大体一致:1.准备测试数据2.通过API测试工具,发起对被测API的reque
互联网产品的测试策略现在很多都会存在API测试、轻量级GUI测试、轻量级单元测试等。API测试其实我们一开始想得最多的图形化工具应该是postman、jmeter等。如果使用最简单的get方法,还可以直接通过使用CURL命令(即命令行工具cURL)。
1.API测试的基本步骤
不管使用什么API测试工具,API测试的基本步骤大体一致:
1.准备测试数据
2.通过API测试工具,发起对被测API的request
3.验证返回结果的response
2.基于Spring Boot构建的API
我们平时在工作中,接触得最多的是用JAVA框架Spring boot框架开发的简单的Restful API。
Springboot建议的目录结果如下:root package结构-com.example.myproject
-
1.Application.java:根目录,主要用于做一些框架配置
-
2.domain目录主要用于实体(Entity)与数据访问层(Repository)
-
3.service层主要是业务类代码
-
4.controller负责页面访问控制
下面例子是一个Account API的功能,基于提供的ID值创建一个Account对象,并返回这个新创建Account对象。
git代码地址:https://github.com/SpectoLabs/spring-cloud-contract-blog/tree/master/account-service
- 1.Services类
AccountService.getById(String id)方法,具体逻辑就是返回一个以传入ID为ID的Account对象
package com.demo.account.services;
import com.demo.account.domains.Account;
import org.springframework.stereotype.Service;
@Service
public class AccountService {
public Account getById(String id) {
return new Account(id, "friends", "tom@api.io");
}
}
- 2.controller类
@RestController是指controller里面的方法都是以json格式输出,不用在写什么jackjson配置
@RequestMapping(method = RequestMethod.GET, value = “/account/{id}”):说明API的endpoint以及对应的操作是GET方法
public Account getAccount(@PathVariable String id) {return accountService.getById(id); }:说明了GET方法具体的业务逻辑是由accountService.getById()方法实现的。
package com.demo.account.controllers;
import com.demo.account.domains.Account;
import com.demo.account.services.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AccountController {
private final AccountService accountService;
@Autowired
public AccountController(AccountService accountService) {
this.accountService = accountService;
}
@RequestMapping(method = RequestMethod.GET, value = "/account/{id}")
public Account getAccount(@PathVariable String id) {
return accountService.getById(id);
}
}
- 3.domains类
package com.demo.account.domains;
public class Account {
private final String id;
private final String type;
private final String email;
public Account(String id, String type, String email) {
this.id = id;
this.type = type;
this.email = email;
}
public String getId() {
return id;
}
public String getType() {
return type;
}
public String getEmail() {
return email;
}
}
最后: 可以关注公众号:伤心的辣条 ! 进去有许多资料共享!资料都是面试时面试官必问的知识点,也包括了很多测试行业常见知识,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。
在这里我向大家推荐一个自动化学习交流群。交流学习群号:914172719 里面会分享一些资深架构师录制的测试有关视频录像…
如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一键三连哦!
好文推荐
转行面试,跳槽面试,软件测试人员都必须知道的这几种面试技巧!
面试经:一线城市搬砖!又面软件测试岗,5000就知足了…
面试官:工作三年,还来面初级测试?恐怕你的软件测试工程师的头衔要加双引号…
更多推荐
所有评论(0)