JMETER 非 GUI:缺少 class com.thoughtworks.xstream.converters.ConversionException 错误

JEMETER Non-GUI : missing class com.thoughtworks.xstream.converters.ConversionException error

我正在从头开始创建 JMeter 测试计划。 这是我的代码:

public static void main(String[] args) throws Exception {

        StandardJMeterEngine jmeterEngine = new StandardJMeterEngine();
        JMeterUtils.setJMeterHome("/Users/myDir/Documents/JMeter/apache-jmeter-3.3");
        JMeterUtils.loadJMeterProperties("src/main/resources/jmeter.properties");
        JMeterUtils.initLocale();

        //create test plan
        TestPlan testPlan = new TestPlan();
        testPlan.setEnabled(true);
        testPlan.setName("Test Plan");

        //create thread group
        ThreadGroup threadGroup = new ThreadGroup();
        threadGroup.setNumThreads(1);
        threadGroup.setRampUp(1);

        // create http sampler
        HTTPSampler httpSampler = new HTTPSampler();
        httpSampler.setProtocol("https");
        httpSampler.setDomain("myDomain.services.company.com");
        httpSampler.setPath("ims/login/v1/token");
        httpSampler.setMethod("POST"); 
        httpSampler.setFollowRedirects(true);
        httpSampler.setAutoRedirects(false);
        httpSampler.setUseKeepAlive(true);
        httpSampler.addArgument("client_id", "argValue1");
        httpSampler.addArgument("scope", "argValue2");
        httpSampler.addArgument("userName", "abc%2B249%40gmail.com");
        httpSampler.addArgument("password", "Abc123"); 

        //Add sampler to the thread group
        threadGroup.addTestElement(httpSampler);

        HashTree testPlanHashTree = new HashTree();
        testPlanHashTree.add(testPlan);
        testPlanHashTree.add(threadGroup);

        // Generating the JMX file
        SaveService.saveTree(testPlanHashTree, new FileOutputStream(JMeterUtils.getJMeterHome() + "/bin/Test2.jmx"));
    }

当我执行此代码时,我的 Test2.jmx 已创建。

接下来,我尝试使用以下命令通过 Jmeter 的非 GUI 模式 运行 Test2.jmx:

./jmeter.sh -n -t Test2.jmx

我的测试计划 Jmeter 没有 运行 而是抛出以下错误:

Error in NonGUIDriver java.lang.IllegalArgumentException: Problem loading XML from:'/Users/chandrat/Documents/JMeter/apache-jmeter-3.3/bin/Test2.jmx', missing class com.thoughtworks.xstream.converters.ConversionException: 
---- Debugging information ----
cause-exception     : com.thoughtworks.xstream.converters.ConversionException
cause-message       : 
first-jmeter-class  : org.apache.jmeter.save.converters.HashTreeConverter.unmarshal(HashTreeConverter.java:67)
class               : org.apache.jmeter.save.ScriptWrapper
required-type       : org.apache.jmeter.threads.ThreadGroup
converter-type      : org.apache.jmeter.save.ScriptWrapperConverter
path                : /jmeterTestPlan/org.apache.jorphan.collections.HashTree/org.apache.jorphan.collections.HashTree/ThreadGroup
line number         : 6
version             : 3.3 r1808647
-------------------------------

1) 我不知道如何解决这个问题?原因消息是空白的?我应该怎么做才能执行我的测试计划而不会出现上述错误?

2) 能不能也看看我的测试计划生成代码?那个代码对吗? testGroup是直接添加到树中还是先添加到测试计划中再将测试计划添加到树中?

这是我的 pom 文件中的依赖项,以防我缺少 jar。

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.jmeter</groupId>
        <artifactId>ApacheJMeter_core</artifactId>
        <version>3.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.jmeter</groupId>
        <artifactId>jorphan</artifactId>
        <version>3.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.jmeter</groupId>
        <artifactId>ApacheJMeter_http</artifactId>
        <version>3.3</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.7</version>
    </dependency>
    <dependency>
        <groupId>oro</groupId>
        <artifactId>oro</artifactId>
        <version>2.0.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.avalon.framework</groupId>
        <artifactId>avalon-framework-api</artifactId>
        <version>4.3.1</version>
    </dependency>
  </dependencies>

请帮忙。谢谢!

为了能够在 JMeter GUI 中打开生成的测试计划,您需要定义更多的属性,将您的代码修改为:

public static void main(String[] args) throws Exception {

    StandardJMeterEngine jmeterEngine = new StandardJMeterEngine();
    JMeterUtils.setJMeterHome("/Users/myDir/Documents/JMeter/apache-jmeter-3.3");
    JMeterUtils.loadJMeterProperties("src/main/resources/jmeter.properties");
    JMeterUtils.initLocale();

    //create test plan
    TestPlan testPlan = new TestPlan();
    testPlan.setEnabled(true);
    testPlan.setName("Test Plan");
    testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
    testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
    testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

    LoopController loopController = new LoopController();
    loopController.setLoops(1);
    loopController.setFirst(true);
    loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
    loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
    loopController.initialize();

    //create thread group
    ThreadGroup threadGroup = new ThreadGroup();
    threadGroup.setNumThreads(1);
    threadGroup.setRampUp(1);
    threadGroup.setSamplerController(loopController);
    threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
    threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());

    // create http sampler
    HTTPSampler httpSampler = new HTTPSampler();
    httpSampler.setProtocol("https");
    httpSampler.setDomain("myDomain.services.company.com");
    httpSampler.setPath("ims/login/v1/token");
    httpSampler.setMethod("POST");
    httpSampler.setFollowRedirects(true);
    httpSampler.setAutoRedirects(false);
    httpSampler.setUseKeepAlive(true);
    httpSampler.addArgument("client_id", "argValue1");
    httpSampler.addArgument("scope", "argValue2");
    httpSampler.addArgument("userName", "abc%2B249%40gmail.com");
    httpSampler.addArgument("password", "Abc123");
    httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSampler.class.getName());
    httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

    //Add sampler to the thread group

    HashTree testPlanHashTree = new HashTree();
    HashTree threadGroupHashTree = testPlanHashTree.add(testPlan, threadGroup);
    threadGroupHashTree.add(httpSampler);

    // Generating the JMX file
    SaveService.saveTree(testPlanHashTree, new FileOutputStream(JMeterUtils.getJMeterHome() + "/bin/Test2.jmx"));
}

参考文献: