如何在 jmeter 中连接字符串和整数值

How to concatenate string and integer values in jmeter

我正尝试在 JMeter 中连接字符串和整数值,如下所示

for(int i in 1..100){
    def cloudItemId="cloudItemId"+i;
    def deviceItemId="deviceItemId"+i;
}

但我收到以下错误

Response message:Exception: groovy.lang.MissingMethodException: No signature of method:
java.lang.String.positive() is applicable for argument types: () values: []
Possible solutions: notify(), size(), tokenize()

Iconcatenate 如何与 cloudItemId/deviceItemId

问题是+符号用于整数中的数学加号和字符串中的连接

您可以使用 <<:

连接
for(int i in 1..100){    
    def cloudItemId="cloudItemId" <<i;
    log.info(cloudItemId.toString());
    def deviceItemId="deviceItemId"<<i;
    log.info(cloudItemId.toString());
}

或调用 String.valueOf 来连接字符串:

for(int i in 1..100){
    def cloudItemId="cloudItemId" + String.valueOf(i);
    log.info(cloudItemId);
    def deviceItemId="deviceItemId"+ String.valueOf(i)
    log.info(cloudItemId);
}

我无法使用最新的 JMeter 5.4.1 重现您的问题(您 supposed to be using the latest JMeter version 以防万一):

所以请注意 jmeter.log file 中的语法和可疑条目。

您还可以稍微重构您的代码以使用 GStrings and make it more Groovy:

1.upto(100, { i ->
    def cloudItemId = "cloudItemId$i"
    def deviceItemId = "deviceItemId$i"
    log.info('cloudItemId: ' + cloudItemId)
})