一、前言
在java开发中经常需要造数据进行测试接口,这里记录一下常用的通过造数据测试接口的方法。

二、一般的接口传参方式
1、接口的方式最好是使用JSON或者map的方式,这样的好处是传参可以灵活伸缩,返回的结果也最好是JSON或者map的方式
- /**
- * 获取授权访问令牌accessToken
- * @param params
- * @return
- */
- @PostMapping("token")
- JSONObject getAccessToken(@RequestBody Map
params) ;
2、组装参数方法
- /**
- * 组装获取Access Token参数
- */
- public Map
getACTokenParam() { - Map
map = new HashMap<>(); - String timestamp = DateTimeUtils.getDateStr();
- client_secret+timestamp+client_id+username+password+grant_type+scope+client_secret
- String clientSecM = MD5Util.getMd5(clientSecret).toUpperCase();
- String passwordM = MD5Util.getMd5(password).toUpperCase();
- String subString = (clientSecM + timestamp + clientId + userName + passwordM + grantType + scope + clientSecM);
- String sign = MD5Util.getMd5(subString).toUpperCase();
- map.put("grant_type", grantType);
- map.put("client_id", clientId);
- map.put("timestamp", timestamp);
- map.put("username", userName);
- map.put("password", passwordM);
- map.put("scope", scope);
- map.put("sign", sign);
- return map;
- }
3.常见的造数据
随机生成4位数
Random rand = new Random();
int randomNum = rand.nextInt(9000) + 1000;
休眠
//休眠3秒
Thread.sleep(3000);
获取guid
/**
* 获取guid
*
* @return
*/
public static String getGuid() {
return UUID.randomUUID().toString().replaceAll("\\-", "");
}
。。
- /**
- * 获取guid
- *
- * @return
- */
- public static String getGuid() {
- return UUID.randomUUID().toString().replaceAll("\\-", "");
- }
三、示例
- /**
- * 测试用户
- * @throws Exception
- */
- public void testUser() throws Exception {
- // 创建一个ObjectMapper对象
- ObjectMapper mapper = new ObjectMapper();
- // 创建一个Json对象
- ObjectNode json = mapper.createObjectNode();
-
- URL url = new URL("http://127.0.0.1/tourist-contract/register");
- for (int i = 0; i < 500; i++) {
- Random rand = new Random();
- int randomNum = rand.nextInt(9000) + 1000;
- json.put("Phone", "1308111"+randomNum);
- json.put("openID", "user_20000" + i);
- json.put("UserName", "test_" + i);
- json.put("unifyID", "test_" + i);
- json.put("userID", "test_" + i);
- json.put("reservedField", "reservedField");
-
- ObjectNode json1 = mapper.createObjectNode();
- json1.put("sender", json);
- json1.put("mark", "travel");
- log.info("注册用户上链参数json:"+json1);
-
- //休眠3秒
- Thread.sleep(3000);
- // 接口调用
- ltApi(json1, url, "1308111"+randomNum, "用户注册", "新增");
-
- }
- }
白盒测试
白盒测试是软件开发过程中的一种测试方法,它主要关注软件内部的结构和逻辑。这种测试方法可以帮助开发人员发现并纠正软件中的错误,提高软件的可靠性和稳定性。以下是一些常见的Java白盒测试方法:
总之,白盒测试是一种非常重要的软件开发过程,有助于确保代码的质量和可靠性。通过合理的白盒测试策略和方法,可以提高软件的可维护性、可扩展性和安全性等方面的质量水平。