Testing Java Classes with JMeter

Martin Dürrmeier picture Martin Dürrmeier · Mar 4, 2010 · Viewed 29.4k times · Source

I'd like to test a Java Service Call. My first attempt was to use the "Java Request Sampler" The docu says

This sampler lets you control a java class that implements the JavaSamplerClient interface.

I'm not sure how to get the org.apache.jmeter.protocol.java.sampler.JavaSamplerClient Interface as there is no Maven Artifact nor provided binaries on the JMeter side. Just a JMeter Maven Plugin (which is not what I'm looking for). I could install the needed binaries in my local Maven Repository, I Just don't know where they are available.

Wondering if anybody at all is using the "Java Request Sampler" ?

P.S. Maybe I should try the JUnit Sampler

Answer

Steve picture Steve · Mar 4, 2010

We are building with ANT and are using the JARs, which are located in the binary file from JMeter under \lib\ext\. The AbstractJavaSamplerClient which is used for the Java Request Sampler is located in the file \lib\ext\ApacheJMeter_java.jar. For working with this abstract class, you also have to import the JAR file \lib\ext\ApacheJMeter_core.jar, which is (for example) holding the class SampleResult.

After building our Java class we put the resulting JAR file also in the folder \lib\ext\. If our class uses any dependency (3rd part JAR), we put it in '\lib' folder. After that, you can start JMeter and you're able to select your Java class in a Java Request Sampler.

Here is an example of such a Java Request Sampler:

public class JavaRequestSamplerDemo extends AbstractJavaSamplerClient {

  @Override
  public SampleResult runTest(JavaSamplerContext ctx) {
    JMeterVariables vars = JMeterContextService.getContext().getVariables();
    vars.put("demo", "demoVariableContent");

    SampleResult sampleResult = new SampleResult();
    sampleResult.setSuccessful(true);
    sampleResult.setResponseCodeOK();
    sampleResult.setResponseMessageOK();
    return sampleResult;
  }  
}