接口测试设置参数的方式-接口的参数是一个json长串

本文介绍如何在接口测试中处理JSON格式的参数,通过示例展示了如何构造复杂的JSON对象,并提供了数据源的编写方法。同时,讨论了不同情况下的参数有效性验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.接口的参数是一个json长串

public static JSONObject getRoomPostData()
			throws UnsupportedEncodingException {
		JSONObject json = new JSONObject();
		json.put("title", "标题");
		json.put("digest", "摘要");
		json.put("analystName", "分析师名称");
		json.put("url", "标题");
		json.put("sourceId", "14345");
		json.put("roomId", "4");
		json.put("operationType", "1");
		json.put("type", "1");
		json.put("sourceCreateTime", DateUtil.formatDate(new Date(),
				DateUtil.FMT_DATE_YYYYMMDDHHMMSS));
		json.put("replies", "0");
		json.put("analystImage", "0");
		json.put("orderWeight", "0");

		JSONObject tmp = new JSONObject();
		tmp.put("originWidth", "200");
		tmp.put("originHeight", "200");
		tmp.put("originUrl",
				"http://pimg1.126.net/silver/img/common/logo.png?1444875175");
		JSONArray array = new JSONArray();
		array.add(tmp);
		json.put("imageList", array);
		System.out.println(json.toString());
		return json;
	}



数据源的写法:
@DataProvider(name = "data")
 public static Object[][] dataObjects() throws UnsupportedEncodingException {
  String jsonString = TradeUtils.getRoomPostData().toString();
  String[] case1 = { jsonString, "200" };
  String[] case2 = { "{}", "401" };
  String[] case3 = { "{\"title\":\"标题\"}", "401" };
  String[] case4 = { "d", "401" };
  Object[][] obj = { case1, case2, case3, case4  };



数据组织方法:
        public static JSONObject getRoomPostData()
        throws UnsupportedEncodingException {
        JSONObject json = new JSONObject();
        json.put("title", "标题");
        json.put("digest", "摘要");
        json.put("analystName", "分析师名称");
        json.put("url", "标题");
        json.put("sourceId", "14345");
        json.put("roomId", "4");
        json.put("operationType", "1");
        json.put("type", "1");
        json.put("sourceCreateTime", DateUtil.formatDate(new Date(),
        DateUtil.FMT_DATE_YYYYMMDDHHMMSS));
        json.put("replies", "0");
        json.put("analystImage", "0");
        json.put("orderWeight", "0");

        JSONObject tmp = new JSONObject();
        tmp.put("originWidth", "200");
        tmp.put("originHeight", "200");
        tmp.put("originUrl","http://pimg1.126.net/silver/img/common/logo.png?1444875175");
        JSONArray array = new JSONArray();
        array.add(tmp);
        json.put("imageList", array);
        System.out.println(json.toString());
        return json;
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值