Controller Testing
MockMvc Test
@WebMvcTest(ProductController.class)
class ProductControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ProductService productService;
@Test
void shouldReturnProduct() throws Exception {
when(productService.getById(1L))
.thenReturn(new Product(1L, "Laptop"));
mockMvc.perform(get("/api/products/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Laptop"));
}
}
Testing Strategies
Testing Pyramid
/ E2E \
/----------\
/ Integration \
/----------------\
/ Unit \
/--------------------
Test Types
- Unit: Individual components
- Integration: Component interactions
- E2E: Full user workflows
- Performance: Load/stress testing
Best Practices
- Write tests first (TDD)
- Aim for 80% coverage
- Test edge cases
- Keep tests fast and isolated
Key Points
- Understanding Controller Testing is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for Controller Testing in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Controller Testing implementation
// Key aspects: validation, error handling, logging, testing
public class ControllerTesting {
// Production-ready implementation
}Identify and handle edge cases for Controller Testing. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for Controller Testing. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. @WebMvcTest loads?
2. @WebMvcTest excludes?
3. What is the primary purpose of Controller Testing?
4. What is a common mistake when implementing Controller Testing?
Flashcards
Question
@WebMvcTest loads?
Click to reveal answer
Answer
Only web layer (controllers)
Question
@WebMvcTest excludes?
Click to reveal answer
Answer
Services, repositories
Question
What is Controller Testing?
Click to reveal answer
Answer
Controller Testing is a key concept in backend development.
Question
When to use Controller Testing?
Click to reveal answer
Answer
Use Controller Testing when building production systems that require reliability, scalability, and maintainability.
Question
Controller Testing best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.@WebMvcTest: web layer only
- 2.MockMvc: simulate HTTP requests
- 3.@MockBean for service mocking
- 4.Test status, body, headers
Interview Tips
- •Write controller tests
- •Use @WebMvcTest
Cheat Sheet
Controller Testing
- @WebMvcTest: web layer only
- MockMvc: simulate HTTP
- @MockBean: mock services
- Test: status, body, headers