unit-test-controller-layer
Learn to unit test Spring REST controllers using MockMvc with mocked service dependencies. This skill covers testing HTTP methods, validating responses with JsonPath, handling errors, checking headers, and verifying content negotiation—all with focused examples for GET, POST, PUT, and DELETE endpoints.
Unit Test Controller Layer teaches you to test REST controller endpoints in isolation using MockMvc and mocked service dependencies.
AI-generated summary based on this skill's SKILL.md
Install
giuseppe-trisciuoglio/developer-kit/unit-test-controller-layer · repository language: Python
git clone https://github.com/giuseppe-trisciuoglio/developer-kit
cp -r developer-kit/plugins/developer-kit-java/skills/unit-test-controller-layer ~/.claude/skills/unit-test-controller-layernpx skillfed install giuseppe-trisciuoglio/developer-kit/unit-test-controller-layerFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How to unit test REST controllers with MockMvc?
unit-test-controller-layer teaches you to test Spring REST controllers using MockMvc by setting up @WebMvcTest annotations, performing HTTP requests (GET, POST, PUT, DELETE), and asserting responses. You'll mock service dependencies with @MockBean, validate HTTP status codes, check JSON response content with JsonPath, and verify that your controller layer correctly handles requests and delegates to services.
What does unit-test-controller-layer cover for error handling?
unit-test-controller-layer includes testing error handling and validation in the web layer. You'll learn to verify 400 Bad Request responses for validation failures, test controller exception handling, check error message content, and validate that your controllers properly catch and respond to service exceptions and constraint violations.
How do I mock service dependencies in controller tests?
unit-test-controller-layer shows how to mock service dependencies using @MockBean annotations in your test class. You configure mock behavior with Mockito (when/thenReturn), inject mocked services into your controller, and verify that the controller calls the right service methods with correct arguments while testing the controller's response independently.
Can unit-test-controller-layer help test path variables and query params?
Yes. unit-test-controller-layer covers testing request/response mapping in Spring, including path variables and query parameters. You'll learn to construct MockMvc requests with variable paths, query strings, and request parameters, then validate that your controller correctly extracts and uses these values in its responses.
What HTTP methods does unit-test-controller-layer cover?
unit-test-controller-layer provides focused examples for testing GET, POST, PUT, and DELETE endpoints. You'll learn to verify correct HTTP methods are called, validate request bodies for POST/PUT operations, check response status codes for each method, and ensure your controller routes requests to the appropriate service operations.
How does unit-test-controller-layer validate JSON responses?
unit-test-controller-layer teaches MockMvc validation of JSON responses using JsonPath assertions. You'll extract and verify specific fields from response bodies, check nested JSON structures, validate arrays and objects, and confirm that your controller returns correctly formatted and structured JSON data matching your API contract.
SKILL.md
rendered from the published skill — quoted content, verbatim
Unit Testing REST Controllers with MockMvc
Overview
Provides patterns for unit testing @RestController and @Controller classes using MockMvc. Covers request/response handling, HTTP status codes, request parameter binding, validation, content negotiation, response headers, and exception handling with mocked service dependencies.
When to Use
Use for: controller tests, API endpoint testing, Spring MVC tests, mock HTTP requests, unit testing web layer endpoints, verifying REST controllers in isolation.
Instructions
- Setup standalone MockMvc:
MockMvcBuilders.standaloneSetup(controller)for isolated testing - Mock service dependencies: Use
@Mockfor all services,@InjectMocksfor the controller - **Test
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 4 files
plugins/developer-kit-java/skills/unit-test-controller-layer/SKILL.md
plugins/developer-kit-java/skills/unit-test-controller-layer/references/advanced.md
plugins/developer-kit-java/skills/unit-test-controller-layer/references/common-pitfalls.md
plugins/developer-kit-java/skills/unit-test-controller-layer/references/troubleshooting.md