简体   繁体   中英

How to correct use hashmap and properties in JMeter

I faced the next problem. I have a.txt file, which contains data in the format:

"name"="value"

True data as example:

counter=10
scheduleNewPeriodEnd=2100-12-31
scheduleNewPeriodStart=2100-01-01

And i took this code as example and trying to convert it to hashmap with JSR223 Sampler or Beanshell Sampler. Then i want to put some value from map to properties for use it in next thread with requests:

    String filePath = "/soap/otherData.txt";
    String counter = ""; 
    String scheduleNewPeriodEnd = "";
    String scheduleNewPeriodStart = "";
    String arSchruleTypeId = "";
    String sarSchruleTypeId = "";

    HashMap map = new HashMap();
   
    String line;
    BufferedReader reader = new BufferedReader(new FileReader(filePath));
    while ((line = reader.readLine()) != null)
    {
        String[] parts = line.split("=", 2);
        if (parts.length >= 2)
        {
            String key = parts[0];
            String value = parts[1];
            map.put(key, value);
        } else {
            System.out.println("ignoring line: " + line);
        }
    }
   
    counter = map.get("counter");
    scheduleNewPeriodStart = map.get("scheduleNewPeriodStart");
    scheduleNewPeriodEnd = map.get("scheduleNewPeriodEnd");
    arSchruleTypeId = map.get("arSchruleTypeId");
    sarSchruleTypeId = map.get("sarSchruleTypeId");

    props.put(schPerStart,  scheduleNewPeriodStart);
    
    reader.close();

But it doesn't work. When i want to see what is props contains - the actual result this:

log.info("Property schPerStart is:  " + props.get("schPerStart"));

INFO o.a.j.u.BeanShellTestElement: Property schPerStart is:  ${schStart}

Expected result should be:

INFO o.a.j.u.BeanShellTestElement: Property schPerStart is: 2100-01-01

So i haven't any variable with name ${schStart} and don't understand why JMeter put it into props. The java code was tested in Idea and saving values to the map works correctly. Is it i doing something wrong?

Question resolved. Problem was that i wrapped code into class and he didn't call there. I just deleted class and method wrappers and it works fine now.

The problem is in the only line authored by you, just surround your schPerStart with quotation marks and the "code" should start working as you expect.

props.put("schPerStart",  scheduleNewPeriodStart);

Also be aware that starting from JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting as Groovy performance is much better comparing to Beanshell, see Apache Groovy - Why and How You Should Use It article for more details.

You won't have to change a single line, just copy and paste the whole snippet into the relevantJSR223 Test Element and enjoy (unless you would like to make your code more " groovy ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM