Unit Testing
What Is Unit Testing?
Testing individual units (methods/classes) in isolation.
JUnit 5
@SpringBootTest
class ProductServiceTest {
@MockBean
private ProductRepository repository;
@Autowired
private ProductService service;
@Test
void shouldReturnProductWhenExists() {
Product product = new Product(1L, "Laptop", new BigDecimal("999.99"));
when(repository.findById(1L)).thenReturn(Optional.of(product));
Product result = service.getById(1L);
assertThat(result.getName()).isEqualTo("Laptop");
verify(repository).findById(1L);
}
}
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 Unit 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 Unit Testing in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Unit Testing implementation
// Key aspects: validation, error handling, logging, testing
public class UnitTesting {
// Production-ready implementation
}Identify and handle edge cases for Unit 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 Unit 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. Unit testing tests?
2. @MockBean does?
3. What is the primary purpose of Unit Testing?
4. What is a common mistake when implementing Unit Testing?
Flashcards
Question
Unit testing tests?
Click to reveal answer
Answer
Individual units in isolation
Question
@MockBean?
Click to reveal answer
Answer
Creates mock, replaces real bean in context
Question
What is Unit Testing?
Click to reveal answer
Answer
Unit Testing is a key concept in backend development.
Question
When to use Unit Testing?
Click to reveal answer
Answer
Use Unit Testing when building production systems that require reliability, scalability, and maintainability.
Question
Unit 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.Unit testing: test in isolation
- 2.JUnit 5 for test framework
- 3.MockBean for mocking Spring beans
- 4.AssertJ/Hamcrest for assertions
Interview Tips
- •Write unit tests
- •Know testing frameworks
Cheat Sheet
Unit Testing
- Test in isolation
- JUnit 5: @Test, @BeforeEach, @AfterEach
- @MockBean: mock in Spring context
- Assertions: assertThat, assertEquals