使用JMeter测试JAVA接口,需要编写一个java类,继承AbstractJavaSampleClient,实现具体接口的调用,然后将此工程打包为一个jar包,防止在JMETER_HOME\lib\ext目录下,然后在JMeter里配置调用。
- 编写Java类,继承AbstractJavaSampleClient
- 打包 jar
- 将jar包放置至 JMETER_HOME\lib\ext 目录
- JMeter里配置调用
1. 添加依赖
<dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_java</artifactId> <version>2.7</version> </dependency>
2. 编写Java类
public class AdSearchServiceTest extends AbstractJavaSamplerClient { private static AdSearchService adSearchService; static { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); adSearchService = applicationContext.getBean("adSearchService", AdSearchService.class); } @Override public SampleResult runTest(JavaSamplerContext javaSamplerContext) { SampleResult sampleResult = new SampleResult(); String keyword = javaSamplerContext.getParameter("keyword"); AdSearchService.Param param = new AdSearchService.Param(); param.setKeyword(keyword); param.setStart(1); param.setLimit(10); SearchApiResult kievResult = adSearchService.search(param); sampleResult.setResponseData(kievResult.getJson(), "utf-8"); return sampleResult; } public static void main(String[] args) { AdSearchServiceTest kiev = new AdSearchServiceTest(); Arguments arguments = new Arguments(); arguments.addArgument("keyword", "微信"); JavaSamplerContext context = new JavaSamplerContext(arguments); SampleResult result = kiev.runTest(context); System.out.println(new String(result.getResponseData())); } }
3. 打包,打包时需将相关依赖的包一同打包,否则会出现类找不到错误。
4. 将jar包放置在JMeter_home\lib\ext 目录下
5. 配置JMeter
原文地址:https://www.cnblogs.com/buxiangbuliang/p/9397846.html
时间: 2024-12-21 03:10:35