我已经构建了一个 Java 类并将其导出为 JAR。我已经在 JMeter ($JMETER_HOME/lib) 中成功导入了它,我可以在 Beanshell Sampler 和 JSR233 Sampler 中成功导入它。
我已经遵循了几个指令,比如用 JMeter (Bean Shell) 运行 Java 类,但我仍然收到错误:
尝试使用类构造函数时出现“找不到命令:MyItem(java.lang.String, java.lang.String)”。
“尝试解析方法:未定义变量上的 getDescription()...”使用任何方法时。
class MyJavaItem{
//XML
String title;
String link;
String description;
String pubDate;
String guid;
String dcDate;
//JSON
int code;
String message;
String reference;
String documentId;
String documentType;
public MyJavaItem(String title, String description) {
this.title = title;
this.description = description;
//
JSONObject jsonObject = new JSONObject(description);
try {
message = jsonObject.getString("message");
} catch (JSONException e){
}
try {
code = jsonObject.getInt("code");
} catch (JSONException e){
}
try {
reference = jsonObject.getString("reference");
} catch (JSONException e){
}
try {
documentType = jsonObject.getString("documentType");
} catch (JSONException e){
}
try {
documentId = jsonObject.getString("documentId");
} catch (JSONException e){
}
}
public boolean containsReference(String ref){
return
StringUtils.containsIgnoreCase(message,ref) ||
StringUtils.containsIgnoreCase(documentId,ref) ||
StringUtils.containsIgnoreCase(reference,ref);
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public String getDcDate() {
return dcDate;
}
public void setDcDate(String dcDate) {
this.dcDate = dcDate;
}
public String getDocumentId() {
return documentId;
}
public void setDocumentId(String documentId) {
this.documentId = documentId;
}
public String getDocumentType() {
return documentType;
}
public void setDocumentType(String documentType) {
this.documentType = documentType;
}
public static MyJavaItem findByNumAtCard(ArrayList<MyJavaItem> activeMQArray, String numAtCard) {
for (MyJavaItem item : activeMQArray){
if (item.containsReference(numAtCard)){
return item;
}
}
return null;
}
}
这是我的 BeanShell / JSR233 采样器:
import MyJavaItem;
import ParseXML;
import java.util.List;
String staging = ParseXML.staging;
String returns = ParseXML.magentoReturnsResponses;
ArrayList items = ParseXML.parseXML(staging,returns);
log.info("" + bsh.shared.items.size());
for (Object item : items){
MyJavaItem item = items.get(i); // VALID
MyJavaItem myItem = (MyJavaItem) item; //VALID
MyJavaItem aaaa = MyJavaItem("title","description"); //INVALID
aaa.getDescription(); //INVALID
}