Implementing Service Testing
1. Writing Unit Tests
| Aspect | Detail |
|---|---|
| Scope | One class / function |
| Speed | Milliseconds; no I/O |
| Tools | JUnit 5, AssertJ, Mockito |
| Coverage target | Domain > 80%; UI/glue lower |
2. Writing Integration Tests
| Element | Detail |
|---|---|
| Scope | Service + real DB / broker |
| Tools | Testcontainers, Spring Boot Test |
| Isolation | Per-test schema or rollback |
3. Implementing Contract Testing
| Concept | Detail |
|---|---|
| Consumer-driven | Consumer publishes pact |
| Provider verifies | Replays pact in CI |
| Tools | Pact, Spring Cloud Contract |
| Win | Detects breaking changes pre-merge |
4. Using Component Testing
| Aspect | Detail |
|---|---|
| Scope | Whole service in isolation, deps stubbed |
| Boundary | Test through HTTP/gRPC API |
| Tools | WireMock, MockServer, Testcontainers |
5. Implementing End-to-End Testing
| Aspect | Detail |
|---|---|
| Scope | Full stack, real services |
| Cost | Slow, brittle; keep few |
| Pyramid | Many unit, fewer integration, fewest E2E |
| Tools | Playwright, Cypress, Karate |
6. Testing Asynchronous Communication
| Practice | Detail |
|---|---|
| Embedded broker | Testcontainers Kafka/Rabbit |
| Awaitility | Poll until expectations met |
| Schema validation | Confluent test utils |
7. Implementing Chaos Testing
| Tool | Capability |
|---|---|
| Chaos Mesh / Litmus | K8s fault injection |
| Gremlin | Managed chaos |
| Toxiproxy | Network latency / drops |
| Practice | Game days; start small |
8. Testing Security
| Type | Tool |
|---|---|
| SAST | SonarQube, Semgrep, CodeQL |
| DAST | OWASP ZAP, Burp |
| SCA | Snyk, Dependabot, Trivy |
| Secrets | gitleaks, TruffleHog |
| Container | Trivy, Grype |
9. Implementing Performance Testing
| Type | Detail |
|---|---|
| Load | Sustained expected traffic |
| Stress | Beyond limits to find break |
| Spike | Sudden surge |
| Soak | Hours/days for leaks |
| Tools | k6, Gatling, JMeter, Locust |
10. Using Test Doubles
| Type | Use |
|---|---|
| Dummy | Filler, never used |
| Stub | Canned responses |
| Spy | Records calls |
| Mock | Pre-programmed expectations |
| Fake | Working impl (in-mem DB) |
11. Testing Resilience Patterns
| Pattern | Test |
|---|---|
| Retry | Inject transient failure; assert N attempts |
| Circuit breaker | Force open; assert short-circuit |
| Timeout | Slow stub; assert deadline |
| Bulkhead | Saturate one pool; others survive |
12. Using Testing Tools
| Tool | Use |
|---|---|
| JUnit 5 + AssertJ | Java unit |
| Testcontainers | Real deps in Docker |
| WireMock / MockServer | HTTP stubs |
| Pact | Contracts |
| k6 / Gatling | Load |
| Playwright | E2E browser |