define a list in BeanShell (jmeter)

Leo picture Leo · Mar 16, 2015 · Viewed 8.4k times · Source

Could anyone help with BeanShell script? So, I'm trying to use a List in my code sample, however, I couldn't define a list properly. The code like this from BeanShell PostProcessor sampler:

import java.io.*;
import java.util.*;
import org.json.*;
import org.apache.jmeter.samplers.SampleResult;


    if ((prev.getResponseCode() != null) && (prev.getResponseCode().equals("200") == true)) {
        JSONObject response = new JSONObject(prev.getResponseDataAsString());
        JSONArray array = response.getJSONArray("users");
        List<String> users_list = new ArrayList<String>();
        for(int i = 0; i < array.length(); i++)  {
            JSONObject object = array.getJSONObject(i);
            users_list.add(object.getString("user_id"));
            print(users_list);
      }
    }

jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval   In file: inline evaluation of: ``import java.io.*; import java.util.*; import org.json.*; import org.apache.jmete . . . '' Encountered "=" at line 10, column 31.

Answer

Shai Alon picture Shai Alon · Feb 21, 2016

This is the simple way I got to create a list in Jmeter:

Example: creating a list in the size of the number of threads (var parameter):

import java.util.List;
import java.util.ArrayList;

list = new ArrayList();

for(int i = 0; i < ${NumThreads}; i++)  {
    list.add(i);
}