-1

Hey All I have a question about running Jmeter as step in Junit, Is it possible that in Junit to call to JMX file and run it in Jmeter? and all will be done from eclipse?

for example: step 1 validate data in DB step 2 get properties step 3 fire data via Jmeter step 4 verify data in Junit

Bastian
  • 1,089
  • 7
  • 25
  • 74

2 Answers2

1

It should be possible using JMeter API.

  1. Make sure you have all .jars from "lib" and "lib/ext" folders of your JMeter installation in your JUnit project classpath
  2. Use StandardJMeterEngine class to run your test like:

    import org.apache.jmeter.engine.StandardJMeterEngine;
    import org.apache.jmeter.reporters.ResultCollector;
    import org.apache.jmeter.reporters.Summariser;
    import org.apache.jmeter.save.SaveService;
    import org.apache.jmeter.util.JMeterUtils;
    import org.apache.jorphan.collections.HashTree;
    import org.junit.Test;
    
    import java.io.File;
    
    public class YourJUnitTestClass {
    
        @Test
        public void runJMeter() throws Throwable {
            StandardJMeterEngine jmeter = new StandardJMeterEngine();
            JMeterUtils.loadJMeterProperties("/path/to/your/jmeter/bin/jmeter.properties");
            JMeterUtils.setJMeterHome("/path/to/your/jmeter");
            JMeterUtils.initLocale();
            SaveService.loadProperties();
            HashTree testPlanTree = SaveService.loadTree(new File("/path/to/your/jmeter/Test.jmx"));
            Summariser summer = null;
            String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
            if (summariserName.length() > 0) {
                summer = new Summariser(summariserName);
            }
            String logFile = "/path/to/result.jtl";
            ResultCollector logger = new ResultCollector(summer);
            logger.setFilename(logFile);
            testPlanTree.add(testPlanTree.getArray()[0], logger);
            jmeter.configure(testPlanTree);
            jmeter.run();
        }
    }
    

More information: Five Ways To Launch a JMeter Test without Using the JMeter GUI

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

You are trying execute JMeter inside JUnit test(s) which is a wrong approach,

Because JMeter is mainly a performance tool

Apache JMeter may be used to test performance both on static and dynamic resources, Web dynamic applications. It can be used to simulate a heavy load on a server, group of servers, network or object to test its strength or to analyze overall performance under different load types.

And junit is not suitable for performance test

JUnit is designed for unit testing. There are lot of things that need to be taken care while writing performance tests in Java

What you can do If you want unit testing using JMeter is to add unit testing inside/using JMeter by JUnit Sampler

Classname the name of the class to test. The sampler will scan the jar files in jmeter/lib/ext and jmeter/lib/junit for classes extending JUnit's TestCase.

It's also more than ok to have several tools to execute different types of testing

Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233