Skip to main content
Version: Latest

Mock Mode Manager

The MockModeManager class provides centralized control over mock mode configuration across the entire Brobot framework, ensuring consistency and eliminating confusion about which mock mode flag to use.

Overviewโ€‹

Prior to the introduction of MockModeManager, mock mode configuration was scattered across multiple components:

  • brobotProperties.getCore().isMock() (legacy SikuliX setting)
  • ExecutionEnvironment.mockMode (runtime environment setting)
  • Various system properties (brobot.mock.mode, brobot.mock, etc.)

This led to confusion and potential inconsistencies. The MockModeManager solves this by providing a single source of truth.

Key Featuresโ€‹

Single Source of Truthโ€‹

All mock mode checks and settings go through MockModeManager, ensuring consistency across the entire framework.

Automatic Synchronizationโ€‹

When you set mock mode through MockModeManager, it automatically updates:

  • All relevant system properties
  • ExecutionEnvironment configuration
  • FrameworkSettings (for SikuliX compatibility)
  • Any other framework-specific mock settings

Debug Capabilitiesโ€‹

Built-in logging to help debug mock mode state across all components.

Basic Usageโ€‹

Enabling Mock Modeโ€‹

import io.github.jspinak.brobot.config.mock.MockModeManager;

// Enable mock mode globally
MockModeManager.setMockMode(true);

Checking Mock Mode Statusโ€‹

// Check if mock mode is enabled
if (brobotProperties.getCore().isMock()) {
// Execute mock-specific logic
System.out.println("Running in mock mode");
} else {
// Execute real mode logic
System.out.println("Running in real mode");
}

Debugging Mock Mode Stateโ€‹

// Log the current mock mode state across all components
MockModeManager.logMockModeState();

This will output something like:

Mock Mode State:
System Properties:
brobot.mock = true
brobot.mock.action.success.probability = 1.0
ExecutionEnvironment:
mockMode = true
hasDisplay = false
canCaptureScreen = false
BrobotProperties: Configured via Spring (brobot.core.mock)

Integration with Testsโ€‹

Using with BrobotTestBaseโ€‹

All test classes that extend BrobotTestBase automatically use MockModeManager:

import io.github.jspinak.brobot.test.BrobotTestBase;
import org.junit.jupiter.api.Test;

public class MyTest extends BrobotTestBase {

@Test
public void testInMockMode() {
// Mock mode is automatically enabled via MockModeManager
assertTrue(isMockMode());

// Your test logic here
}

@Test
public void testInRealMode() {
// Temporarily disable mock mode
disableMockMode();

try {
assertFalse(isMockMode());
// Test with real screen capture
} finally {
// Re-enable mock mode for other tests
MockModeManager.setMockMode(true);
}
}
}

Manual Test Configurationโ€‹

For tests that don't extend BrobotTestBase:

import io.github.jspinak.brobot.config.mock.MockModeManager;
import io.github.jspinak.brobot.config.core.BrobotProperties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class StandaloneTest {

@Autowired
private BrobotProperties brobotProperties;

@BeforeEach
public void setup() {
// Manually enable mock mode
MockModeManager.setMockMode(true);
}

@Test
public void testFeature() {
assertTrue(brobotProperties.getCore().isMock());
// Test logic
}
}

Application Startupโ€‹

Initializing Mock Mode from Propertiesโ€‹

During application startup, you can initialize mock mode based on system properties:

import io.github.jspinak.brobot.config.mock.MockModeManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BrobotApplication {

public static void main(String[] args) {
// Initialize mock mode based on system properties
MockModeManager.initializeMockMode();

SpringApplication.run(BrobotApplication.class, args);
}
}

Spring Configurationโ€‹

Configure mock mode through Spring properties:

# application.yml
brobot:
core:
mock: true # Enable mock mode

Or via properties file:

# application.properties
brobot.core.mock=true

Or via command line:

java -jar myapp.jar --brobot.core.mock=true

Implementation Detailsโ€‹

Synchronized Propertiesโ€‹

MockModeManager.setMockMode(true) sets the following:

  1. System Property:

    • brobot.mock = "true"
  2. ExecutionEnvironment:

    • mockMode = true
    • forceHeadless = true (when enabling mock mode)
    • allowScreenCapture = false (when enabling mock mode)
  3. BrobotProperties:

    • Configured separately via Spring Boot's brobot.core.mock property
    • Not directly updated by MockModeManager (Spring managed)

Priority Orderโ€‹

When checking mock mode status, MockModeManager.isMockMode() checks in this order:

  1. Primary: ExecutionEnvironment.getInstance().isMockMode() (if available)
  2. Fallback: System property brobot.mock (if ExecutionEnvironment not available)

For Spring-managed components, use brobotProperties.getCore().isMock() which reads from the Spring configuration property brobot.core.mock.

Best Practicesโ€‹

1. Always Use MockModeManagerโ€‹

Instead of checking individual mock flags:

// โŒ Don't do this
if (brobotProperties.getCore().isMock() || ExecutionEnvironment.getInstance().isMockMode()) {
// ...
}

// โœ… Do this
if (brobotProperties.getCore().isMock()) {
// ...
}

2. Set Mock Mode Onceโ€‹

Set mock mode at the beginning of your test or application:

@BeforeAll
public static void setupClass() {
MockModeManager.setMockMode(true);
}

3. Use Logging for Debuggingโ€‹

When troubleshooting mock mode issues:

// Before your test
MockModeManager.logMockModeState();

// Run your test
// ...

// After if needed
MockModeManager.logMockModeState();

4. Handle Mode Transitions Carefullyโ€‹

If switching between modes in a test:

@Test
public void testModeTransition() {
// Start in mock mode
MockModeManager.setMockMode(true);
// ... mock tests ...

// Switch to real mode
MockModeManager.setMockMode(false);
try {
// ... real mode tests ...
} finally {
// Always restore mock mode for other tests
MockModeManager.setMockMode(true);
}
}

Migration Guideโ€‹

From Direct Property Settingโ€‹

Before:

System.setProperty("brobot.mock", "true");

ExecutionEnvironment env = ExecutionEnvironment.builder()
.mockMode(true)
.build();
ExecutionEnvironment.setInstance(env);

After:

import io.github.jspinak.brobot.config.mock.MockModeManager;

MockModeManager.setMockMode(true);

From Multiple Mock Checksโ€‹

Before:

boolean isMock = brobotProperties.getCore().isMock() || 
"true".equals(System.getProperty("brobot.mock.mode")) ||
ExecutionEnvironment.getInstance().isMockMode();

After:

boolean isMock = brobotProperties.getCore().isMock();

Troubleshootingโ€‹

Mock Mode Not Taking Effectโ€‹

  1. Check that MockModeManager is being used:

    MockModeManager.logMockModeState();
  2. Ensure you're calling setMockMode() early enough in your test/application lifecycle

  3. Verify no other code is directly setting mock flags after MockModeManager

Inconsistent Mock Behaviorโ€‹

If you see inconsistent behavior between components:

  1. Use MockModeManager.setMockMode() instead of setting individual flags
  2. Check for any legacy code still using direct property access
  3. Enable debug logging to trace mock mode changes

API Referenceโ€‹

MockModeManager Methodsโ€‹

MethodDescription
setMockMode(boolean enable)Enable or disable mock mode globally
isMockMode()Check if mock mode is currently enabled
logMockModeState()Log current mock mode state for debugging
initializeMockMode()Initialize mock mode based on system properties

Core Mock Mode Documentationโ€‹

Testing Guidesโ€‹

Configuration Guidesโ€‹

Advanced Mock Featuresโ€‹