0
votes

JMeter function ${__time()} embedded in HTTP Request body doesn't work when running .jmx through java. It works perfectly fine when running the test in JMeter GUI. Can someone please provide any pointers? I even tried to assign this function to a variable in JMeterGUI and then used that variable in json body but again it works in GUI but not when running .jmx file through Java, My API responds with 400 bad request means incorrect body.

HTTP Request Json Body in Jmeter GUI:

{"UniqID" : ${__time()}}

My Java code:

JMeterUtils.loadJMeterProperties("../bin/jmeter.properties");
JMeterUtils.setJMeterHome("../jMeter");
JMeterUtils.initLocale();
SaveService.loadProperties();
File jmxFile = new File(".../test.jmx");
HashTree testPlanTree = SaveService.loadTree(jmxFile);
StandardJMeterEngine jMeterEngine = new StandardJMeterEngine();
jMeterEngine.configure(testPlanTree);
jMeterEngine.
jMeterEngine.run();
jMeterEngine.exit();

Update:

I copied ApacheJMeter_functions.jar from gradle local folder ..\Users\.gradle\caches\modules-2\files-2.1\org.apache.jmeter\ApacheJMeter_functions\ to ../jMeter/lib/ext which is a sub-directory in jMeter home path set in my code above and it worked.

But now the question is how do I achieve this with just the gradle dependency and not by adding actual jar to jMeter home as I cant upload jar files to git.

1
@user7294900, for some reasons your comments are gone, but I have tried what you said using implementation in dependency file but it did not do anything.akash bondre

1 Answers

0
votes

I came across a path variable search_path in jmeter.properties file which needed to be set to the path of jmeter_function jar but then came a new question how do I get the path of gradle dependency download as java.class.path was giving me only thin jar path. So after spending many days in search for a solution, finally was able to create below solution:

task myTest(type: JavaExec) {
    ...someCode...

    doFirst {
        args = [sourceSets.test.runtimeClasspath.filter {File f ->
            f.name.matches("(.*)ApacheJMeter_functions(.*)")}.asPath]
    }
}

And then I used above argument value in my java code to set seach_path as below

JMeterUtils.loadJMeterProperties("../bin/jmeter.properties");
JMeterUtils.setJMeterHome("../jMeter");

String jMeterClassPath = args[0];
JMeterUtils.setProperty("search_paths",jMeterClassPath);

JMeterUtils.initLocale();
SaveService.loadProperties();
File jmxFile = new File(".../test.jmx");
HashTree testPlanTree = SaveService.loadTree(jmxFile);
StandardJMeterEngine jMeterEngine = new StandardJMeterEngine();
jMeterEngine.configure(testPlanTree);
jMeterEngine.
jMeterEngine.run();
jMeterEngine.exit();