嗨Iam Fresher在Jmeter
我写了一个Java Sampler代码。我不知道这是对还是错。如果我在Http Request
中将该网址和参数设置为正确的结果,但如果写为javasampler
我没有得到该结果,我获得了Pass结果但没有响应和请求数据
我的采样器代码是:
package org.apache.jmeter.protocol.java.test;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
public class ExampleJavaSampler extends AbstractJavaSamplerClient implements Serializable {
String mySvc = "";
JavaSamplerContext context;
public Arguments getDefaultParameters(){
Arguments arg = new Arguments();
arg.addArgument("url", "http://www.url.com:5252/Switch/Download");
arg.addArgument("e_type", "bank");
arg.addArgument("e_id", "4");
arg.addArgument("b_id", "1");
arg.addArgument("a_id", "0002");
arg.addArgument("link_branch", "");
arg.addArgument("terminal_id", "");
arg.addArgument("version", "10");
arg.addArgument("entity", "100");
System.out.println("inside default");
return arg;
}
public void setupTest(JavaSamplerContext context) {
System.out.println("inside Setup");
}
public SampleResult runTest(JavaSamplerContext context) {
System.out.println("Inside Run test:");
String urls = context.getParameter("url");
String e_type = context.getParameter("e_type");
String e_id = context.getParameter("e_id");
String b_id = context.getParameter("b_id");
String a_id = context.getParameter("a_id");
String l_branch = context.getParameter("e_type");
String t_id = context.getParameter("e_type");
String oion = context.getParameter("e_type");
String entity = context.getParameter("e");
SampleResult result = new SampleResult();
result.getURL();
result.setSampleLabel("Test Result");
result.setDataType(SampleResult.TEXT);
result.sampleStart();
try{
java.net.URL url = new java.net.URL(urls+"?=e_type="+e_type+"&e_id="+e_id+"&b_id="+b_id);
System.out.println(url);
java.net.HttpURLConnection connection = (java.net.HttpURLConnection)url.openConnection(); // have to cast connection
connection.setRequestMethod("POST");
connection.connect();
result.sampleEnd(); // stop stopwatch
result.setSuccessful( true );
result.setResponseMessage( "Successfully performed action" );
result.setResponseCodeOK(); // 200 code
} catch (Exception e) {
result.sampleEnd(); // stop stopwatch
result.setSuccessful( false );
result.setResponseMessage( "Exception: " + e );
// get stack trace as a String to return as document data
java.io.StringWriter stringWriter = new java.io.StringWriter();
e.printStackTrace( new java.io.PrintWriter( stringWriter ) );
result.setResponseData( stringWriter.toString() );
result.setDataType( org.apache.jmeter.samplers.SampleResult.TEXT );
result.setResponseCode( "500" );
}
return result;
}
void teardownTest() {
System.out.println("inside tear Down:");
}
}
在此代码之后,我创建了.jar
文件并放置lib/ext
。然后我打电话给Javarequest并在那里显示所有参数,然后我运行这个测试计划,获得成功消息坚果没有结果
这是正确的方法还是我们必须为此添加一些东西?
答案 0 :(得分:1)
我已经回复了here。
您需要在尝试块中调用result.setResponseData() ,否则您将看不到任何成功案例。 “响应数据”片段的“查看结果树”监听器仅在出错时根据您的代码填充。