Java Integration Testing
Comprehensive integration testing patterns for Java/Spring Boot services using Spock Framework.
When to use this skill
- Setting up integration tests for a new service
- Migrating from unit tests to proper integration tests
- Choosing mock strategy: GrpcMock/WireMock (preferred) vs @MockitoBean/@SpringBean (ProtoShims only)
- Configuring Testcontainers (PostgreSQL, Valkey/Redis)
- Testing gRPC handlers or external gRPC clients
- Testing REST controllers with MockMvc
- Testing Kafka consumers with EmbeddedKafka
Skill Contents
Sections
Available Resources
π references/ - Detailed documentation
- base spec patterns
- container cleanup
- container reuse
- grpc handler testing
- grpcmock resilience
- grpcmock setup
- grpcmock stub helpers
- kafka testing
- migration workflow
- rest controller testing
- testcontainers setup
Quick Decision Tree
What are you testing?
β
βββΊ Your own gRPC handlers?
β βββΊ Use [grpc-handler-testing.md](references/grpc-handler-testing.md)
β (In-process transport, GrpcClientTestConfig)
β
βββΊ External gRPC clients (retries/timeouts)?
β βββΊ Use [grpcmock-setup.md](references/grpcmock-setup.md)
β (WireMock-like stubbing for gRPC)
β
βββΊ REST controllers?
β βββΊ Use [rest-controller-testing.md](references/rest-controller-testing.md)
β (MockMvc, @AutoConfigureMockMvc)
β
βββΊ Kafka consumers?
β βββΊ Use [kafka-testing.md](references/kafka-testing.md)
β (@EmbeddedKafka, KafkaTestUtils)
β
βββΊ Setting up BaseIntegrationSpec?
β βββΊ Use [base-spec-patterns.md](references/base-spec-patterns.md)
β (Mock framework choice, reset strategies)
β
βββΊ Configuring containers?
βββΊ Use [testcontainers-setup.md](references/testcontainers-setup.md)
(PostgreSQL jdbc:tc, Valkey)
Core Principles
The Golden Rules
| Rule | Why |
|------|-----|
| Real domain services | Tests should exercise actual business logic |
| Real repositories | Use Testcontainers for DB/cache |
| Prefer GrpcMock/WireMock for external services | Tests real client wrappers, resilience, error codes |
| Bean mocking only for ProtoShims/custom RPCs | Use @MockitoBean/@SpringBean only when GrpcMock/WireMock is not feasible |
| Reset mocks in setup() | Prevent test interference (@MockitoBean only) |
| Clean data in cleanup() | Ensure test isolation |
| No @DirtiesContext | Use proper cleanup instead |
| No @Transactional on tests | Let transactions commit to test real behavior |
| Static containers | Share across test classes for speed |
| Centralize config in BaseSpec | All test beans and mocks in base class |
External Dependency Mocking Hierarchy
Always prefer the highest-fidelity mock available:
PREFER (most realistic) AVOID (least realistic)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
GrpcMock WireMock @MockitoBean/@SpringBean
(gRPC services) (REST services) (ONLY for ProtoShims/custom RPCs)
| External Dependency Type | Mock Strategy | Why | |--------------------------|---------------|-----| | Standard gRPC service | GrpcMock (always) | Tests real client wrapper, retry logic, error codes, timeouts | | Standard REST service | WireMock (always) | Tests real HTTP client, error handling, serialization | | ProtoShim / custom RPC (e.g., UserModel, TradeModel) | @MockitoBean/@SpringBean | Custom protocols not directly testable via GrpcMock | | Kafka producer | @MockitoBean/@SpringBean | No network mock available |
Why GrpcMock/WireMock over bean mocking:
- Tests the real client wrapper code (serialization, error mapping, retries)
- Catches bugs in client configuration and resilience patterns
- More realistic - simulates actual network behavior
- Single GrpcMock server handles ALL external gRPC services
What to Mock vs Use Real
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Test JVM Process β
β β
β βββββββββββββββββββ βββββββββββββββββββββββββββββββββββββββ β
β β Test Code β β Spring Boot Context β β
β β β β β β
β β Direct API call β βββΊ β Handler / Controller β β
β β (stub or HTTP) β β β β β
β β β β βΌ β β
β β β β βββββββββββββββββββββββ β β
β β β β β Domain Service β β β
β β β β β (REAL impl) β β β
β β β β β β β β β
β β β β β ββββββ΄βββββ β β β
β β β β β βΌ βΌ β β β
β β β β β Repository Cache β β β
β β β β β (REAL) (REAL) β β β
β β β β βββββββββββββββββββββββ β β
β β β β β β
β β GrpcMock/ β β gRPC Client βββββββΊ GrpcMock β β
β β WireMock β β (REAL wrapper) Server β β
β β (preferred) β β β β
β β β β REST Client βββββββΊ WireMock β β
β β @SpringBean β β (REAL wrapper) Server β β
β β (ProtoShims β β β β
β β only) β β ProtoShim βββββ @SpringBean MOCK β β
β β β β (custom RPC) β β
β βββββββββββββββββββ βββββββββββββββββββββββββββββββββββββββ β
β β
β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ β
β β PostgreSQL β β Valkey β β Kafka β β
β β Testcontainer β β Testcontainer β β EmbeddedKafka β β
β β (if needed) β β (if needed) β β (if needed) β β
β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Mock Framework Choice
When bean mocking IS needed (ProtoShims/custom RPCs only):
| Approach | Annotation | Mock Syntax | Verification | Reset Needed |
|----------|------------|-------------|--------------|--------------|
| Mockito | @MockitoBean | when(mock.method()).thenReturn(value) | verify(mock).method() | Yes |
| Spock | @SpringBean | mock.method() >> value | 1 * mock.method() | No |
WARNING: You cannot mix syntaxes! With @MockitoBean, Spock's >> syntax will NOT work.
See base-spec-patterns.md for detailed examples.
References
| Reference | Description | |-----------|-------------| | base-spec-patterns.md | BaseIntegrationSpec patterns with mock frameworks | | testcontainers-setup.md | Container configuration patterns | | grpc-handler-testing.md | Testing your own gRPC handlers | | grpcmock-setup.md | GrpcMock for external services | | rest-controller-testing.md | REST controller testing | | kafka-testing.md | Kafka consumer testing |
Related Skills
| Skill | Purpose | |-------|---------| | java-testing | General testing guidelines | | java-coverage | JaCoCo coverage setup | | grpc-services-rfc-33 | gRPC service standards |
<!-- AUTO-GENERATED FILE - DO NOT EDIT DIRECTLY --> <!-- Source: bitsoex/ai-code-instructions β java/skills/java-setup-integration-tests/SKILL.md --> <!-- To modify, edit the source file and run the distribution workflow -->