Backend Development 10 min read

Designing a Reusable Automated API Testing Framework in Java

This article explains how to build a modular automated API testing project in Java by creating a base class that encapsulates request handling, header and cookie management, common validation methods, and response processing, allowing individual test modules to focus solely on specific API parameters and responses.

FunTester
FunTester
FunTester
Designing a Reusable Automated API Testing Framework in Java

Believe in the theory of ten thousand lines of code!

Starting today, I will share how to write an automated testing project. For a long‑term automation project, the idea is to create a base class that encapsulates request object creation, header and cookie handling, custom user information (e.g., id, uid, password), common validation methods, business response status extraction, and other needed utilities. Each business module then inherits from this base class and only needs to focus on its own API parameters and response data.

The base class implements an interface IBase that defines all these functionalities, usage scenarios, and precautions, enabling the common parts to be centralized while keeping module classes lightweight.

Video series

FunTester testing framework video introduction (Part 1)

Getting HTTP request objects – video

Sending requests and parsing responses – video

Basic JSON object operations – video

GET request practice – video

POST request practice – video

How to handle header and cookie – video

FunRequest class features – video

Business validation in interface testing – video

Interface testing business validation

Gitee address: https://gitee.com/fanapi/tester

Code

package com.fun.base.interfaces;

import com.alibaba.fastjson.JSONObject;
import com.fun.base.bean.RequestInfo;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;

import java.io.File;

/**
 * 每个项目需要重写的方法
 */
public interface IBase {

    /**
     * 获取get请求对象
     *
     * @param url
     * @return
     */
    HttpGet getGet(String url);

    /**
     * 获取get请求对象
     *
     * @param url
     * @param arg
     * @return
     */
    HttpGet getGet(String url, JSONObject arg);

    /**
     * 获取post请求对象
     *
     * @param url
     * @return
     */
    HttpPost getPost(String url);

    /**
     * 获取post请求对象
     *
     * @param url
     * @param params
     * @return
     */
    HttpPost getPost(String url, JSONObject params);

    /**
     * 获取post请求对象
     *
     * @param url
     * @param params
     * @param file
     * @return
     */
    HttpPost getPost(String url, JSONObject params, File file);

    /**
     * 获取响应
     *
     * @param request
     * @return
     */
    JSONObject getResponse(HttpRequestBase request);

    /**
     * 获取响应
     *
     * @param url
     * @return
     */
    JSONObject getGetResponse(String url);

    /**
     * 获取响应
     *
     * @param url
     * @param args
     * @return
     */
    JSONObject getGetResponse(String url, JSONObject args);

    /**
     * 获取响应
     *
     * @param url
     * @return
     */
    JSONObject getPostResponse(String url);

    /**
     * 获取响应
     *
     * @param url
     * @param params
     * @return
     */
    JSONObject getPostResponse(String url, JSONObject params);

    /**
     * 获取响应
     *
     * @param url
     * @param params
     * @param file
     * @return
     */
    JSONObject getPostResponse(String url, JSONObject params, File file);

    /**
     * 校验响应正确性
     *
* 用于处理响应结果,一般校验json的必要层级和响应码
     *
*
     * @param response
     * @return
     */
    boolean isRight(JSONObject response);

    /**
     * 检查响应是否符合标准
     *
* 会在fanlibrary类使用,如果没有ibase对象,会默认返回test_error_code
     * requestinfo主要用于校验该请求是否需要校验,黑名单有配置black_host提供
     *
*
     * @param response    响应json
     * @param requestInfo 请求info
     * @return
     */
    int checkCode(JSONObject response, RequestInfo requestInfo);

    /**
     * 登录
     */
    void login();

    /**
     * 设置header
     */
    void setHeaders(HttpRequestBase request);

    /**
     * 处理响应结果
     *
     * @param response
     */
    void handleResponseHeader(JSONObject response);

    /**
     * 获取公共的登录参数
     *
     * @return
     */
    JSONObject getParams();

    /**
     * 初始化对象,从json数据中,一般指cookie
     *
* 主要用于new了新的对象之后,然后赋值的操作,场景是从另外一个服务的对象拷贝到现在的对象,区别于clone,因为可能还会涉及其他的验证,所以单独写出一个方法,极少用到
     *
*/
    void init(JSONObject info);

    /**
     * 记录请求
     */
    void recordRequest();

    /**
     * 获取请求,用于并发
     *
     * @return
     */
    HttpRequestBase getRequest();

}

Disclaimer: This article was first published on the “FunTester” public account; please follow for discussion. Reproduction by third parties (except Tencent Cloud) is prohibited.

Technical article selection

Linux performance monitoring tool netdata Chinese version

Performance testing framework version 3

HTTP mind map illustration

Graphical output of performance test data

Measuring latency of asynchronous write interfaces in load testing

Quantitative performance testing for multiple login methods

JMeter throughput error analysis

Multi-project login conflict test cases

No-code article selection

Programming mindset for everyone

JSON basics

2020 Tester self‑improvement

Pitfalls for automation beginners (Part 1)

Pitfalls for automation beginners (Part 2)

How to become a full‑stack automation engineer

Manual testing vs. automated testing?

Why automated testing projects fail

Simplifying test cases

JavaAutomationBackend DevelopmentHTTPCode Exampletesting frameworkAPI testing
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.