在Java中运行Jmeter测试时出现空指针异常

时间:2016-04-07 10:18:06

标签: java maven jmeter

以下是我在Java8中运行jmeter测试计划的简单代码。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="one">
            <h3>Placeholder 1</h3>
            <form>
                    <button id="next1" type="button" class="btn btn-lg btn-default">Next</button>
            </form>    

</div><!--Closes One-->

<div id="two">
            <h3>Placeholder 2</h3>
            <form>
                    <button id="next2" type="button" class="btn btn-lg btn-default">Next</button>
            </form> 
</div><!--Closes Two-->

<div id="three">
            <h3>Placeholder 3</h3>
            <form>
                    <button id="next3" type="button" class="btn btn-lg btn-default">Next</button>
            </form> 
</div><!--Closes Three-->

<div id="four">
            <h3>Placeholder 4</h3>
</div><!--Closes Four-->

但是我在运行时遇到以下错误。

import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;


public class JmxSuite {

    public static void main(String[] args){
        StandardJMeterEngine jmeter = new StandardJMeterEngine();
        JMeterUtils.setJMeterHome("C:\\PLACES\\apache-jmeter-2.11");



        HashTree testPlanTree = new HashTree();

        HTTPSampler httpSampler = new HTTPSampler();
        httpSampler.setDomain("exapmle.com");
        httpSampler.setPort(80);
        httpSampler.setPath("/");
        httpSampler.setMethod("GET");


        LoopController loopController = new LoopController();
        loopController.setLoops(1);
        loopController.addTestElement(httpSampler);
        loopController.setFirst(true);
        loopController.initialize();


        ThreadGroup threadGroup = new ThreadGroup();
        threadGroup.setNumThreads(1);
        threadGroup.setRampUp(1);
        threadGroup.setSamplerController(loopController);

        TestPlan testPlan = new TestPlan("RKSV Jmeter Testing");

        testPlanTree.add("testPlan", testPlan);
        testPlanTree.add("loopController", loopController);
        testPlanTree.add("threadGroup", threadGroup);
        testPlanTree.add("httpSampler", httpSampler);

        jmeter.configure(testPlanTree);
        jmeter.run();
    }
}

以下是我的pom.xml

INFO    2016-04-07 15:40:44.060 [jmeter.e] (): Listeners will be started after enabling running version
INFO    2016-04-07 15:40:44.079 [jmeter.e] (): To revert to the earlier behaviour, define jmeterengine.startlistenerslater=false
INFO    2016-04-07 15:40:44.108 [jmeter.p] (): No response parsers defined: text/html only will be scanned for embedded resources
INFO    2016-04-07 15:40:44.115 [jmeter.p] (): Maximum connection retries = 10
INFO    2016-04-07 15:40:44.121 [jmeter.e] (): Running the test!
INFO    2016-04-07 15:40:44.141 [jmeter.s] (): List of sample_variables: []
INFO    2016-04-07 15:40:44.141 [jmeter.s] (): List of sample_variables: []
Exception in thread "main" java.lang.NullPointerException
    at org.apache.jmeter.util.JMeterUtils.setProperty(JMeterUtils.java:885)
    at org.apache.jmeter.threads.JMeterContextService.startTest(JMeterContextService.java:92)
    at org.apache.jmeter.engine.StandardJMeterEngine.run(StandardJMeterEngine.java:313)
    at JmxSuite.main(JmxSuite.java:48)

可能的原因是什么? 我使用了来自5 ways to run jmeter

的第4个主题的代码

2 个答案:

答案 0 :(得分:3)

You're missing few lines from the article, double check your code, to wit:

JMeterUtils.loadJMeterProperties("C:\\PLACES\\apache-jmeter-2.11\\bin\\jmeter.properties");
JMeterUtils.initLogging();
JMeterUtils.initLocale();

Also there is an example project jmeter-from-code which can be used as a reference or skeleton.

Also consider using latest version of JMeter, for the moment it's Apache JMeter 2.13

答案 1 :(得分:2)

The error occurs because the internal Properties-object of JMeterUtils is null at this point. Add the following line to your code to initalize this object:

JMeterUtils.loadJMeterProperties("/path/to/your/jmeter/bin/jmeter.properties");

EDIT: Instead of doing it all manually you should call

JMeterUtils.initializeProperties("/path/to/your/jmeter/bin/jmeter.properties");

which is the correct way and does all the initalizing of logging and locale internally.