Backend Development 7 min read

API Business Validation and Testing Practices with the FunTester Framework

This article demonstrates practical API business validation using the FunTester testing framework, covering user registration and login scenarios with example Java code, and provides a series of instructional videos for HTTP request handling, response parsing, and header/cookie management.

FunTester
FunTester
FunTester
API Business Validation and Testing Practices with the FunTester Framework

The basic functions of the interface framework have been covered, and now the article shares practical API testing practices, focusing on business validation for user registration and login using an open‑source API for demonstration. Test scripts are written without data‑driven approaches or traditional assertions, resembling a BDD style and employing multi‑interface verification.

Video series:

FunTester testing framework video introduction (序)

Obtaining HTTP request objects – testing framework video

Sending requests and parsing responses – testing framework video

Basic JSON object operations – video

GET request practice – testing framework video

POST request practice – video demonstration

Handling headers and cookies – video demonstration

FunRequest class features – video demonstration

API Business Validation

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

package com.fun;

import com.alibaba.fastjson.JSONObject;
import com.fun.config.HttpClientConstant;
import com.fun.frame.httpclient.FanLibrary;
import com.fun.utils.RString;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpPost;

public class AR extends FanLibrary {
    public static String APIKEY;

    public static void main(String[] args) {
        testDemo001();
        testDemo002();
        testOver();
    }

    public static void testDemo001() {
        JSONObject funTester = registerUser("FunTester", RString.getString(10), RString.getString(10));
        int code = funTester.getIntValue("code");
        if (code != 400) fail();
    }

    public static void testDemo002() {
        String name = "Fun" + getMark();
        String pwd = RString.getString(10);
        String remark = RString.getString(10);
        JSONObject funTester = registerUser(name, pwd, remark);
        int code = funTester.getIntValue("code");
        if (code != 200) fail();
        JSONObject result = funTester.getJSONObject("result");
        String name1 = result.getString("name");
        if (!name.equals(name1)) fail();
        String remark1 = result.getString("remarks");
        if (!remark.equals(remark1)) fail();
        userLogin(name, pwd);
    }

    public static JSONObject registerUser(String name, String passwd, String remark) {
        if (StringUtils.isEmpty(APIKEY)) developerLogin();
        String url = "https://api.apiopen.top/registerUser";
        JSONObject param = new JSONObject();
        param.put("apikey", APIKEY);
        param.put("name", name);
        param.put("passwd", passwd);
        param.put("nikeName", "FunTester");
        param.put("headerImg", "http://pic.automancloud.com/sick-jvm-heap-1.png");
        param.put("phone", "13100001111");
        param.put("email", "[email protected]");
        param.put("vipGrade", "3");
        param.put("autograph", "abc");
        param.put("remarks", remark);
        HttpPost httpPost = getHttpPost(url, param);
        JSONObject response = getHttpResponse(httpPost);
        output(response);
        return response;
    }

    public static void register() {
        String url = "https://api.apiopen.top/developerRegister";
        JSONObject param = new JSONObject();
        param.put("name", "FunTester");
        param.put("passwd", "FunTester");
        param.put("email", "[email protected]");
        HttpPost httpPost = getHttpPost(url, param);
        JSONObject response = getHttpResponse(httpPost);
        output(response);
    }

    public static void userLogin(String name, String pwd) {
        if (StringUtils.isEmpty(APIKEY)) developerLogin();
        String url = "https://api.apiopen.top/loginUser";
        JSONObject param = new JSONObject();
        param.put("name", name);
        param.put("passwd", pwd);
        param.put("apikey", APIKEY);
        HttpPost httpPost = getHttpPost(url, param);
        JSONObject response = getHttpResponse(httpPost);
        output(response);
        if (!(response.getIntValue("code") == 200)) fail();
    }

    public static void developerLogin() {
        String url = "https://api.apiopen.top/developerLogin";
        JSONObject params = new JSONObject();
        params.put("name", "funtester");
        params.put("passwd", "funtester");
        HttpPost httpPost = getHttpPost(url, params);
        httpPost.addHeader(HttpClientConstant.X_Requested_KWith);
        httpPost.addHeader(getHeader("name", "FunTester"));
        JSONObject response = getHttpResponse(httpPost);
        output(response);
        if (response.getIntValue("code") == 200) {
            APIKEY = response.getJSONObject("result").getString("apikey");
        } else {
            fail();
        }
    }
}

Disclaimer: This article was originally published on the "FunTester" public account; please follow the original source and do not repost without permission (except Tencent Cloud).

Technical article selections include topics such as Linux performance monitoring (netdata), performance testing frameworks, HTTP mind maps, visualizing test data, latency measurement for asynchronous writes, multi‑login performance testing, and JMeter throughput error analysis.

BackendJavaautomationHTTPtesting 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.