Operations 7 min read

Building Maintainable API Test Modules in Java: UserCenter Case Study

This article presents a practical guide to designing a usercenter module for API testing in Java, detailing a maintenance‑free data strategy, key automation principles, and a full code example that demonstrates user info retrieval, password modification, avatar upload, and feedback handling, with a Gitee repository link.

FunTester
FunTester
FunTester
Building Maintainable API Test Modules in Java: UserCenter Case Study

The author introduces the usercenter module, a component of a larger project that handles user management and automated test cases. The goal is to create test cases that never require data maintenance, ensuring that test data remains unpolluted and reliable.

For automation, the author advocates avoiding hard‑coded data unless it is immutable static configuration. They recommend not using parameterization or preset conditions, instead relying on comprehensive monitoring and early warning to significantly reduce maintenance costs. A separate strategy for single‑interface parameterized tests is mentioned but deferred to a future discussion.

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

The following Java class illustrates the implementation:

package com.okayqa.studentapd.function;

import com.alibaba.fastjson.JSONObject;
import com.fun.frame.Output;
import com.fun.frame.Save;
import com.fun.utils.Join;
import com.fun.utils.RString;
import com.fun.utils.Time;
import com.fun.utils.WriteRead;
import com.okayqa.common.Common;
import com.okayqa.common.Users;
import com.okayqa.studentapd.base.OkayBase;
import com.okayqa.studentapd.profile.UserApi;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;

public class UserCenter extends OkayBase {
    private static Logger logger = LoggerFactory.getLogger(UserCenter.class);

    public UserCenter(OkayBase okayBase) {
        super(okayBase);
    }

    /**
     * Get user information
     */
    public JSONObject getUserInfo() {
        String api = UserApi.USER_INFO;
        JSONObject params = getParams();
        JSONObject response = getPostResponse(api, params);
        output(response);
        return response;
    }

    /**
     * Modify password; by default the username is used as the password and the token is refreshed.
     */
    public JSONObject modifyPwd(String oldpwd, String newpwd) {
        String url = UserApi.MODIFY_PWD;
        JSONObject params = getParams();
        params.put("newpwd", getPassword(newpwd));
        params.put("oldpwd", getPassword(oldpwd));
        JSONObject response = getPostResponse(url, params);
        output(response);
        if (isRight(response)) {
            String token = response.getJSONObject("data").getString("token");
            this.setToken(token);
            super.setToken(token);
        }
        return response;
    }

    public void testDemo001() {
        OkayBase okayBase = new OkayBase(Users.getStuUser(1393), WriteRead.readTextByString("testdemo001"));
        UserCenter userCenter = new UserCenter(okayBase);
        String randomStr = RString.getStringWithoutNum(10);
        userCenter.modifyPwd(userCenter.pwd, randomStr);
        new OkayBase(Users.getStuUser(1393), randomStr);
        Save.info(randomStr, "testdemo001");
    }

    /**
     * Upload user avatar
     */
    public JSONObject uploadAvatar() {
        JSONObject params = getParams();
        params.put("avatar", "file");
        JSONObject response = getPostResponse(UserApi.UPLOAD_AVATAR, params, new File(Common.PIC_PATH));
        output(response);
        return response;
    }

    /**
     * Get feedback list
     */
    public JSONObject feedBackList() {
        String api = UserApi.FEED_BACK_LIST;
        JSONObject params = getParams();
        params.put("currentpage", "1");
        JSONObject response = getPostResponse(api, params);
        output(response);
        return response;
    }

    /**
     * Submit feedback
     */
    public JSONObject feedBack() {
        String api = UserApi.FEED_BACK;
        JSONObject params = getParams();
        params.put("content", "I am testing:" + Time.getDate());
        JSONObject response = getPostResponse(api, params);
        Output.output(response);
        return response;
    }

    public JSONObject submitSubject(int... subjects) {
        String api = UserApi.SUBJECT_SUBMIT;
        JSONObject params = getParams();
        params.put("schoolTypeId", 1);
        params.put("subjectIds", Join.join(ArrayUtils.toObject(subjects), ",", "[", "]"));
        params.put("gradeIds", "[12]");
        params.put("typeId", 1);
        params.put("currentpage", 1);
        params.put("contype", 3);
        JSONObject response = getPostResponse(api, params);
        output(response);
        return response;
    }

    /**
     * Verify token for XiaoE Tong
     */
    public JSONObject checkToken() {
        String api = UserApi.CHECK_TOKEN;
        JSONObject params = getParams();
        params.put("source", "imcenter");
        params.put("utype", 1);
        params.put("user_info", getJson("gender=1", "avatar_url=https://www.google.com", "nickname=FunTester"));
        JSONObject response = getPostResponse(api, params);
        output(response);
        return response;
    }
}

The class extends OkayBase and provides methods for common user‑related API operations, each constructing request parameters, invoking getPostResponse, outputting the result, and returning the JSON response. The testDemo001 method demonstrates a full test flow: initializing the base, modifying the password with a random string, and recording the outcome.

By following this structure, developers can create reusable, low‑maintenance test modules that integrate seamlessly with the FunTester framework and support reliable automated testing of user‑center functionalities.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend Developmenttest automationCode ExampleAPI testing
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

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.