Jmeter接口自动化-16-Jmeter中BeanShell中怎么创建List对象

一、Jmeter发起请求参数中key-value中value为数组的形式,我们该如何创建

首先看下面的Java编译器中的java代码

我们的请求参数形式:

{
    "terminal":"aaaaaa",
    "platform":"1",
    "channel":"xxxxxxxx",
    "loginId":"xxxxxxxxx",
    "deviceId":"xxxxxxxxxxxxxxxxx",
    "v":"v1.1.1.1",
    "businessGroup":[
        {
            "businessCode":"111",
            "storeGroup":[
                {
                    "storeId":11111,
                    "venderId":11111,
                    "skuList":[
                        111111111,
                        111111111
                    ]
                }
            ]
        }
    ]
}

 

idea编写入参代码:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test01 {

    public static void main(String[] args){

        Map myMap = new HashMap();
        Map subMap = new HashMap();
        Map subStore = new HashMap();
        List<Object> businessGroupRes = new ArrayList<Object>();
        List<Object> storeGroupRes = new ArrayList<Object>();
        List<String> sku=new ArrayList<String>();

        sku.add("${skuId}");

        subStore.put("storeId","${storeId}");
        subStore.put("venderId","${venderId}");
        subStore.put("skuList",sku);
        storeGroupRes.add(subStore);

        subMap.put("businessCode","1");
        subMap.put("storeGroup",storeGroupRes);

        businessGroupRes.add(subMap);


        myMap.put("terminal","devtools");
        myMap.put("channel","miniprograms");
        myMap.put("platform","9");
        myMap.put("loginId","${loginId}");
        myMap.put("deviceId","${deviceId}");
        myMap.put("v","${version}");
        myMap.put("businessGroup",businessGroupRes);
        System.out.println(myMap);
    }
}

打印:

{loginId=${loginId}, businessGroup=[{businessCode=111, storeGroup=[{skuList=[${skuId}], venderId=${venderId}, storeId=${storeId}}]}], v=${version}, channel=miniprograms, terminal=devtools, deviceId=${deviceId}, platform=11111}

二、我们将idea编写的代码移入jmeter的BeanShell中

运行发现,从这行开始报错:

List<Object> businessGroupRes = new ArrayList<Object>();
List<Object> storeGroupRes = new ArrayList<Object>();
List<String> sku=new ArrayList<String>();

分析得知:

Jmeter中的BeanShell不支持变量的引用

去掉<>

List businessGroupRes = new ArrayList();
List storeGroupRes = new ArrayList();
List sku=new ArrayList();

再次运行即可完美解决错误!!!

posted @ 2021-01-24 12:03  旅行没有终点  阅读(1088)  评论(0编辑  收藏  举报