0

嗨,我更新鲜Jmeter

我已经编写了一个 Java Sampler 代码。我不知道这是对还是错。如果我将该 URL 和参数放在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 并在那里显示所有参数,然后我运行这个测试计划,得到成功消息 nut no result

这是正确的方法还是我们必须为结果添加一些东西?

4

1 回答 1

1

我已经在这里回复了。

您需要try块中调用 result.setResponseData() ,否则您将看不到任何成功。根据您的代码,“查看结果树”侦听器的“响应数据”部分仅在错误时填充。

于 2013-11-11T18:13:42.117 回答