JEMETER非GUI:缺少类com.thoughtworks.xstream.converters.ConversionException错误

时间:2017-11-15 07:20:21

标签: jmeter jmeter-3.2

我正在从头开始创建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>

请帮忙。谢谢!

1 个答案:

答案 0 :(得分:0)

为了能够在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"));
}

参考文献: