About this lesson
REST Assured path parameter: list products by category with getList()
Learn REST AssuredAdvancedAdvanced Topics
How it works
RESTful URLs often put the thing you are asking about into the path itself: /categories/{category}/products reads as the products of one category. That is a path parameter, different from a query parameter that filters a search after a question mark. In REST Assured you can write the finished path directly, or keep the template and fill it with pathParam(), giving the placeholder name and the value; both reach the same endpoint.
testkru matches the category case-insensitively, so a capitalised category name still worked, and an unknown category returns 404 with an error that lists the valid ones: electronics, books and furniture. A known category returns a JSON array of full product objects, each with its id, name, price, category object and specs.
Collecting one field from every element is a JsonPath job. On a root array, getList("name") returns a Java List with one entry per product, and the overload that also takes String.class returns a typed List<String>. Its size() is the count, and printing the list uses the list's own toString(), which wraps the entries in square brackets separated by commas.
About the assignment
Request the electronics products, collect their names into a list, and print the list and how many entries it has.
What you will do
- Send a GET for
/categories/electronics/productsand keep theResponse. - Extract every product name with
jsonPath().getList()into aList<String>, using theListimport the starter already has. - Print the list after the
Electronics products:label. - Print the list's
size()after theCount:label.
Common mistakes
Using getString() for the names
getString("name")on this array returns one String that happens to look like a list. It prints the same, but a String has nosize(), and itslength()counts characters, not products.Not handling a misspelt category
A typo in the path gets a 404 error body with no product array.
getList()then returnsnull, and callingsize()on it throws aNullPointerException. Check the status first.Hard-coding the count
Printing a fixed number passes today and lies tomorrow when a product is added. Derive the count from the list the server returned.
In a real test suite
Path parameters pair naturally with JUnit 5 parameterised tests: one test method, a @ValueSource of category names, and pathParam() filling the template for each run. Each case asserts the status, that the list is not empty and that every product carries the requested category.
Related lessons
- Filter by CategoryFilters by category with a query parameter on the search endpoint instead.
- Extract from Books with TagsIntroduces reading a JSON array into a Java
ListwithgetList(). - Extract List of ValuesCollects one field across every object in an array.
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;
import java.util.List;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: GET /categories/electronics/products
// TODO: Extract the list of product names
// TODO: Print the names and the count
}
}