unit-test-controller-layer

Installation
Summary

Unit testing REST controllers in isolation with MockMvc and mocked service dependencies.

  • Covers testing all HTTP methods (GET, POST, PUT, PATCH, DELETE) with status code and response body validation using JsonPath assertions
  • Includes patterns for request parameter binding, validation errors, exception handling, and content negotiation across different Accept and Content-Type headers
  • Uses standalone MockMvc setup with Mockito to mock service layer dependencies, keeping tests focused on HTTP handling without integration test overhead
  • Provides examples for testing request/response headers, path variables, query parameters, and error scenarios (404, 400, 401, 500)
SKILL.md

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

  1. Setup standalone MockMvc: MockMvcBuilders.standaloneSetup(controller) for isolated testing
  2. Mock service dependencies: Use @Mock for all services, @InjectMocks for the controller
  3. Test HTTP methods: GET, POST, PUT, PATCH, DELETE with correct status codes
  4. Validate responses: JsonPath assertions for JSON, content matchers for body
  5. Test validation: Send invalid input, verify 400 status with error details
  6. Test errors: Verify 404, 400, 401, 403, 500 for appropriate conditions
  7. Validate headers: Both request (Authorization) and response headers
Related skills

More from giuseppe-trisciuoglio/developer-kit

Installs
897
GitHub Stars
246
First Seen
Feb 3, 2026