基于java的直线型接口测试框架初探

在使用java语言作为接口测试的过程中,发现java语言的简洁性的确不如脚本语言,如python,很多功能python一行代码几个方法就能搞定,java需要几行,而且有时候并不利于理解。最近接触到了一个词“直线型”代码。看了之后有所感觉,重新写了一个直线型代码风格的接口请求框架。

非常简单,只支持get和post接口,返回json格式的响应,后期会不断完善这个框架。

使用前的代码风格如下:

使用后的代码风格如下:

看起来是不是更容易理解了。哈哈……

源码如下:

package com.fission.source.httpclient;

import net.sf.json.JSONObject;
import org.apache.http.Header;
import org.apache.http.client.methods.HttpRequestBase;

import java.util.ArrayList;
import java.util.List;

/**
 * 预计重写apilibrary,使用面对对象思想
 */
public class FanRequest {
    /**
     * 请求类型,true为get,false为post
     */
    boolean requestType;
    /**
     * 请求对象
     */
    HttpRequestBase request;
    /**
     * 接口地址
     */
    String apiName;
    /**
     * host地址
     */
    String host;
    /**
     * header集合
     */
    List<Header> headers = new ArrayList<>();
    /**
     * get参数
     */
    JSONObject args = new JSONObject();
    /**
     * post参数
     */
    JSONObject params = new JSONObject();

    /**
     * 构造方法
     *
     * @param requestType
     */
    private FanRequest(boolean requestType) {
        this.requestType = requestType;
    }


    /**
     * 获取get对象
     *
     * @return
     */
    public static FanRequest isGet() {
        FanRequest FanRequest = new FanRequest(true);
        return FanRequest;
    }

    /**
     * 获取post对象
     *
     * @return
     */
    public static FanRequest isPost() {
        FanRequest FanRequest = new FanRequest(false);
        return FanRequest;
    }

    /**
     * 设置host
     *
     * @param host
     * @return
     */
    public FanRequest setHost(String host) {
        this.host = host;
        return this;
    }

    /**
     * 设置接口地址
     *
     * @param apiName
     * @return
     */
    public FanRequest setApiName(String apiName) {
        this.apiName = apiName;
        return this;
    }

    /**
     * 添加get参数
     *
     * @param key
     * @param value
     * @return
     */
    public FanRequest addArgs(Object key, Object value) {
        args.put(key, value);
        return this;
    }

    /**
     * 添加post参数
     *
     * @param key
     * @param value
     * @return
     */
    public FanRequest addParam(Object key, Object value) {
        params.put(key, value);
        return this;
    }

    /**
     * 添加header
     *
     * @param key
     * @param value
     * @return
     */
    public FanRequest addHeader(Object key, Object value) {
        headers.add(ApiLibrary.getHeader(key.toString(), value.toString()));
        return this;
    }

    /**
     * 获取请求响应
     *
     * @return
     */
    public JSONObject getResponse() {
        String url = host + apiName;
        if (requestType) {
            request = ApiLibrary.getHttpGet(url, args);
        } else {
            request = ApiLibrary.getHttpPost(url + ApiLibrary.changeJsonToArguments(args), params);
        }
        headers.forEach(header -> request.addHeader(header));
        return ApiLibrary.getHttpResponse(request);
    }
}

欢迎有兴趣的朋友一起交流:群号:340964272

猜你喜欢

转载自blog.csdn.net/Fhaohaizi/article/details/82254171