Android开发效率提升

接口联调及测试效率提升实践

2017-01-07  本文已影响0人  ammike

接口测试痛点(开发新东方四六级所遇到的)

解决实践

一、思路描述#####
  1. 接口未开发阶段:采用rap,进行接口地址的配置,参数的封装
  2. 接口部署阶段:
二、实际操作#####
  1. 安装fiddler 设置代理,为了避免无用数据,设置filtters,一般把服务器域名配置上就可以了

  2. 手机或者电脑上设置代理到 代理服务器

  3. 写单元测试 封装接口(此处忽略)

  4. 导出fiddler里面所有数据,并放置到assets目录,如下图所示


    Paste_Image.png
  5. 写okhttp拦截器,读取本地离线数据:

public class LocalJsonInterceptor implements Interceptor{
    public static final int SECOND = 1000;
    /**
     * 设置网络延时模拟 默认2秒
     */
    public static long mockNetDelay = 2;
    @Override
    public Response intercept(Chain chain) throws IOException {
        String responseString = createResponseBody(chain);

        Response response = new Response.Builder()
                .code(200)
                .message(responseString)
                .request(chain.request())
                .protocol(Protocol.HTTP_1_0)
                .body(ResponseBody.create(MediaType.parse("application/json"), responseString.getBytes()))
                .addHeader("content-type", "application/json")
                .build();
        return response;
    }

    /**
     * 读文件获取json字符串,生成ResponseBody
     *
     * @param chain
     * @return
     */
    private String createResponseBody(Chain chain) {

        HttpUrl uri = chain.request().url();
        String path = uri.url().getPath();
        String host = uri.host();
        try{

            if(mockNetDelay>0){
                Thread.sleep(mockNetDelay*SECOND);
            }
            return getresponseString(host,path);
        }catch (Exception e){
            ResponseBean bean = new ResponseBean();
            bean.setCode(-123);
            bean.setMessage("呵呵---->>读取本地json数据发生线程异常");
            Gson gson = new Gson();
            return gson.toJson(bean);
        }
    }

    public String getresponseString(String host,String path){
        try {
            String subPath = path.substring(0,path.lastIndexOf("/"));
            String method = path.substring(path.lastIndexOf("/")+1,path.length());
            String [] methodJson = App.getInstance().getAssets().list(host+subPath);
            Random random=new Random();
            int index = 0;
            // 随机50次 能不能找到 看命 此处可以随机同一接口不同场景下的返回数据
            int i;
            for(i= 0 ;i<50;i++){
                index=random.nextInt(methodJson.length);
                int findIndex = methodJson[index].indexOf("[");
                if(findIndex>=0){
                    if(method.equals(methodJson[index].substring(0,findIndex))){
                        break;
                    }
                }else if(method.equals(methodJson[index])){
                    break;
                }
            }
            if(i == 50){
                ResponseBean bean = new ResponseBean();
                bean.setCode(-122);
                bean.setMessage("呵呵---->>本地json数据未找到");
                Gson gson = new Gson();
                return gson.toJson(bean);
            }

            InputStream is = App.getInstance().getAssets().open(host+subPath+"/"+methodJson[index]);
            int size = is.available();

            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String text = new String(buffer, "GB2312");
            return text;

        } catch (Exception e) {
            ResponseBean bean = new ResponseBean();
            bean.setCode(-121);
            bean.setMessage("local---->>"+e.toString());
            Gson gson = new Gson();
            return gson.toJson(bean);
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读