📦 POJO & Jackson — Type-Safe API Testing
POJO & Jackson — Type-Safe API Testing: Writing raw String JSON is like a notary copying a contract by hand, letter by letter: if a character is wrong the paper still accepts the
Writing raw String JSON is like a notary copying a contract by hand, letter by letter: if a character is wrong the paper still accepts the signature, but the mistake surfaces only in court. Using a POJO is like turning that contract text into a software template: leave the "name" field empty and you get a red compile-time warning before the document ever reaches the signing stage. REST Assured works fine with `body(jsonString)` — so why switch to POJOs? Because a typo like `"email":"test@test.com "` (trailing space) makes the test pass sometimes and fail sometimes — a flaky test whose root cause takes hours to find. In Java, Jackson and Lombok solve this with `@Data @Builder`: when you write `UserRequest.builder().email(email).build()`, the IDE autocompletes the email field, and leaving it out gives a compile error. The real QA value: a POJO's `@JsonProperty("email_address")` annotation maps the API's snake_case field to your Java camelCase variable — do that mapping in a String and you get a NullPointerException; in a POJO it resolves automatically and the parsed response value flows directly into your assertion.
What is a POJO? — Java Analogy
When you store key/value pairs in a Java HashMap, you can make typos and there is no type safety. A POJO (Plain Old Java Object) defines field names and types as a class — it is like making a HashMap type-safe.
❌ String JSON (Fragile)
The IDE cannot catch this typo.
IDE flags it instantly. Refactoring is automatic.
Request POJO — Clean Code with Lombok
Micro Lab: REST Assured assertion writing
Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.
How Does @JsonInclude(NON_NULL) Hide Unfilled Fields?
@Data + @Builder Lombok annotations…
`@Data` + `@Builder` Lombok annotations AUTO-generate getter/setter/constructor code at COMPILE time — you don't WRITE this code, it's not VISIBLE in the IDE but it EXISTS in the compiled class.
Thanks to @JsonInclude(NON_NULL)…
Thanks to `@JsonInclude(NON_NULL)`, `.name("Alice").job("QA").build()` does NOT write `firstName`/`lastName` to JSON AT ALL — null fields are SILENTLY skipped, no `"firstName":null` noise is PRODUCED.