JMeter - Convert Beanshell to Groovy

21 views
Skip to first unread message

Ajith R

unread,
Jun 21, 2024, 3:51:00 AMJun 21
to JMeter Forum
Hi Team,

I have a BeanShell Sampler with the below script:

import TestApp.MessageRegister; 
import TestApp.Message;
import TestApp.MessagePropertyAnnotation; 
import TestApp.MessageTypeAnnotation;
import TestApp.RegisteredDeviceStatus;
import com.csa.asn1.MessageType; 
import com.csa.asn1.BinaryWriterASN1; 
import com.csa.asn1.BitConverter; 
import com.csa.asn1.Tag;
import org.apache.commons.codec.binary.Hex; 

MessageRegister registerMsg = new MessageRegister(); 
registerMsg.MacAddress = "60:5B:20:0E:FE:E5";
registerMsg.ManufactureDate = "2024-06-06T18:00:00.0000000Z"; registerMsg.SerialNumber = "${serialNumber}";
registerMsg.Model = "Model"; 
registerMsg.StagedVersion = "1"; 
registerMsg.RunningVersion = "1";
registerMsg.ManufactureVersion = "1"; 

byte[] bytes = registerMsg.ToBytes(); 

return (Hex.encodeHexString(bytes)); 

The above code needs to be converted into groovy.

Could you please help me with this? 

Thanks for your support. 

Regards, 

DT

unread,
Jun 21, 2024, 3:54:01 AMJun 21
to JMeter Forum
In the majority of cases valid Beanshell / Java code will be valid Groovy code so you can just copy and paste this into the JSR223 Sampler.

I would only change this line:

registerMsg.SerialNumber = "${serialNumber}";

to this one:

registerMsg.SerialNumber = vars.get("serialNumber");

as Groovy engine will compile and cache only first occurrence and if the value is dynamic the first one will be used in the subsequent iterations.

More information:

Reply all
Reply to author
Forward
0 new messages