Record data test interface

I. Introduction

In Java development, it is often necessary to create data to test the interface. Here are the commonly used methods of testing the interface by creating data.

 

2. General interface parameter transfer methods

1. The interface method is best to use JSON or map. The advantage of this is that the parameters can be flexibly expanded, and the returned results are also best to be in JSON or map.

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

2. Assembly parameter method

	/**
	 * 组装获取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. Commonly generated data

Randomly generate 4 digits

    Random rand = new Random();
    int randomNum = rand.nextInt(9000) + 1000;

hibernate

//Sleep for 3 seconds
 Thread.sleep(3000);

Get guid

    /**
     * 获取guid
     * 
     * @return
     */
    public static String getGuid() {
        return UUID.randomUUID().toString().replaceAll("\\-", "");
    }
 

。。

	/**
	 * 获取guid
	 * 
	 * @return
	 */
	public static String getGuid() {
		return UUID.randomUUID().toString().replaceAll("\\-", "");
	}

3. Example

	/**
	 * 测试用户
	 * @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, "用户注册", "新增");

		}
	}

Guess you like

Origin blog.csdn.net/dongjing991/article/details/132813071