3
votes

I have built a Java Class and exported it as JAR. I have imported it successfully in JMeter ($JMETER_HOME/lib) and I can successfully import it in a Beanshell Sampler and also in a JSR233 Sampler.

I have followed several instructions like Running Java class with JMeter (Bean Shell) but I am still getting the errors:

"Command not found: MyItem( java.lang.String, java.lang.String)" when trying to use the Class Constructor.

"Attempt to resolve method: getDescription() on undefined variable..." when using any method.

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;
    }
}

And this is my BeanShell / JSR233 Sampler:

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
}
1

1 Answers

1
votes

Your class is not public, so it will not be visible "to the world". Change it to

public class MyJavaItem{