首页 > 代码库 > HTTP测试桩

HTTP测试桩

我们在对某个子系统进行性能测试的时候,可能会依赖外部系统,有的时候依赖的外部系统可能在现有的环境中没有,这个时候模拟桩就派上用场了。

准备工作:

1、  Tomcat

技术分享

2、  json相关jar包: http://download.csdn.net/detail/zhangy0329/607838

技术分享

3、  java完整代码,创建的工程没有包名

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

public class testEE extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
if (null == req) {
return;
}

resp.setCharacterEncoding("utf-8");
req.setCharacterEncoding("utf-8");
resp.setContentType("application/json; charset=utf-8");
PrintWriter out;

Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("itemType", "S0");
map1.put("itemId", "201707151008");
map1.put("itemName", "型号");
map1.put("quantity", "5");
map.put("result", "200");
map.put("OrderItemVO", map1);
JSONObject json = JSONObject.fromObject(map);

out = resp.getWriter();
out.println(json);
out.close();

}

}

4、  导出为为class文件

在项目名称上右键Export>>General>>File System

技术分享

技术分享

 

5、  将导出class文件放在classes目录下

技术分享

6、  配置web.xml

技术分享

7、  启动tomcat并验证,如下图表示成功

 技术分享

 

HTTP测试桩