Skip to main content
Version: Latest

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")) with brobotProperties.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:

  1. Use MockModeManager.logMockModeState() to debug
  2. Check that BrobotTestBase is properly extended
  3. Ensure no legacy code is directly setting mock flags
  4. Refer to the documentation below

See Alsoโ€‹

Core Mock Mode Documentationโ€‹

Testing Guidesโ€‹

Configuration Guidesโ€‹

Advanced Mock Featuresโ€‹