Правильный код для HeaderManager инициализации и добавления соответствующего заголовка Content-Type будет выглядеть примерно так:
HeaderManager headerManager = new HeaderManager();
headerManager.setName("HTTP Header Manager");
headerManager.add(new Header("Content-Type", "application/json"));
headerManager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
headerManager.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());
На всякий случай полный код для создания план тестирования JMeter с одним сэмплером HTTP Request и диспетчером заголовков HTTP, добавленным в качестве дочернего элемента этого сэмплера:
package com.example;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.protocol.http.control.Header;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.gui.HeaderPanel;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
public class JMeterFromCode {
public static void main(String[] args) throws Exception {
String slash = System.getProperty("file.separator");
File jmeterHome = new File("c:/apps/jmeter");
File jmeterProperties = new File(jmeterHome.getPath() + slash + "bin" + slash + "jmeter.properties");
//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.setJMeterHome(jmeterHome.getPath());
JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
// JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = new HashTree();
// HTTP Request Sampler
HTTPSamplerProxy httpRequest = new HTTPSamplerProxy();
httpRequest.setDomain("example.com");
httpRequest.setPort(80);
httpRequest.setPath("/");
httpRequest.setMethod("GET");
httpRequest.setName("HTTP Request");
httpRequest.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpRequest.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
// HTTP Header Manager
HeaderManager headerManager = new HeaderManager();
headerManager.setName("HTTP Header Manager");
headerManager.add(new Header("Content-Type", "application/json"));
headerManager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
headerManager.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());
// Loop Controller
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();
// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Example Thread Group");
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());
// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
HashTree httpSamplerHashTree = new HashTree();
httpSamplerHashTree.add(httpRequest, headerManager);
threadGroupHashTree.add(httpSamplerHashTree);
// save generated test plan to JMeter's .jmx file format
SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + slash + "example.jmx"));
}
}
Дополнительная информация: Пять способов запустить тест JMeter без использования JMeter GUI