记录造数据测试接口

一、前言

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

 

二、一般的接口传参方式

1、接口的方式最好是使用JSON或者map的方式,这样的好处是传参可以灵活伸缩,返回的结果也最好是JSON或者map的方式

    /**
     * 获取授权访问令牌accessToken
     * @param params
     * @return
     */
    @PostMapping("token")
    JSONObject getAccessToken(@RequestBody Map<String, Object> params);

2、组装参数方法

	/**
	 * 组装获取Access Token参数
	 */
	public Map<String, Object> getACTokenParam() {
		Map<String, Object> 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, "用户注册", "新增");

		}
	}

猜你喜欢

转载自blog.csdn.net/dongjing991/article/details/132813071
今日推荐