Skip to main content

REST API Test Automation with SHAFT

SHAFT wraps REST Assured to provide a fluent API for HTTP request building, response validation, and JSON/XML extraction.

Your First API Test

public class ApiTest {
private SHAFT.API api;

@Test
public void getUserById() {
api.get("https://jsonplaceholder.typicode.com/users/1")
.perform();

api.verifyThatResponse()
.statusCode().isEqualTo(200)
.perform();

api.assertThatResponse()
.extractedJsonValue("name")
.isEqualTo("Leanne Graham")
.perform();
}

@BeforeMethod
public void setUp() {
api = new SHAFT.API("https://jsonplaceholder.typicode.com");
}
}

Request Building

// GET with query parameters
api.get("/users")
.setParameter("page", "1")
.setParameter("limit", "10")
.perform();

// POST with JSON body
api.post("/users")
.setContentType("application/json")
.setRequestBody("""
{"name": "Jane", "email": "jane@example.com"}
""")
.perform();

// PUT, PATCH, DELETE
api.put("/users/1").setRequestBody(body).perform();
api.patch("/users/1").setRequestBody(body).perform();
api.delete("/users/1").perform();

Response Validation

// Status code
api.verifyThatResponse()
.statusCode().isEqualTo(200)
.perform();

// Response body content
api.verifyThatResponse()
.body().contains("Leanne Graham")
.perform();

// Response time
api.verifyThatResponse()
.time().isLessThan(2000)
.perform();

// JSON path extraction
String email = api.getResponseJSONValue("email");
String city = api.getResponseJSONValue("address.city");

Authentication

// Basic auth
api.get("/secure/resource")
.setAuthentication("username", "password", "basic")
.perform();

// Bearer token
api.get("/secure/resource")
.setRequestHeader("Authorization", "Bearer " + token)
.perform();

Properties for API Testing

# Timeout (milliseconds)
apiSocketTimeout=30000

# Log all API responses in reports
alwaysLogAPIResponse=true

Learn More