Skip to content
CodeKruPro

Automate a negative test: handle validation errors

Automation taskMediumJava onlyAsked in: Amazon, Swiggy

"Write a negative test for this endpoint" is the natural follow-up to the POST task. Assert both the status and the error message; checking only one of them is a common weak answer.

Handle Validation Errors

Handle Validation Errors

When validation fails, the API returns 400 Bad Request with {"valid": false, "errors": [...]}.

Sending an empty name and no email: {"name": ""} triggers both validation errors.

Task:

  1. POST to /validate/json with body {"name": ""}
  2. Print the status, valid flag, error count, and each error

Expected output:

Status: 400
Valid: false
Error count: 2
- Field 'name' is required and must not be empty
- Field 'email' is required and must not be empty