Back to Skills

unit-test-controller-layer

Provides patterns for unit testing REST controllers using MockMvc and @WebMvcTest. Generates controller tests that validates request/response mapping, validation, exception handling, and HTTP status codes. Use when testing web layer endpoints in isolation for API endpoint testing, Spring MVC tests, mock HTTP requests, or controller layer unit tests.

314stars37forksUpdated 7/31/2026

Security Assessment

Safe(95/100)
Security Score95/100

About unit-test-controller-layer

Unit Test Controller Layer is a Java testing-guidance skill that provides patterns for unit testing Spring REST controllers in isolation using MockMvc and @WebMvcTest. It solves the problem of verifying the web layer — request and response mapping, input validation, HTTP status codes, headers, content negotiation, and exception handling — without spinning up the full application context or hitting real services, keeping controller tests fast and focused.

The skill lays out a clear workflow: set up standalone MockMvc around the controller, mock service dependencies with Mockito (@Mock and @InjectMocks), exercise every HTTP method (GET, POST, PUT, PATCH, DELETE) with the correct expected status codes, and assert response bodies with JsonPath. It shows how to test validation failures returning 400 with error details, error conditions returning 404/401/403/500, request and response headers including Authorization, and content negotiation. Reference documents add advanced patterns — multiple status-code scenarios, role-based access and authentication testing, file upload testing with MockMultipartFile, paginated responses, custom exception handlers, and async endpoints — along with common pitfalls and troubleshooting guidance, plus a validation loop that uses .andDo(print()) to compare actual versus expected output.

It targets Java and Spring Boot developers writing web-layer tests. Use cases include verifying REST endpoints in isolation, asserting validation and error-handling behavior, testing secured endpoints and file uploads, and generating maintainable controller test suites. It is read/write test-authoring guidance using standard testing tools, with no risky runtime behavior.

FAQ

What testing tools does this skill use?

Spring's MockMvc (standalone setup and @WebMvcTest) together with Mockito for mocking service dependencies and JsonPath for asserting JSON responses.

How are dependencies isolated in these tests?

Services are mocked with @Mock and injected into the controller with @InjectMocks, and MockMvcBuilders.standaloneSetup wires up the controller so no full Spring context or real service is needed.

Which scenarios does it cover?

All HTTP methods with correct status codes, validation failures returning 400, error conditions (404, 401, 403, 500), request and response headers, content negotiation, file uploads, pagination, custom exception handlers, and async endpoints.

How do you debug a failing assertion?

The validation workflow recommends adding .andDo(print()) to dump the actual request/response, comparing it against expected output, and then fixing the assertion.

What dependency is required?

The spring-boot-starter-test dependency (test scope), which brings in MockMvc, Mockito, JsonPath, and the JUnit tooling used throughout the examples.

All Files

4 files
references/advanced.md3.6 KB
View
SKILL.md7.4 KB
View
references/common-pitfalls.md4.2 KB
View
references/troubleshooting.md3.0 KB
View

Install unit-test-controller-layer

Download and extract the skill files to your .claude/skills/ directory.

Quick Setup:

  1. Copy the skill folder to .claude/skills/
  2. Claude will automatically detect and use the skill