A Reusable Java Verification Utility for Automated Testing

This article presents a reusable Java verification class that centralizes common validation logic for automated testing, handling JSON responses, code checks, numeric and boolean assertions, array detection, and regex matching, while also fixing hidden bugs and extending array validation capabilities.

FunTester
FunTester
FunTester
A Reusable Java Verification Utility for Automated Testing

Overview

The Verify class is a reusable Java utility for automated test validation. It wraps a net.sf.json.JSONObject response and provides concise methods to assert status codes, extract node values, and verify data‑type constraints (numeric, boolean, array, nested JSON) without writing repetitive validation logic in each test case.

Key fields

verifyJson – the JSON object under test.

code – placeholder for the response code (retrieved via FanLibrary.getiBase().checkCode).

lines – a List<String> containing a line‑by‑line representation of the JSON, generated by parseJsonLines for fast string matching.

Core verification methods

int getCode()

– returns the response code by delegating to the test framework’s checkCode method. boolean isRight() – convenience check that the code equals 0. String getValue(String key) – scans lines for a line that starts with key: and returns the value after the colon. boolean isContains(String... text) – verifies that every supplied text fragment appears anywhere in the JSON string. boolean isNum(String... value) – confirms that the specified keys exist and their values are numeric (uses an internal isNumber helper). boolean notNull(String... keys) – ensures the listed keys are present and their values are non‑empty strings. boolean isArray(String key) – checks the character following the key in the raw JSON; if it is ‘[’, the node is an array. boolean isJson(String key) – similar to isArray but looks for ‘{’ to detect a nested JSON object. boolean isBoolean(String... value) – validates that the values of the given keys match the literals true or false using a regular‑expression check. boolean isRegex(String regex) – applies a custom regular expression to the entire JSON string.

JSON line parsing

The static method parseJsonLines(JSONObject response) converts a JSON object into a list of key:value strings. It removes punctuation, quotes, and escape characters, replaces delimiters with a line separator constant ( LINE), and splits the result. This representation enables fast, index‑free look‑ups for the verification methods above.

Example usage

import net.sf.json.JSONObject;

// Assume `response` is the JSON returned by an HTTP request
JSONObject response = ...;

// Create the verifier
Verify verifier = new Verify(response);

// Basic status‑code check
if (!verifier.isRight()) {
    throw new AssertionError("Unexpected response code: " + verifier.getCode());
}

// Verify specific fields
verifier.isContains("success", "data");
verifier.isNum("totalCount");
verifier.notNull("userId", "sessionId");
verifier.isBoolean("active");
verifier.isArray("items");
verifier.isJson("metadata");

// Custom regex validation
verifier.isRegex("\\\"id\\\":\\\"[0-9a-f]{24}\\\"");
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.

JavaJSONutilitycode-reuseautomation testingverification
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.