Repository Testing
Using Testcontainers
@SpringBootTest
class ProductRepositoryTest {
@Container
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15")
.withDatabaseName("testdb");
@DynamicPropertySource
static void configure(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}
@Autowired
private ProductRepository repository;
@Test
void shouldFindByCategory() {
repository.save(new Product("Laptop", "electronics"));
List<Product> result = repository.findByCategory("electronics");
assertThat(result).hasSize(1);
}
}
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 Repository 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 Repository Testing in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Repository Testing implementation
// Key aspects: validation, error handling, logging, testing
public class RepositoryTesting {
// Production-ready implementation
}Identify and handle edge cases for Repository 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 Repository 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. Testcontainers provides?
2. @DynamicPropertySource does?
3. What is the primary purpose of Repository Testing?
4. What is a common mistake when implementing Repository Testing?
Flashcards
Question
Testcontainers?
Click to reveal answer
Answer
Real databases in Docker for testing
Question
@DynamicPropertySource?
Click to reveal answer
Answer
Overrides Spring properties at runtime
Question
What is Repository Testing?
Click to reveal answer
Answer
Repository Testing is a key concept in backend development.
Question
When to use Repository Testing?
Click to reveal answer
Answer
Use Repository Testing when building production systems that require reliability, scalability, and maintainability.
Question
Repository 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.Testcontainers: real DB in Docker
- 2.@DynamicPropertySource for config
- 3.Test actual queries, not mocks
- 4.Close to production environment
Interview Tips
- •Test repository layer
- •Use Testcontainers
Cheat Sheet
Repository Testing
- Testcontainers: real DB in Docker
- @DynamicPropertySource: runtime config
- Test actual queries
- Close to production