Using Java Reflection to Execute API Test Cases in an HttpClient Framework
The article presents a Java HttpClient testing framework that leverages reflection to dynamically invoke API methods, detailing optimized functions for running all test cases of an interface, single test cases, and the core execution logic that interacts with a MySQL database and records results.
The author describes building a simple HttpClient-based API testing framework in Java, repeatedly refactoring it until a long‑standing issue was resolved, with a key technique being the use of Java reflection to invoke methods based on parameter names.
Below is the optimized method for executing all test cases of a single API interface:
// 发帖话题分类接口 001
public void case_shequ_topic_cattlist() {
String apiName = "shequ_topic_cattlist";
mark = getMark();
executeCase(apiName, mark);
}Below is the optimized method for executing a single test case of an API interface:
// 创建帖子接口 002
public void case_shequ_post_create() {
String apiName = "shequ_post_create";
mark = getMark();
executeCase(apiName, mark);
}The executeCase() method reads test cases from a MySQL database, iterates over them, extracts expected and actual verification values, compares results, writes outcomes back to the database, and finally aggregates the run statistics:
public void executeCase(String apiName, int mark) {
List<Map<String, String>> data = LocalMySql.getInstance().getCaseFromMySql(apiName);// 数据库读取用例
for (int i = 0; i < data.size(); i++) {// 遍历执行用例
Map<String, String> use = data.get(i);// 获取单个用例
String case_id = use.get("case_id");
String expect_value1 = use.get("verify_value1");// 获取验证点期望值
String expect_value2 = use.get("verify_value2");// 获取验证点期望值
String expect_value3 = use.get("verify_value3");// 获取验证点期望值
String actual_key1 = use.get("verify_key1");// 获取检查点key
String actual_key2 = use.get("verify_key2");// 获取检查点key
String actual_key3 = use.get("verify_key3");// 获取检查点key
String params = use.toString();// 记录传入参数
JSONObject response = executeMethodByName(apiName, use);
String actual_value1 = getDataValue(response, actual_key1);// 获取验证点实际值
String actual_value2 = getDataValue(response, actual_key2);// 获取验证点实际值
String actual_value3 = getDataContains(response, actual_key3, expect_value3);// 获取验证点实际值,此为包含验证
String[] data2 = { expect_value1, actual_value1, expect_value2, actual_value2, expect_value3,
actual_value3 };
int result = getResult(data2) ? 1 : 2;// 获取测试结果,1为通过,2为失败
LocalMySql.getInstance().saveApiTestResult(case_id, mark, result, apiName, expect_value1, actual_value1,
expect_value2, actual_value2, expect_value3, actual_value3, params);// 写入数据库
}
LocalMySql.getInstance().addApiTestResult(apiName, mark, test_mark);// 统计本次运行所有用例结果
}The executeMethodByName() method demonstrates how reflection is used to load a class, obtain a method by its name, invoke it with a map of parameters, and return the resulting JSON object:
public JSONObject executeMethodByName(String apiName, Map<String, String> use) {
JSONObject jsonObject = null;
Object obj = null;
Method method = null;
try {
// 里面写自己的类名及路径
Class<?> c = Class.forName("juziyule.Special_juzi");
obj = c.newInstance();
// 第一个参数写的是方法名,第二个\第三个\...写的是方法参数列表中参数的类型
method = c.getMethod(apiName, Map.class);
// invoke是执行该方法,并携带参数值
} catch (Exception e) {
output("反射执行出错!", e);
}
try {
jsonObject = (JSONObject) method.invoke(obj, new Object[] { use });
} catch (Exception e) {
output("反射运行方法异常!", e);
}
return jsonObject;
}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.
