1、传入参数对象化
public class Param { String no;//编号 String name;//姓名 String age;//年龄 String sex;//性别 String expResu;//期望结果 String actResu;//实际结果 String pass;//是否通过 String desc;//描述 .......}
2、使用Excel储存数据,表格如下
3、用poi读取和写入数据,这里我们封装一个ExcelUtil类,代码如下
public class ExcelUtil { //读取Excel中数据 public static List read() throws Exception{ HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow row = s.createRow(0); HSSFCell cell = row.createCell((int)0,0); //------------从xls读出数据 wb = new HSSFWorkbook(new FileInputStream("D:\\learn\\test.xls")); s = wb.getSheetAt(0); //获得EXCEL行数 int rowNums=s.getLastRowNum(); //获得Excell列数 //int columnNum=r.getPhysicalNumberOfCells(); List params=new ArrayList(); for(int i=1;i<=rowNums;i++){ HSSFRow r = s.getRow(i); cell=r.getCell(0); Param param= new Param(); param.setNo(r.getCell(0).getStringCellValue()); param.setName(r.getCell(1).getStringCellValue()); param.setAge(r.getCell(2).getStringCellValue()); param.setSex(r.getCell(3).getStringCellValue()); param.setExpResu(r.getCell(4).getStringCellValue());// System.out.println(cell.getRichStringCellValue()); params.add(param); } return params; } /** * 写入Excel,在任意坐标处写入数据。 * String value:你要输入的内容 * int x :行坐标,Excel从 0 算起 * int y :列坐标,Excel从 0 算起 */ public static void writeCell(String filePath,int x,int y,String value) { try { // 创建Excel的工作书册 Workbook,对应到一个excel文档 HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath)); HSSFSheet sheet=wb.getSheetAt(0); HSSFRow row=sheet.getRow(x); HSSFCell cell=row.getCell((short) y); cell.setCellValue(value); FileOutputStream os; os = new FileOutputStream(filePath); wb.write(os); os.close(); } catch (Exception e) { e.printStackTrace(); } }}
4、客户端代码如下:
public class TestClient { public static void main(String[]agrs){ TestClient a=new TestClient(); try { a.client(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void client() throws Exception{ List params = ExcelUtil.read(); for(Param pa:params){ try { // 接报文的地址 String param= new JsonsUtil().BuildJson(pa); URL serverUrl= new URL("http://localhost:8090/lctest/TestServer"); URLConnection uct= serverUrl.openConnection(); HttpURLConnection hutc=(HttpURLConnection)uct; // 设置报文参数 hutc.setRequestMethod("POST"); // 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在 http正文内,因此需要设为true, 默认情况下是false; hutc.setDoOutput(true); // 设置是否从httpUrlConnection读入,默认情况下是true hutc.setDoInput(true); // hutc.setAllowUserInteraction(true); // 开启流,写入数据data OutputStream out=hutc.getOutputStream(); out.write(param.getBytes("UTF-8")); out.flush(); out.close(); // 获取返回的数据 StringBuffer buffer=new StringBuffer(); BufferedReader reader = null; InputStream ins=hutc.getInputStream(); reader = new BufferedReader(new InputStreamReader(ins,"UTF-8")); String sg=reader.readLine(); if (sg!= null){ buffer.append(sg); } System.out.println("接收返回值:" + buffer); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
5、服务端代码如下:
public class TestServer extends HttpServlet { private static final long serialVersionUID = 1L; private static JSONArray ja; /** * @see HttpServlet#HttpServlet() */ public TestServer() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub try { this.excute(request, response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub try { this.excute(request, response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void excute(HttpServletRequest request,HttpServletResponse response) throws Exception{ request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/xml"); String method=request.getMethod(); String url=request.getRequestURI(); String param; // 获取收到的报文 BufferedReader reader = request.getReader(); String line = ""; line = reader.readLine(); ja=new JsonsTest().ParseJson(line); StringBuffer resultBuffer=new StringBuffer(); resultBuffer.append("访问方式"+method+"访问成功"); resultBuffer.append("接收到的数据:"+line+"name: "+ja.getJSONObject(0).getString("name")); PrintWriter out =response.getWriter(); out.println(resultBuffer.toString()); out.flush(); out.close(); }}
6、客户端调取服务端,运行结果如下:
下一步我们就要做接口的自动化测试设计
欢迎大家关注微信公众号与QQ群进行交流