zoukankan      html  css  js  c++  java
  • Junit接口测试(1)

    //用到的方法

    package interfaceTest_1;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.List;
    
    /**
     * Created by Ethan on 2015/5/12.
     */
    public class HTTPUtil {
        public static String postJson(String uri, String params) {
            HttpClient httpclient = new DefaultHttpClient();
            String backstr = null;
            try {
                HttpPost postMethod = new HttpPost(uri);
                postMethod.setEntity(new StringEntity(params, "utf-8")); // 灏嗗弬鏁板~鍏�OST
                postMethod.setHeader("Content-Type", "application/json; charset=utf-8");
                HttpResponse response = httpclient.execute(postMethod); // 鎵ц�POST鏂规硶
                backstr = EntityUtils.toString(response.getEntity(), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return backstr;
    
        }
    
        public static String dopost(String uri, List<BasicNameValuePair> params) {
            HttpClient httpclient = new DefaultHttpClient();
            String backstr = null;
            try {
                HttpPost postMethod = new HttpPost(uri);
                postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); // 灏嗗弬鏁板~鍏�OST
                // Entity涓�
                postMethod.setHeader("Content-Type",
                        "application/x-www-form-urlencoded; charset=utf-8");
    
                HttpResponse response = httpclient.execute(postMethod); // 鎵ц�POST鏂规硶
                backstr = EntityUtils.toString(response.getEntity(), "utf-8");
    
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return backstr;
    
        }
    }


    //回包处理

    package Vo;
    
    public class ResVo {
        private String data;
        private Integer result;
        private String msg;
        public String getData() {
            return data;
        }
        public void setData(String data) {
            this.data = data;
        }
        public Integer getResult() {
            return result;
        }
        public void setResult(Integer result) {
            this.result = result;
        }
        public String getMsg() {
            return msg;
        }
        public void setMsg(String msg) {
            this.msg = msg;
        }
        
    
    }

    //JSON格式上传测试

    package junitTest;
     
    
    import java.util.HashMap;
    import java.util.Map;
    import org.junit.Test;
    import com.alibaba.fastjson.JSON;
    import Vo.ResVo;
    import interfaceTest_1.HTTPUtil;
     
    
    public class XXX {
    
      //........
      @Test
      public void saveUserRatio(){
       int groupId=7;
       int babyId = 5;
       String website = "http://192.168.1.100/iftest/xxx.htm";  
       Map<String,Object> m = new HashMap<String,Object>();
       Map<String,Object> a = new HashMap<String,Object>();   
       m.put("groupId",groupId);
       a.put("babyId",babyId);
       m.put("a", a);
       String res = HTTPUtil.postJson(website,JSON.toJSONString(m));
       System.out.println(JSON.toJSONString(m));
       ResVo resVo = JSON.parseObject(res,ResVo.class);
       System.out.println(res);
       org.junit.Assert.assertSame(0, resVo.getResult()); 
      }


    //接口需要上传图片的测试

    package junitTest;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import org.apache.http.message.BasicNameValuePair;
    import org.junit.Test;
    import com.alibaba.fastjson.JSON;
    import Vo.ResVo;
    import interfaceTest_1.HTTPUtil;
    
    public class NotesTest {
    
        @Test
        public void notesSave(){
            String groupId="7";
            String pictureStatus = "0";
            String pictures = "1";
            String website = "http://192.168.1.100/if.htm";        
            List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
            params.add(new BasicNameValuePair("groupId",groupId));
            params.add(new BasicNameValuePair("note.pictureStatus", pictureStatus));
            params.add(new BasicNameValuePair("note.pictures", pictures));    
            System.out.println(JSON.toJSONString(params));
            String res = HTTPUtil.dopost(website,params);
            ResVo resVo = JSON.parseObject(res,ResVo.class);
            System.out.println(res);
            org.junit.Assert.assertSame(0, resVo.getResult());    
        }
    
    }
  • 相关阅读:
    C语言基于单链表得学生成绩管理系统
    C语言实现扫雷小程序外挂,棒棒的
    小白学习C语言一定要掌握的那些知识点!
    C语言快速入门教程之10分钟快速掌握数据类型
    神奇的C语言,这才是C语言大牛操作,作为面试题,怕是秒杀众人
    多线程
    java基础- Collection和map
    String 和 new String
    idea快捷键
    用bootstrap 分页插件问题
  • 原文地址:https://www.cnblogs.com/cvv54/p/4786662.html
Copyright © 2011-2022 走看看