• 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());    
        }
    
    }
  • 相关阅读:
    Eos开发——构造查询条件
    随记
    Spring的三种通过XML实现DataSource注入方式
    事务处理
    Spring AOP实例——异常处理和记录程序执行时间
    输出日志实例改成用Spring的AOP来实现
    用ProxyFactoryBean创建AOP代理
    Spring的通知(Advice)
    Spring的3种切入点PointCut实现
    学习AOP之JAVA的代理机制
  • 原文地址:https://www.cnblogs.com/cvv54/p/4786662.html
Copyright © 2020-2023  润新知