添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
首发于 接口测试

Jmeter接口测试问题整理(一)

BeanShell调用第三方Jar包问题集合

问题一:第三方Jar包的方法调用失败

BeanShell脚本:

import dataProducer.mongodb.*;
import org.apache.commons.lang.StringUtils;
log.info("调用第三方Jar包,MongoDB创建colletion>>>");
String url="172.16.8.193:27017";
String database="dtstack";
log.info("close MongoDB connection>>>");
MongoDBDataProducer.close();//关闭MongoDB资源
MongoDBDataProducer.connectNoAuth(url,database);

错误日志:

2020-08-13 10:38:00,414 INFO o.a.j.t.JMeterThread: Thread started: 数栈-StreamApp接口 1-1
2020-08-13 10:38:00,416 INFO o.a.j.u.BeanShellTestElement: 调用第三方Jar包,MongoDB创建colletion>>>
2020-08-13 10:38:00,417 INFO o.a.j.u.BeanShellTestElement: close MongoDB connection>>>
2020-08-13 10:38:00,420 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval    Sourced file: inline evaluation of: ``import dataProducer.mongodb.*; import org.apache.commons.lang.StringUtils;  log. . . . '' : Method Invocation MongoDBDataProducer.connectNoAuth
2020-08-13 10:38:00,420 WARN o.a.j.p.j.s.BeanShellSampler: Exception executing script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval    Sourced file: inline evaluation of: ``import dataProducer.mongodb.*; import org.apache.commons.lang.StringUtils;  log. . . . '' : Method Invocation MongoDBDataProducer.connectNoAuth
2020-08-13 10:38:00,420 INFO o.a.j.s.FileServer: Stored: ./login.csv

排查问题步骤:

  1. 排除语法错误问题
  2. 日志中可以看出 connectNoAuth 方法调用失败,排查是否调到 connectNoAuth 这个方法:将 MongoDBDataProducer.connectNoAuth(url,database); 改成 MongoDBDataProducer.connectNoAuth(url,1,database); 再运行,报错如下:
2020-08-13 10:47:39,948 INFO o.a.j.t.JMeterThread: Thread started: 数栈-StreamApp接口 1-1
2020-08-13 10:47:39,949 INFO o.a.j.u.BeanShellTestElement: 调用第三方Jar包,MongoDB创建colletion>>>
2020-08-13 10:47:39,950 INFO o.a.j.u.BeanShellTestElement: close MongoDB connection>>>
2020-08-13 10:47:39,955 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import dataProducer.mongodb.*; import org.apache.commons.lang.StringUtils;  log. . . . '' : Error in method invocation: Static method connectNoAuth( java.lang.String, int, java.lang.String ) not found in class'dataProducer.mongodb.MongoDBDataProducer'
2020-08-13 10:47:39,955 WARN o.a.j.p.j.s.BeanShellSampler: Exception executing script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import dataProducer.mongodb.*; import org.apache.commons.lang.StringUtils;  log. . . . '' : Error in method invocation: Static method connectNoAuth( java.lang.String, int, java.lang.String ) not found in class'dataProducer.mongodb.MongoDBDataProducer'
2020-08-13 10:47:39,955 INFO o.a.j.s.FileServer: Stored: ./login.csv

可反向推断原先 MongoDBDataProducer.connectNoAuth(url,database); 是调到的 \3. 排查是否是 connectNoAuth 方法报错,只是jmeter未捕获到异常信息;使用try catch来捕获异常:

try {
    MongoDBDataProducer.connectNoAuth(url,database);
catch (Throwable ex) {
    log.error("Failed to do this or that", ex);
}

错误日志:

2020-08-13 10:51:08,385 INFO o.a.j.t.JMeterThread: Thread started: 数栈-StreamApp接口 1-1
2020-08-13 10:51:08,386 INFO o.a.j.u.BeanShellTestElement: 调用第三方Jar包,MongoDB创建colletion>>>
2020-08-13 10:51:08,387 INFO o.a.j.u.BeanShellTestElement: close MongoDB connection>>>
2020-08-13 10:51:08,390 ERROR o.a.j.u.BeanShellTestElement: Failed to do this or that
java.lang.NoSuchMethodError: com.mongodb.MongoClient.getDatabase(Ljava/lang/String;)Lcom/mongodb/client/MongoDatabase;
    at dataProducer.mongodb.MongoDBDataProducer.connect(MongoDBDataProducer.java:84) ~[dataPrepare-1.0-SNAPSHOT.jar:?]
    at dataProducer.mongodb.MongoDBDataProducer.connect(MongoDBDataProducer.java:97) ~[dataPrepare-1.0-SNAPSHOT.jar:?]
    at dataProducer.mongodb.MongoDBDataProducer.connectNoAuth(MongoDBDataProducer.java:106) ~[dataPrepare-1.0-SNAPSHOT.jar:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_201]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_201]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_201]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_201]
    at bsh.Reflect.invokeMethod(Reflect.java:131) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.Reflect.invokeStaticMethod(Reflect.java:95) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.Name.invokeMethod(Name.java:868) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:69) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:96) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:41) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.BSHBlock.evalBlock(BSHBlock.java:125) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.BSHBlock.eval(BSHBlock.java:75) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.BSHBlock.eval(BSHBlock.java:41) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.BSHTryStatement.eval(BSHTryStatement.java:80) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.Interpreter.eval(Interpreter.java:659) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.Interpreter.eval(Interpreter.java:750) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.Interpreter.eval(Interpreter.java:739) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_201]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_201]
    at org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:166) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.util.BeanShellInterpreter.eval(BeanShellInterpreter.java:189) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.util.BeanShellTestElement.processFileOrScript(BeanShellTestElement.java:151) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.protocol.java.sampler.BeanShellSampler.sample(BeanShellSampler.java:124) [ApacheJMeter_java.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) [ApacheJMeter_core.jar:4.0 r1823414]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_201]
2020-08-13 10:51:08,391 INFO o.a.j.s.FileServer: Stored: ./login.csv

问题二:出现 java.lang.NoSuchMethodError: com.mongodb.MongoClient.getDatabase(Ljava/lang/String;)Lcom/mongodb/client/MongoDatabase; 错误

BeanShell脚本:

import dataProducer.mongodb.*;
import org.apache.commons.lang.StringUtils;
log.info("调用第三方Jar包,MongoDB创建colletion>>>");
String url="172.16.8.193:27017";
String database="dtstack";
log.info("close MongoDB connection>>>");
MongoDBDataProducer.close();//关闭MongoDB资源
try {
    MongoDBDataProducer.connectNoAuth(url,database);
catch (Throwable ex) {
    log.error("Failed to do this or that", ex);
}

错误日志:

2020-08-13 10:51:08,385 INFO o.a.j.t.JMeterThread: Thread started: 数栈-StreamApp接口 1-1
2020-08-13 10:51:08,386 INFO o.a.j.u.BeanShellTestElement: 调用第三方Jar包,MongoDB创建colletion>>>
2020-08-13 10:51:08,387 INFO o.a.j.u.BeanShellTestElement: close MongoDB connection>>>
2020-08-13 10:51:08,390 ERROR o.a.j.u.BeanShellTestElement: Failed to do this or that
java.lang.NoSuchMethodError: com.mongodb.MongoClient.getDatabase(Ljava/lang/String;)Lcom/mongodb/client/MongoDatabase;
    at dataProducer.mongodb.MongoDBDataProducer.connect(MongoDBDataProducer.java:84) ~[dataPrepare-1.0-SNAPSHOT.jar:?]
    at dataProducer.mongodb.MongoDBDataProducer.connect(MongoDBDataProducer.java:97) ~[dataPrepare-1.0-SNAPSHOT.jar:?]
    at dataProducer.mongodb.MongoDBDataProducer.connectNoAuth(MongoDBDataProducer.java:106) ~[dataPrepare-1.0-SNAPSHOT.jar:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_201]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_201]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_201]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_201]
    at bsh.Reflect.invokeMethod(Reflect.java:131) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.Reflect.invokeStaticMethod(Reflect.java:95) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.Name.invokeMethod(Name.java:868) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:69) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:96) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:41) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.BSHBlock.evalBlock(BSHBlock.java:125) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.BSHBlock.eval(BSHBlock.java:75) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.BSHBlock.eval(BSHBlock.java:41) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.BSHTryStatement.eval(BSHTryStatement.java:80) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.Interpreter.eval(Interpreter.java:659) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.Interpreter.eval(Interpreter.java:750) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.Interpreter.eval(Interpreter.java:739) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_201]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_201]
    at org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:166) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.util.BeanShellInterpreter.eval(BeanShellInterpreter.java:189) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.util.BeanShellTestElement.processFileOrScript(BeanShellTestElement.java:151) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.protocol.java.sampler.BeanShellSampler.sample(BeanShellSampler.java:124) [ApacheJMeter_java.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) [ApacheJMeter_core.jar:4.0 r1823414]