Mock Mode Migration Guide
This guide helps you migrate existing Brobot tests to use the new centralized MockModeManager for consistent mock mode configuration.
Overview of Changesโ
The introduction of MockModeManager simplifies mock mode management by providing:
- Single source of truth for mock mode status
- Automatic synchronization across all components
- Simplified test configuration via
BrobotTestBase
Quick Migration Stepsโ
Step 1: Update Test Base Classโ
Ensure your tests extend BrobotTestBase:
Before:
public class MyTest {
@BeforeEach
public void setup() {
System.setProperty("brobot.mock", "true");
}
}
After:
import io.github.jspinak.brobot.test.BrobotTestBase;
public class MyTest extends BrobotTestBase {
// Mock mode is automatically configured!
}
Step 2: Replace Mock Mode Checksโ
Update code that checks mock mode status:
Before:
if (brobotProperties.getCore().isMock()) {
// mock logic
}
// OR
if ("true".equals(System.getProperty("brobot.mock.mode"))) {
// mock logic
}
// OR
if (ExecutionEnvironment.getInstance().isMockMode()) {
// mock logic
}
After:
import io.github.jspinak.brobot.config.mock.MockModeManager;
import io.github.jspinak.brobot.config.core.BrobotProperties;
@Autowired
private BrobotProperties brobotProperties;
if (brobotProperties.getCore().isMock()) {
// mock logic
}
Step 3: Replace Mock Mode Settingsโ
Update code that sets mock mode:
Before:
// Multiple places to set
System.setProperty("brobot.mock", "true");
System.setProperty("brobot.mock.mode", "true");
ExecutionEnvironment env = ExecutionEnvironment.builder()
.mockMode(true)
.build();
ExecutionEnvironment.setInstance(env);
After:
// Single method call
MockModeManager.setMockMode(true);
Common Migration Scenariosโ
Scenario 1: Spring Boot Testsโ
Before:
@SpringBootTest
@TestPropertySource(properties = {
"brobot.core.mock=true",
"brobot.mock=true"
})
public class IntegrationTest {
@BeforeEach
public void setup() {
// Additional manual mock setup
}
}
After:
@SpringBootTest
public class IntegrationTest extends BrobotTestBase {
// Mock mode automatically configured
// Properties are synchronized by MockModeManager
}
Scenario 2: Custom Test Setupโ
Before:
public abstract class CustomTestBase {
@BeforeEach
public void setupMockMode() {
System.setProperty("brobot.mock.mode", "true");
try {
Field mockField = BrobotProperties.class.getField("mock");
mockField.set(null, true);
} catch (Exception e) {
// Handle error
}
}
}
After:
import io.github.jspinak.brobot.test.BrobotTestBase;
public abstract class CustomTestBase extends BrobotTestBase {
// Mock mode handled by parent class
@BeforeEach
@Override
public void setupTest() {
super.setupTest(); // Ensures MockModeManager configuration
// Your custom setup here
}
}
Scenario 3: Conditional Mock Modeโ
Before:
@Test
public void testWithConditionalMock() {
boolean useMock = System.getenv("CI") != null;
if (useMock) {
System.setProperty("brobot.mock", "true");
}
// Test logic
}
After:
@Test
public void testWithConditionalMock() {
boolean useMock = System.getenv("CI") != null;
if (useMock) {
MockModeManager.setMockMode(true);
}
// Test logic
}
Scenario 4: Mode Switching During Testโ
Before:
@Test
public void testModeSwitch() {
// Start with mock
System.setProperty("brobot.mock", "true");
// ... mock tests ...
// Switch to real
System.setProperty("brobot.mock", "false");
// ... real tests ...
// Back to mock
System.setProperty("brobot.mock", "true");
}
After:
@Test
public void testModeSwitch() {
// Start with mock
MockModeManager.setMockMode(true);
// ... mock tests ...
// Switch to real
MockModeManager.setMockMode(false);
try {
// ... real tests ...
} finally {
// Always restore mock mode
MockModeManager.setMockMode(true);
}
}
Debugging Migration Issuesโ
Verify Mock Mode Stateโ
If you're experiencing issues after migration, use the debug logging:
@Test
public void debugMockState() {
// Log complete mock mode state
MockModeManager.logMockModeState();
// This shows:
// - System properties
// - ExecutionEnvironment state
// - BrobotProperties configuration (via Spring)
}
Common Issues and Solutionsโ
Issue 1: Mock Mode Not Enabledโ
Symptom: Tests fail with HeadlessException or try to capture real screens
Solution:
public class MyTest extends BrobotTestBase {
@BeforeEach
@Override
public void setupTest() {
super.setupTest(); // MUST call parent setup!
// Your setup here
}
}
Issue 2: Inconsistent Mock Behaviorโ
Symptom: Some components use mock mode, others don't
Solution:
// Debug the state
MockModeManager.logMockModeState();
// Use Spring-managed property (preferred for Spring components)
if (brobotProperties.getCore().isMock()) {
// mock logic
}
Issue 3: Legacy Code Interferenceโ
Symptom: Mock mode changes unexpectedly
Solution: Search for and replace all direct mock settings:
# Find direct brobotProperties.getCore().isMock() assignments
grep -r "brobotProperties.getCore().isMock()\s*=" .
# Find system property settings
grep -r "setProperty.*mock" .
# Replace with MockModeManager.setMockMode()
Benefits After Migrationโ
Cleaner Test Codeโ
Before: Multiple setup lines After: Single base class extension
Consistent Behaviorโ
All components synchronized automatically - no more partial mock states
Better Debuggingโ
Single logging method shows complete mock state
Easier Maintenanceโ
Changes to mock configuration only need updates in one place
Checklistโ
- All test classes extend
BrobotTestBase - Replaced all legacy mock checks (
ExecutionEnvironment.getInstance().isMockMode(),System.getProperty("brobot.mock")) withbrobotProperties.getCore().isMock() - Replaced all mock mode settings with
MockModeManager.setMockMode() - Removed redundant system property settings
- Verified tests pass in both local and CI environments
- Added
super.setupTest()call in overridden setup methods
Need Help?โ
If you encounter issues during migration:
- Use
MockModeManager.logMockModeState()to debug - Check that
BrobotTestBaseis properly extended - Ensure no legacy code is directly setting mock flags
- Refer to the documentation below
See Alsoโ
Core Mock Mode Documentationโ
- Mock Mode Guide - Comprehensive guide to using mock mode
- Mock Mode Manager - Centralized mock mode management
- Test Utilities - BrobotTestBase and testing utilities
Testing Guidesโ
- Testing Introduction - Overview of Brobot testing approaches
- Unit Testing - Unit testing patterns with BrobotTestBase
- Integration Testing - Integration test patterns with Spring
- Profile-Based Testing - Profile-specific mock configurations
- Testing Strategy - Overall testing strategy
Configuration Guidesโ
- BrobotProperties Usage - Configuring mock mode via properties
- Properties Reference - Complete property reference
- Headless Configuration - Headless mode (often used with mock)
Advanced Mock Featuresโ
- Mock Stochasticity - Probabilistic mock behavior
- Enhanced Mocking - Advanced mock scenarios
- CI/CD Testing - Mock mode in CI/CD pipelines
- ActionHistory Mock Snapshots - Creating mock data
- ActionHistory Integration Testing - Testing with action histories