Backend Development 5 min read

Groovy‑Based Automated API Testing Framework for Password Modification

This article presents a Groovy‑driven, modular API testing framework that automates login, token handling, and password‑change requests using multithreaded execution, and includes full source code for the test driver, UserCenter, and OkayBase classes.

FunTester
FunTester
FunTester
Groovy‑Based Automated API Testing Framework for Password Modification

The author discusses the challenges of testing interrelated business APIs and chooses Groovy as the scripting language for a custom, modular testing framework, preferring script‑based execution over tools like JMeter, Postman, or SoapUI.

The test flow for the password‑change API is described: first log in to obtain a token, then call the modify‑password endpoint with newpwd , oldpwd , and token ; a successful response returns a new token for subsequent operations.

Below is the main test script that creates a configurable number of threads, each performing a series of password‑change requests. The script uses a ThreadBase abstraction and a UserCenter helper to encapsulate the API calls.

class T8 extends OkayBase {
    public static void main(String[] args) {
        int thread = changeStringToInt(args[0]);
        int times = changeStringToInt(args[1]);
        List
threads = new ArrayList<>();
        for (int i = 0; i < thread; i++) {
            OkayBase base = getBase(i);
            UserCenter userCenter = new UserCenter(base);
            userCenter.modifyPwd();
            ThreadBase threadBase = new ThreadBase() {
                @Override
                protected void before() {}
                @Override
                protected void doing() throws Exception {
                    userCenter.modifyPwd();
                }
                @Override
                protected void after() {}
            };
            threadBase.setTimes(times);
            threads.add(threadBase);
        }
        new Concurrent(threads).start();
        allOver();
    }
}

The UserCenter class encapsulates the password‑modification request, building parameters, sending a POST request, handling the response, and updating the token when the operation succeeds.

public class UserCenter extends OkayBase {
    private static Logger logger = LoggerFactory.getLogger(UserCenter.class);
    public UserCenter(OkayBase okayBase) { super(okayBase); }
    public JSONObject modifyPwd() {
        String url = UserApi.MODIFY_PWD;
        JSONObject params = getParams();
        params.put("newpwd", getPassword(this.getUname()));
        params.put("oldpwd", getPassword(this.getPwd()));
        JSONObject response = getPostResponse(url, params);
        output(response);
        if (isRight(response)) {
            String token = response.getJSONObject("data").getString("token");
            this.setToken(token);
        }
        return response;
    }
}

The base class OkayBase provides common utilities such as login handling, password encryption, parameter construction, and response validation.

public class OkayBase extends SourceCode implements IBase {
    private static Logger logger = LoggerFactory.getLogger(OkayBase.class);
    int uid;
    String token;
    String uname;
    String pwd;
    public OkayBase(String uname, String pwd) {
        this.uname = uname;
        this.pwd = pwd;
        login();
    }
    public String getPassword() {
        String s = uname.substring(uname.length() - 6);
        return getPassword(s);
    }
    public String getPassword(String pwd) { return RSAUtils.getPassword(pwd); }
    public JSONObject getParams() {
        JSONObject json = getJson("uid=" + uid, "token=" + token);
        json.put("imei", "isFake");
        json.put("serial", "W170500652");
        json.put("ua", "f_an_4..0");
        return json;
    }
    @Override
    public boolean isRight(JSONObject jsonObject) {
        int code = TEST_ERROR_CODE;
        try {
            code = jsonObject.getJSONObject("meta").getInt("ecode");
            JSONObject data = jsonObject.getJSONObject("data");
            return code == 0 && !data.isEmpty();
        } catch (Exception e) {
            return false;
        }
    }
    /** Test cleanup */
    public static void allOver() { FanLibrary.testOver(); }
}

Finally, the author invites readers to read the original article and join the discussion.

backendautomationGroovythreadingPassword ManagementAPI 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.