JMeter - Groovy 变量的脚本连接

JMeter - Groovy script concatenation of variables

Groovy 是 JMeter 中的首选脚本

We advise using Apache Groovy or any language that supports the Compilable interface of JSR223.

JSR233 采样器中的以下代码在 Java 中有效,但在 Groovy

中无效
String a= "0"+"1" +
"2" 
+"3";
log.info(a);

我发现 + 运算符的 没有按预期工作,

但是我想将几个变量连接到一个脚本中的解决方案是什么?

我没有使用三引号的答案"""The row Id is: ${row.id}..."""

目前我使用 Java 作为脚本语言并使用 JMeter ${variable} 虽然也是 not recommended:

In this case, ensure the script does not use any variable using ${varName} as caching would take only first value of ${varName}

String text ="...<id>${id}</id><id2>${id2}</id2>...";

在这种情况下 groovy 有什么更好的方法?

编辑:

尝试使用 << 但在拆分到新行时会出现不同的错误

String text ="<id>" <<vars["id1"] << "<id><id2>" 
<< vars["id2"] << "<id2>";

收到错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script12.groovy: 2: unexpected token: << @ line 2, column 1.
   << vars["id2"] << "<id2>";

你为什么不使用:

String text ="<id>" <<vars["id1"] << "<id><id2>" << vars["id2"] << "<id2>";

对我有用

如果我有一个像你一样的 hashmap 来连接,我会尝试:

def vars = ["id": "value", "id2": "value2", "id3": "value3"]

String text = ""
vars.each { k, v ->
    text += "<${k}>${v}</${k}>"
}

println text

Groovy 使用换行符来指示语句结束,除非它知道下一行必须扩展当前行。支持下一行开头的许多二元运算符。 '+' 和 '-' 运算符有二元和一元变体,目前(Groovy 版本至少达到 2.5.x)在下一行的开头不支持这些运算符。您可以将运算符放在上一行的末尾(如第一行)或在上一行的末尾使用行继续符:

String a = "0" + "1" +
"2" \
+ "3"
log.info(a)