unit-test-controller-layer

Test REST controllers with MockMvc and Mockito without loading the full Spring context.

322|37|Updated Oct 21, 2025
One-click install
npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill unit-test-controller-layer
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: unit-test-controller-layer
Source: https://github.com/giuseppe-trisciuoglio/developer-kit/tree/main/skills/junit-test/unit-test-controller-layer
Command: npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill unit-test-controller-layer

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps you quickly verify REST controller behavior in isolation from the service layer, enabling fast feedback on request/response mappings, validation, and exception handling without loading the full Spring context.

Core Features & Use Cases

  • Isolated controller tests: Validate HTTP mappings, status codes, and serialization for controllers in isolation.
  • Request validation: Check parameter binding and validation errors without spinning up the full stack.
  • Error handling verification: Confirm proper error responses via @ControllerAdvice and exception mappings.

Quick Start

Create a test class using @WebMvcTest to load only the web layer, mock the service dependencies, and run tests with your chosen build tool.

Frequently Asked Questions about unit-test-controller-layer

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I test REST controllers in isolation without loading the full Spring context?

Use @WebMvcTest to load only the web layer and MockMvc to test controller behavior in isolation. This approach verifies request/response mappings, HTTP status codes, and serialization without spinning up services or database connections, enabling fast feedback on controller logic.

Can I use MockMvc to validate HTTP parameter binding and validation errors?

Yes. MockMvc lets you verify parameter binding, validation constraints, and validation error responses without the full Spring stack. Combine it with Mockito-mocked services to test how controllers handle invalid input and constraint violations.

How do I test error handling and exception responses in REST controllers?

Use @WebMvcTest with @ControllerAdvice to validate exception-to-response mappings. MockMvc verifies that exceptions produce correct HTTP status codes and error response bodies, confirming your error handling strategy works as intended.

What's the fastest way to test GET, POST, PUT, and DELETE endpoints?

Set up a standalone MockMvc instance with @WebMvcTest, mock service dependencies using Mockito, and write parameterized or grouped tests for each HTTP verb. This isolated approach runs orders of magnitude faster than integration tests while covering request/response contracts.

Do I need to verify content negotiation and serialization in controller tests?

Yes. MockMvc lets you validate content-type headers, JSON/XML serialization, and response body structure without hitting a real database or service. This catches mapping mismatches and serialization bugs early.

Why use @WebMvcTest instead of full integration tests for controller logic?

WebMvcTest loads only the web layer, skipping service initialization, database setup, and application startup. Tests run in milliseconds rather than seconds, providing rapid feedback on controller behavior while Mockito handles service dependencies.