JMeter 测试 JAVA 接口
Posted Simple, not simple
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JMeter 测试 JAVA 接口相关的知识,希望对你有一定的参考价值。
使用JMeter测试JAVA接口,需要编写一个java类,继承AbstractJavaSampleClient,实现具体接口的调用,然后将此工程打包为一个jar包,防止在JMETER_HOMElibext目录下,然后在JMeter里配置调用。
- 编写Java类,继承AbstractJavaSampleClient
- 打包 jar
- 将jar包放置至 JMETER_HOMElibext 目录
- 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_homelibext 目录下
5. 配置JMeter
以上是关于JMeter 测试 JAVA 接口的主要内容,如果未能解决你的问题,请参考以下文章