Use the smallest test that proves the behavior
Plain unit tests verify isolated business decisions quickly. WebMvcTest covers a web slice for routing, validation, serialization and configured security, while SpringBootTest covers wider context wiring. Realistic database tests are needed for query behavior, constraints and transactions; a mocked repository cannot establish them. Spring Boot 3.x and 4.x differ in some test modules/packages, so confirm imports for the selected release.
Retry only inside a defined budget
A caller needs an overall deadline that includes queue wait, execution and dependency calls. Bound each attempt and retry only suitable transient failures, using backoff and jitter. A timed-out payment may have committed remotely, so idempotency and outcome reconciliation matter more than blindly calling again. Retries at multiple layers can multiply load. A circuit breaker does not replace timeouts, concurrency limits or a meaningful fallback.
Observe and drain safely
Track latency distributions, errors, saturation and dependency timing with low-cardinality dimensions. Secure management endpoints and avoid exposing secrets in logs. Readiness tells traffic routing whether the instance can serve; liveness tells a supervisor whether restarting is appropriate. Making a downstream outage fail liveness can create restart storms. During shutdown stop admission, drain for a bounded period and decide how interrupted work is recovered.
Worked Java example
Illustrative snippet: imports, surrounding types or collaborators may be omitted. Read the assumptions before adapting it.
// Plain unit-test shape; no Spring context is needed.
OrderRepository fake = new InMemoryOrderRepository();
OrderService service = new OrderService(fake);
// Arrange a known order, invoke the use case, assert its result.
// Use a database integration test for SQL/transaction behavior.Walkthrough
This sketch demonstrates test scope rather than a complete repository implementation. A fake makes domain decisions reproducible without a server. It must not be mistaken for evidence about real database isolation or an HTTP security filter chain. Separate these questions into tests that actually execute the relevant boundary.
Practice it yourself
A dependency is down and requests are retrying for thirty seconds. The public request budget is two seconds. Propose a correction.
Check your reasoning
Set attempt timeouts and retry/backoff limits that fit the remaining two-second deadline, stop admitting excessive concurrent work, and choose a contract-appropriate error or fallback. Preserve idempotency and record uncertain outcomes rather than reporting a false success.
Recall checklist
- Explain the main invariant or contract without looking.
- Trace the example and test an edge case.
- State time, space, input variables and assumptions where relevant.
- Explain one alternative and when you would choose it.