Payload Driven Framework Creation
Building the Framework
- Define a standard payload format (e.g., JSON files or Excel sheets) for each API endpoint's test data.
- Build a "payload factory" component that reads and assembles payloads dynamically for each test.
- Create reusable utility methods to send requests and validate responses consistently.
- Wire the payload source into TestNG's
@DataProviderso each data row runs as a separate test iteration.
Why This Approach Scales Well
Once built, adding a new test scenario often just means adding a new row of data or a new JSON payload file — no new code required.
A Folder Layout That Keeps Data and Code Apart
The framework stays maintainable when every concern has its own place. A typical Maven layout looks like this:
src/test/java
base/ BaseTest.java (base URI, auth, common specs)
payloads/ PayloadFactory.java (loads templates, applies overrides)
clients/ UserClient.java (one method per endpoint)
tests/ CreateUserTest.java (assertions only)
utils/ ConfigReader.java
src/test/resources
config/ qa.properties, dev.properties
payloads/ createUser.json (base template)
testdata/ createUser-cases.json (one entry per scenario)
testng.xml
Tests never contain raw JSON or URLs. They ask the factory for a payload, call a client method and check the response. Each layer has one reason to change: if an endpoint path moves, only the client class is edited; if a new mandatory field appears, only the template file is updated, and every scenario picks it up automatically.
Worked Example: Template Plus Overrides
Instead of storing a full body for every scenario, keep one valid template and describe each case as a set of changes to it. The template, createUser.json:
{ "name": "Test User", "email": "test.user@example.com", "age": 28, "role": "viewer" }
The scenario file lists only what differs, plus the expected outcome:
[
{ "case": "valid user", "overrides": {}, "status": 201 },
{ "case": "missing email", "overrides": { "email": null }, "status": 400 },
{ "case": "underage", "overrides": { "age": 15 }, "status": 400 },
{ "case": "unknown role", "overrides": { "role": "superuser" }, "status": 400 }
]
A small factory built on Jackson merges the two:
public class PayloadFactory {
private static final ObjectMapper MAPPER = new ObjectMapper();
public static String build(String template, Map<String, Object> overrides) throws IOException {
Path file = Path.of("src/test/resources/payloads", template);
ObjectNode node = (ObjectNode) MAPPER.readTree(Files.readString(file));
overrides.forEach((key, value) -> node.set(key, MAPPER.valueToTree(value)));
return MAPPER.writeValueAsString(node);
}
}
The data provider reads the scenario file and the test stays tiny:
@DataProvider(name = "createUserCases")
public Object[][] cases() throws IOException {
List<Map<String, Object>> list = MAPPER.readValue(
new File("src/test/resources/testdata/createUser-cases.json"),
new TypeReference<List<Map<String, Object>>>() {});
return list.stream().map(c -> new Object[]{ c }).toArray(Object[][]::new);
}
@Test(dataProvider = "createUserCases")
@SuppressWarnings("unchecked")
public void createUser(Map<String, Object> c) throws IOException {
String body = PayloadFactory.build("createUser.json",
(Map<String, Object>) c.get("overrides"));
userClient.create(body)
.then().statusCode((Integer) c.get("status"));
}
Adding a new scenario, such as an email that is too long, is now a one-line change in the JSON file. Because overrides can set a field to null, missing-value cases are as easy to write as wrong-value cases.
Design Choices That Pay Off Later
- Put the case name in the report. Implement TestNG's
ITestinterface or log the case field, so a failure reads "missing email" rather than "createUser[1]". - Keep environments in config files selected by a system property, such as
-Denv=qa, so the same data runs anywhere. - Generate unique values like emails with a timestamp or random suffix at runtime, so reruns do not collide with existing records.
- Validate response schemas as well as status codes, using a JSON schema file per endpoint.
- Clean up created data in an
@AfterMethodor through a cleanup endpoint, so test environments do not fill with junk.
Signs the Framework Needs Attention
Watch for duplicated templates that differ by one field, scenario files nobody can read without opening the code, tests that pass only in a fixed order, and secrets stored in payload files. Each one means the separation between data, configuration and code has started to slip and should be tidied before more cases are added.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.