Groovy-based API Testing Framework for Password Modification Endpoint
This article explains how to build a Groovy‑driven, modular API testing framework that logs in, obtains a token, and programmatically changes a user's password while handling concurrency and response validation.
When testing interfaces, especially those with business‑level dependencies, generic tools like JMeter, Postman, or SoapUI become cumbersome. The author prefers script‑based execution and therefore chose Groovy as the primary language, reusing Java‑based testing libraries from a previous framework.
The architecture groups APIs by module, using a User object that carries attributes such as uname, pwd, token, and userInfoBean to pass information between module classes.
For the password‑change API, the workflow is: log in to obtain a token, call the modify‑password endpoint with newpwd, oldpwd, and token, and on success receive a new token for subsequent operations.
Below is the main test script that creates a configurable number of threads and requests per thread, then runs them concurrently.
class T8 extends OkayBase {
public static void main(String[] args) {
int thread = changeStringToInt(args[0])
int times = changeStringToInt(args[1])
List<ThreadBase> 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‑change request logic.
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 string = response.getJSONObject("data").getString("token");
this.setToken(string);
}
return response;
}
}The base class OkayBase provides common utilities such as token handling, 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;
}
// getters and setters omitted for brevity
@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, resource release
*/
public static void allOver() {
FanLibrary.testOver();
}
}Running the script with appropriate thread and request counts executes concurrent password‑change operations, demonstrating a scalable approach to API testing.
Readers are invited to view the original article and discuss further.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
