問(wèn)題
在jmeter中使用beanshell腳本時(shí)耙考,使用json-path-xxxxx.jar解析json數(shù)據(jù)時(shí)會(huì)報(bào)錯(cuò):Error in method invocation: Static method read( java.lang.String, java.lang.String ) not found in class'com.jayway.jsonpath.JsonPath
,以下講一下解決這個(gè)問(wèn)題的方法
問(wèn)題重現(xiàn)
1堆缘、在測(cè)試計(jì)劃中導(dǎo)入pathpath的jar包(我使用的是jmeter中l(wèi)ib目錄下的json-path-2.4.0.jar
)
2她倘、對(duì)接口(要求接口的返回值是json
)擂找,接口的返回值為
{
"msg": "Success",
"code": "0000",
"data": {
"versionView": {
"updateNotice": "很遺憾此版本已停用,請(qǐng)更新嗦玖!",
"downloadUrl": "xxxxxxxxxxxxxxxxxxxxx",
"sign": "",
"updateTime": 1558584325000,
"clientVersion": "4.1.5",
"versionCode": 415,
"platform": "iphone",
"clientType": "ios",
"createTime": 1553753740000,
"rate": null,
"name": "testxxxxxxx",
"needForceUpdate": true,
"id": 3538,
"status": "PUBLISHED"
}
},
"requestId": "2937a3ab656c426b9b4e4b29dfeaa07a"
}
添加BeanShell斷言痒留,腳本獲取需要獲取字段needForceUpdate
芹敌,斷言值是否為false
。腳本如下
import com.jayway.jsonpath.JsonPath;
String s = prev.getResponseDataAsString();
String res = JsonPath.read(s,"$..needForceUpdate").toString();
if (res!="false"){
Failure = true;
FailureMessage = "返回結(jié)果為不需要強(qiáng)制更新";
}
運(yùn)行結(jié)果報(bào)錯(cuò)肩榕,報(bào)錯(cuò)內(nèi)容如下:
2019-07-12 16:58:37,511 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: `` import com.jayway.jsonpath.JsonPath; String s = prev.getResponseDataAsString() . . . '' : Typed variable declaration : Error in method invocation: Static method read( java.lang.String, java.lang.String ) not found in class'com.jayway.jsonpath.JsonPath'
2019-07-12 16:58:37,511 WARN o.a.j.a.BeanShellAssertion: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: `` import com.jayway.jsonpath.JsonPath; String s = prev.getResponseDataAsString() . . . '' : Typed variable declaration : Error in method invocation: Static method read( java.lang.String, java.lang.String ) not found in class'com.jayway.jsonpath.JsonPath'
解決方法
以上報(bào)錯(cuò)處的代碼在idea上運(yùn)行是沒(méi)有問(wèn)題的刚陡,也不知道問(wèn)題出在了哪些。
上面報(bào)錯(cuò)大概的意思就是找不到com.jayway.jsonpath.JsonPath
類(lèi)里的靜態(tài)方法read
株汉。
于是想到了一種方法:對(duì)read方法再封裝一次筐乳,使用封裝后的函數(shù)
。
1乔妈、編寫(xiě)代碼蝙云,代碼思路,類(lèi)JsonUtil中的方法ParaseJson封裝read方法褒翰,第一個(gè)參數(shù)是json字符串贮懈,第二個(gè)參數(shù)是jsonpath匀泊。代碼如下:
import com.jayway.jsonpath.JsonPath;
public class JsonUtil {
public String ParaseJson(String str,String paras){
String res = JsonPath.read(str,paras).toString();
return res;
}
}
2优训、將代碼導(dǎo)出為jar包(導(dǎo)出方法自行百度
)。這里直接提供我導(dǎo)出的jar包jmeterUtil.jar各聘,提取碼:o0fr
3揣非、將jar包放入路徑\apache-jmeter-5.0\lib\ext
,導(dǎo)入jmeter躲因。
4早敬、編寫(xiě)代碼如下:
import jmeterUtil.*;
String s = prev.getResponseDataAsString();
JsonUtil t = new JsonUtil();
String data = t.ParaseJson(s,"$..needForceUpdate");
if (res!="true"){
Failure = true;
FailureMessage = "返回結(jié)果為不需要強(qiáng)制更新";
}
5、查看結(jié)果大脉,發(fā)現(xiàn)沒(méi)有報(bào)錯(cuò)搞监,并且成功輸出斷言。成功A蟆K雎俊!!
寫(xiě)在最后
以上不是最好的解決方法宙刘,但畢竟也算的上是一種方法 = = ,...........