unit-test-controller-layer_skill

This skill helps you unit test REST controllers with MockMvc by isolating the web layer and validating requests, responses, and errors.
  • Python

99

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill giuseppe-trisciuoglio/developer-kit --skill unit-test-controller-layer

  • SKILL.md11.6 KB

Overview

This skill provides practical patterns for unit testing Spring REST controllers using MockMvc and @WebMvcTest. It focuses on isolating the web layer by mocking service dependencies to validate request/response mapping, validation, headers, content negotiation, and exception handling. Use it to build fast, focused controller tests without running a full integration context.

How this skill works

The patterns show a standalone MockMvc setup (MockMvcBuilders.standaloneSetup) or lightweight @WebMvcTest configuration, with all injected services mocked via Mockito. Tests perform HTTP requests (GET/POST/PUT/PATCH/DELETE) against controller endpoints and assert status codes, response bodies using JsonPath, headers, and validation error payloads. Error scenarios are simulated by configuring mocked services to throw exceptions.

When to use it

  • Verifying request/response mapping and JSON output for a controller
  • Testing request parameter binding, path variables, and query filters
  • Validating input constraints and asserting 400 validation responses
  • Confirming exception handling and mapping to appropriate HTTP statuses
  • Testing content negotiation and response/request headers in isolation

Best practices

  • Use standalone MockMvc for single-controller tests to keep tests fast
  • Mock all service and repository dependencies; controllers should only handle HTTP concerns
  • Test both happy paths and error paths (404, 400, 401/403, 500) for each endpoint
  • Assert JSON responses with JsonPath and verify response headers explicitly
  • Keep each test focused on one behavior and verify mock interactions

Example use cases

  • Unit test GET /api/users to assert JSON array structure and that service.getAllUsers() is called
  • POST /api/users validation test that sends invalid JSON and expects 400 with errors array
  • Simulate service throwing UserNotFoundException and assert controller returns 404 with error message
  • PUT /api/users/{id} test that verifies update returns 200 and the updated fields
  • Test Authorization header handling by asserting endpoints return 401 without a token and 200 with a valid header

FAQ

Mock the service layer for controller unit tests. That keeps tests focused on HTTP mapping and behavior. Business logic belongs in service-layer tests.

When should I use standalone MockMvc vs @WebMvcTest?

Use standalone MockMvc for single-controller, fast unit tests. Use @WebMvcTest when you need Spring MVC configuration or controller advice applied.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational
unit-test-controller-layer skill by giuseppe-trisciuoglio/developer-kit | VeilStrat