使用JMeter测试Java类 [英] Testing Java Classes with JMeter

查看:260
本文介绍了使用JMeter测试Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一个Java服务调用. 我的第一次尝试是使用"Java请求采样器" 纪录片说

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

此采样器可让您控制Java 实现 JavaSamplerClient接口.

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

我不确定如何获取org.apache.jmeter.protocol.java.sampler.JavaSamplerClient接口,因为在JMeter端既没有Maven Artifact也没有提供二进制文件. 只是一个JMeter Maven插件(这不是我想要的). 我可以在本地Maven存储库中安装所需的二进制文件,只是不知道它们在哪里可用.

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.

想知道是否有人在使用"Java Request Sampler"吗?

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

P.S.也许我应该尝试 JUnit采样器

P.S. Maybe I should try the JUnit Sampler

推荐答案

我们正在使用ANT进行构建,并且正在使用JAR,这些JAR位于

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.

构建Java类后,我们还将生成的JAR文件也放置在文件夹\lib\ext\中. 如果我们的类使用任何依赖关系(第3部分JAR),则将其放在"\ lib"文件夹中. 之后,您可以启动JMeter,然后可以在Java Request Sampler中选择Java类.

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.

以下是此类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;
  }  
}

这篇关于使用JMeter测试Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆