jmter JUnit Request的入门使用:
配置好java的环境后,打开eclipse,新建java project,引入相关jar,编写代码调试通过后,导出jar,放到jmeter目录下的lib路径后重启jmeter;
package com.test.junit;
import static junit.framework.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class JmeterJunit1 {
public static String response_data = "{data:{pageNo:[1,2],list:[{id:2,name:\"admin\"},{id:3,name:\"person\"}]}}";
public static List list = new ArrayList();
@Before
public void setUp() throws Exception {
JSONObject data_obj = JSON.parseObject(response_data);
String json = data_obj.get("data").toString();
JSONObject jso = JSON.parseObject(json);// json字符串转换成jsonobject对象
JSONArray jsarr = jso.getJSONArray("list");// jsonobject对象取得list对应的jsonarray数组
for (int i = 0; i < jsarr.size(); i++) {
JSONObject ao = jsarr.getJSONObject(i);// jsonarray对象通过getjsonobjext(index)方法取得数组里面的jsonobject对象
String vString = ao.getString("name");// jsonobject对象通过key直接取得String的值
if (vString == null) {
vString = ao.getString("username");
}
list.add(vString);
}
System.out.println("list---" + list + "\n");
}
@After
public void tearDown() throws Exception {
}
public void check(String name) {
boolean actualResult = list.contains(name);
assertTrue("the expected status is " + name + ", but now it's not", actualResult);
}
@Test
public void test1() throws Exception {
check("admin");
}
@Test
public void test2() throws Exception {
check("person");
}
@Test
public void test3() throws Exception {
check("admin22");
}
}
jmeter 新建junit request
填写项目包的名字,即可选择类,类中声明多少测试方法即可选择多个方法进行测试