Test cases for REST APIs

I like to create Request and Response classes for each API call, and importantly if at all possible I’ll try to make sure that the request class can be instantiated with minimal input in (using random VALID data for what can be randomised if not specified). Then create fluent methods to override the request for negative testing. Then a test looks something like:

// java
NewRestItemRequest request = new NewRestItemRequest(args);
NewRestItemResponse response = request.withName(“AnInvalidName”).getResponse();

assert response.getSuccess() == false : “Invalid name was allowed”;

From this point I can modify the request base class to do things like adding & removing JSON properties so that I can very quickly create many test cases in very few lines of code.

Then assuming I’ve also coded for the happy path scenario (in this example to create an item), I have 2 lines of code that I can use elsewhere in other tests that require an item to be present (without having to create that item with the UI or DB)