Java Testing Philosophy: Unit, Integration, Contract, E2E

Hot take: Most Java applications are undertested, not because developers are lazy, but because nobody taught us HOW to test well. After 9 years, here's my testing philosophy: The Testing Trophy (not pyramid) for Spring Boot apps: 1. Unit Tests (fast, many)   Test business logic in isolation   Mock everything external   Should run in milliseconds 2. Integration Tests (medium, important)   Test your Spring context loads correctly   Test your repository queries actually work   Use @SpringBootTest + Testcontainers 3. Contract Tests (often forgotten)   Test your API contracts with consumer-driven tests   Catch breaking changes before they reach production 4. E2E Tests (few, focused)   Test the most critical user journeys only   They're slow and brittle - use sparingly My rule: If I'm afraid to refactor it, it needs a test. Bonus tip: Testcontainers changed how I write integration tests forever. Real Postgres/Redis/Kafka in your tests. No more mocking infrastructure. What's your approach to testing in Java? #Java #SpringBoot #Testing #TDD #SoftwareEngineering #Testcontainers

Well depends on what your app is actually doing, but if it is crud heavy I would skip unit tests. Integration tests cover that automatically. If we have a lot of functionality that with complex algorithms then I would consider unit testing. What I see implemented in many companies too much unit tests with zero Integration tests and that alone is not a good enough filter to catch bugs early and they are not good warranty for refactoring.

Like
Reply

To view or add a comment, sign in

Explore content categories