Demo: test-driving a controller with @WebMvcTest
On This Lesson 3 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
write tests first for GET /products/{id}; prepares JNR-016-LAB1.
Draft outline details
Step by step
- Write a failing test for the success case; mock the service; assert status and JSON fields.
- Make it pass (the controller from JNR-013).
- Add a validation test:
POST /productswith a blank name returns 400 and a field error. - Planned mistake: assert only
status().isOk(), and a bug in the JSON passes. Fix: addjsonPathassertions. - Add the not-found test and check the Problem Details body.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.