Deterministic Spring Boot Testing
Testing Spring Boot microservices often falls into two traps:
- Unit Tests are too fake: Mocking every bean (
@MockBean) means you aren't testing real logic. - Integration Tests are too slow: Spinning up full contexts and DBs for every test takes minutes.
BitDive offers a Third Way: Deterministic Replay.
The Problem with @MockBean
When you use @MockBean, you are writing a script of what you think your code does, not what it actually does.
// Traditional Mockito - Fragile & Manual
when(repository.findById("123")).thenReturn(Optional.of(user));
If the database schema changes, this test passes, but production fails. This is the Reality Gap.
The BitDive Approach: Record & Replay
Instead of manually writing mocks, BitDive records the actual behavior of your Spring Boot application in production or staging.
- Record: The BitDive Agent captures the input (HTTP Request) and all downstream interactions (SQL, Kafka, External APIs).
- Virtualize: BitDive creates a "Virtualization Layer" around your Service.
- Replay: The test runs your actual Service logic, but "replays" the recorded data for dependencies.
Key Benefits for Spring Developers
1. No Boilerplate
Stop writing hundreds of lines of when(...).thenReturn(...). BitDive creates the test context automatically.
2. Fast Context Startup
BitDive can start a "slice" of your Spring Context in milliseconds, bypassing the heavy overhead of full @SpringBootTest.
3. Production Realism
Your tests are based on real data, not synthetic data. This catches edge cases (nulls, weird encodings) that manual mocks miss.
Example: Testing a UserService
class UserServiceReplayTest extends ReplayTestBase {
@Override
protected List<ReplayTestConfiguration> getTestConfigurations() {
// Load the recording of the "User Creation" scenario
return ReplayTestUtils.fromRestApiWithJsonContentConfigFile(
Arrays.asList("user-creation-flow-id")
);
}
// No manual test method needed - the harness runs the replay automatically
}
Spring Slice Tests and BitDive
Spring Boot provides slice test annotations that start only a targeted piece of the context:
| Annotation | What It Tests | Loads |
|---|---|---|
@WebMvcTest | Controllers, filters, serialization | Web layer only |
@DataJpaTest | Repositories, JPA mappings, queries | JPA + in-memory DB |
@JsonTest | JSON serialization/deserialization | Jackson ObjectMapper |
These slices are faster than full @SpringBootTest and useful for verifying specific seams. However, they have a key limitation: the test data is still synthetic. You write the test inputs manually, which means you are testing what you think happens, not what actually happens.
BitDive's Deterministic Replay complements slices by providing production-grounded data. Instead of manually creating a UserEntity for your @DataJpaTest, BitDive replays the exact ResultSet that your production database returned. This combines the speed of a slice with the realism of production data.
Next Steps
View the Component Testing Guide to see full examples.