How to concatenate string and integer values in jmeter

Viewed 143

I'm trying to concatenate string and integer values in JMeter as below

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

but I'm getting this below error

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()

How can Iconcatenate with cloudItemId/deviceItemId

2 Answers

I cannot reproduce your issue using latest JMeter 5.4.1 (you're supposed to be using the latest JMeter version just in case):

enter image description here

so pay attention to your syntax and suspicious entries in jmeter.log file.

You can also slightly refactor your code to use GStrings and make it more Groovy:

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

enter image description here

The issue is + sign is used for mathematic plus in integers and concatenation in Strings

You can concatenate using <<:

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

Or call String.valueOf to concat Strings:

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