Skip to content

Conversation

gnodet
Copy link
Contributor

@gnodet gnodet commented Sep 10, 2025

Summary

This PR brings SessionMock in line with the real Session implementation: getService(Class) now throws NoSuchElementException for unknown services by default.

It also simplifies the mock by centralizing the list of configured services and strengthens test coverage.

Motivation

  • Previously, the mock returned null for unknown services (Mockito default), diverging from production behavior and occasionally masking test errors.
  • The earlier “exclusion list” logic was verbose and fragile; adding a new configured service required editing a long chained predicate.

What changed

  • Default behavior
    • Any service not explicitly configured in the mock now results in NoSuchElementException, matching AbstractSession semantics.
  • Implementation simplification
    • Introduced a centralized CONFIGURED_SERVICES set and a contains check, replacing the chained predicate.
    • Used Mockito argThat to apply the default-throw behavior only for services not in CONFIGURED_SERVICES.
    • Resolved a minor circularity where setup could inadvertently query the mock during configuration.
  • API & documentation
    • Deprecated getMockSessionWithEnhancedServiceBehavior(...); getMockSession(...) now has the enhanced behavior by default.
    • Updated Javadoc to document the default-throw behavior and the centralized configuration.
  • Tests
    • Added tests to assert the default-throw behavior for unknown services and validate that configured services are wired correctly.

Compatibility

  • Backward-compatible for typical usage:
    • Code/tests that request configured services continue to work.
    • Tests that relied on null for unknown services should now either configure/stub those services or expect NoSuchElementException.
  • No production code changes; scope limited to impl/maven-testing and its tests.

Files touched

  • impl/maven-testing/src/main/java/org/apache/maven/api/plugin/testing/stubs/SessionMock.java
  • impl/maven-testing/src/test/java/org/apache/maven/api/plugin/testing/stubs/SessionGetServiceTest.java
  • impl/maven-testing/src/test/java/org/apache/maven/api/plugin/testing/stubs/SessionMockDefaultBehaviorTest.java

Verification

  • All existing tests pass locally; new tests cover the default-throw behavior and configured-service access.

@gnodet gnodet changed the title Add enhanced service behavior to SessionMock for better testing Improve SessionMock to throw NoSuchElementException by default for unknown services Sep 11, 2025
@gnodet gnodet changed the title Improve SessionMock to throw NoSuchElementException by default for unknown services Improve SessionMock.getService default behavior with cleaner implementation Sep 11, 2025
@gnodet gnodet force-pushed the feature/enhanced-session-mock-service-behavior branch from 6c9177d to 0110f1e Compare September 17, 2025 09:09
@gnodet gnodet changed the title Improve SessionMock.getService default behavior with cleaner implementation Align SessionMock.getService with real behavior Sep 17, 2025
@gnodet gnodet force-pushed the feature/enhanced-session-mock-service-behavior branch from 0110f1e to 7090119 Compare September 17, 2025 10:00
@gnodet gnodet closed this Sep 17, 2025
@gnodet gnodet force-pushed the feature/enhanced-session-mock-service-behavior branch from 7090119 to dbff760 Compare September 17, 2025 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants