unit-test-controller-layer

Solid

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.

Testing & QA 261 stars 29 forks Updated 1 weeks ago MIT

Install

View on GitHub

Quality Score: 89/100

Stars 20%
81
Recency 20%
90
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# 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 8. **Test content negotiation**: Different Accept and Content-Type headers ### Validation Workflow ``` Run test → If fails: add .andDo(print()) → Check actual vs expected → Fix assertion ``` ## Examples ### Maven / Gradle Dependencies ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scop...

Details

Author
giuseppe-trisciuoglio
Repository
giuseppe-trisciuoglio/developer-kit
Created
7 months ago
Last Updated
1 weeks ago
Language
Python
License
MIT

Integrates with

Similar Skills

Semantically similar based on skill content — not just same category

Testing & QA Solid

unit-test-exception-handler

Provides patterns for unit testing `@ExceptionHandler` and `@ControllerAdvice` in Spring Boot applications. Validates error response formatting, mocks exceptions, verifies HTTP status codes, tests field-level validation errors, and asserts custom error payloads. Use when writing Spring exception handler tests, REST API error tests, or mocking controller advice.

261 Updated 1 weeks ago
giuseppe-trisciuoglio
Testing & QA Solid

unit-test-service-layer

Provides patterns for unit testing service layer with Mockito. Creates isolated tests that mock repository calls, verify method invocations, test exception scenarios, and stub external API responses. Use when testing service behaviors and business logic without database or external services.

261 Updated 1 weeks ago
giuseppe-trisciuoglio
Testing & QA Solid

unit-test-wiremock-rest-api

Provides patterns for unit testing external REST APIs using WireMock. Stubs API responses, verifies request details, simulates failures (timeouts, 4xx/5xx errors), and validates HTTP client behavior without real network calls. Use when testing service integrations with external APIs or mocking HTTP endpoints.

261 Updated 1 weeks ago
giuseppe-trisciuoglio
Testing & QA Solid

spring-boot-test-patterns

Provides comprehensive testing patterns for Spring Boot applications covering unit, integration, slice, and container-based testing with JUnit 5, Mockito, Testcontainers, and performance optimization. Use when writing tests, @Test methods, @MockBean mocks, or implementing test suites for Spring Boot applications.

261 Updated 1 weeks ago
giuseppe-trisciuoglio
Testing & QA Listed

testing-patterns

Backend testing patterns with JUnit 6, Mockito 6, Testcontainers 2.0, Spring Boot slice tests, RestTestClient, and security testing. Use when user mentions testing, coverage, TDD, integration tests, or "write tests for".

0 Updated today
IuliaIvanaPatras