Skip to content
CodeKruPro

Products by Category

62/62

Products by Category

The /categories/{category}/products endpoint filters products by category. This is a path parameter pattern common in RESTful APIs.

Available categories: electronics, books, furniture

Task: Fetch electronics products and print the results.

Expected output:

Electronics products: [Laptop Pro, Wireless Mouse, Mechanical Keyboard]
Count: 3

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

  1. Send a GET for /categories/electronics/products and keep the Response.
  2. Extract every product name with jsonPath().getList() into a List<String>, using the List import the starter already has.
  3. Print the list after the Electronics products: label.
  4. Print the list's size() after the Count: 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 no size(), and its length() 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 returns null, and calling size() on it throws a NullPointerException. 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.

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.

Main.java
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
    }
}