A Generic Verification Utility Class in Java for Automated Testing
This article introduces a reusable Java verification utility class designed for automated testing, detailing its structure, key methods for code validation, value extraction, type checks, and JSON parsing, and provides the full source code for developers to integrate and extend in their projects.
In the process of automation, many validation points are encountered; this article presents a generic verification class in Java that consolidates common validation functions, including code checking, value retrieval, containment checks, numeric and boolean validation, array and JSON detection, and regex matching, along with a utility to parse JSON into line lists.
package com.fun.frame;
import com.fun.base.bean.RequestInfo;
import com.fun.base.interfaces.IBase;
import com.fun.frame.httpclient.FanLibrary;
import com.fun.utils.Regex;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 通用验证方法封装
*/
public class Verify extends SourceCode {
private static Logger logger = LoggerFactory.getLogger(Verify.class);
/**
* 断言的json对象
*/
private JSONObject verifyJson;
/**
* 断言的code码
*/
private int code;
/**
* 断言的json对象分行解析
*/
private List
lines = new ArrayList<>();
public Verify(JSONObject jsonObject) {
this.verifyJson = jsonObject;
this.lines = parseJsonLines(jsonObject);
}
/**
* 获取 code
*
这里的requestinfo主要的目的是为了拦截一些不必要的checkcode验证的,主要有black_host名单提供,在使用时,注意requestinfo的非空校验
* @return
*/
public int getCode() {
return FanLibrary.getiBase().checkCode(verifyJson, null);
}
/**
* 校验code码是否正确,==0
* @return
*/
public boolean isRight() {
return 0 == this.getCode();
}
/**
* 获取节点值
* @param key 节点
* @return 返回节点值
*/
public String getValue(String key) {
int size = lines.size();
for (int i = 0; i < size; i++) {
String line = lines.get(i);
if (line.startsWith(key + ":"))
return line.replaceFirst(key + ":", EMPTY);
}
return EMPTY;
}
/**
* 校验是否包含文本
* @param text 需要校验的文本
* @return 返回 Boolean 值
*/
public boolean isContains(String... text) {
boolean result = true;
String content = verifyJson.toString();
int length = text.length;
for (int i = 0; i < length; i++) {
if (!result) break;
result = content.contains(text[i]) & result;
}
return result;
}
/**
* 校验节点值为数字
* @param value 节点名
* @return 返回 Boolean 值
*/
public boolean isNum(String... value) {
boolean result = true;
int length = value.length;
for (int i = 0; i < length; i++) {
String key = value[i] + ":";
if (!verifyJson.toString().contains(value[i]) || !result)
return false;
for (int k = 0; k < lines.size(); k++) {
String line = lines.get(k);
if (line.startsWith(key)) {
String lineValue = line.replaceFirst(key, EMPTY);
result = isNumber(lineValue) & result;
}
}
}
return result;
}
/**
* 校验节点值不为空
* @param keys 节点名
* @return 返回 Boolean 值,为空返回false,不为空返回true
*/
public boolean notNull(String... keys) {
boolean result = true;
for (int i = 0; i < keys.length; i++) {
String key = keys[i] + ":";
if (!verifyJson.toString().contains(keys[i]) || !result)
return false;
for (int k = 0; k < lines.size(); k++) {
String line = lines.get(k);
if (line.startsWith(key)) {
String lineValue = line.replaceFirst(key, EMPTY);
result = lineValue != null & !lineValue.isEmpty() & result;
}
}
}
return result;
}
/**
* 验证是否为列表,根据字段后面的符号是否是[
* @param key 返回体的字段值
* @return
*/
public boolean isArray(String key) {
String json = verifyJson.toString();
int index = json.indexOf(key);
char a = json.charAt(index + key.length() + 2);
return a == '[';
}
/**
* 验证是否是json,根据后面跟的符号是否是{
* @param key 返回体的字段值
* @return
*/
public boolean isJson(String key) {
String json = verifyJson.toString();
int index = json.indexOf(key);
char a = json.charAt(index + key.length() + 2);
if (a == '{')
return true;
return false;
}
/**
* 是否是Boolean值
* @return
*/
public boolean isBoolean(String... value) {
boolean result = true;
int length = value.length;
for (int i = 0; i < length; i++) {
String key = value[i] + ":";
if (!verifyJson.toString().contains(value[i]) || !result)
return false;
for (int k = 0; k < lines.size(); k++) {
String line = lines.get(k);
if (line.startsWith(key)) {
String lineValue = line.replaceFirst(key, EMPTY);
result = Regex.isRegex(lineValue, "^(false)|(true)$") & result;
}
}
}
return result;
}
/**
* 验证正则匹配结果
* @param regex
* @return
*/
public boolean isRegex(String regex) {
String text = verifyJson.toString();
return Regex.isRegex(text, regex);
}
/**
* 解析json信息
* @param response json格式的响应实体
* @return json每个字段和值,key:value形式
*/
public static List
parseJsonLines(JSONObject response) {
String jsonStr = response.toString();// 先将json对象转化为string对象
jsonStr = jsonStr.replaceAll(",", LINE);
jsonStr = jsonStr.replaceAll("\"", EMPTY);
jsonStr = jsonStr.replaceAll("\\/", OR);
jsonStr = jsonStr.replaceAll("\\{", LINE);
jsonStr = jsonStr.replaceAll("\\[", LINE);
jsonStr = jsonStr.replaceAll("}", LINE);
jsonStr = jsonStr.replaceAll("]", LINE);
List
jsonLines = Arrays.asList(jsonStr.split(LINE));
return jsonLines;
}
}FunTester
10k followers, 1k articles | completely useless
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.