Your first test checks only the status. A shop that answers 200 with the wrong list still passes it. To test the list itself, read the body: client.getForEntity("/products", List.class) turns the JSON text into a Java List, and response.getBody() gives it to you. For GET /products that is a list with one Map per product.
Once the body is Java data, you check it with tools you already know: size(), list indexes and map keys. Here is a check on another route, product 2, the Notebook:
ResponseEntity<Map> response = client.getForEntity("/products/2", Map.class);
Map product = response.getBody();
assertEquals(7, product.get("price"));
The check runs your tests on the shop and on two broken copies: one leaves out the last product, the other lists the cheapest product first. Both still answer 200, so only a test of the body catches them. A failed assertEquals prints what you expected first, then what it got. The output must be:
Your tests on the shop:
testProductsAnswer200 ... ok
testProductsList ... ok
2 tests: OK
Your tests on a copy that leaves out the last product:
testProductsAnswer200 ... ok
testProductsList ... FAIL
AssertionError: expected: <4> but was: <3>
2 tests: 1 failed, so they caught this bug
Your tests on a copy that lists the cheapest product first:
testProductsAnswer200 ... ok
testProductsList ... FAIL
AssertionError: expected: <Mug> but was: <Pen>
2 tests: 1 failed, so they caught this bug
Your task: below the first test, write testProductsList: GET /products reading the body as a List, keep response.getBody() in products, and check that there are 4 products and that the first one's "name" is "Mug".
Assignment
Follow the Coding Tutorial to write a second test that checks the list in the answer.
Hint
Lists count from 0, so the first product is products.get(0), a Map. Give assertEquals what you expect first. A test stops at its first failed check, so each broken copy shows one message.
Free plan: 30 tutor messages. PRO is unlimited.
