Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the PactMock DSL #241

Closed
ludorival opened this issue Jan 3, 2025 · 0 comments · Fixed by #246
Closed

Change the PactMock DSL #241

ludorival opened this issue Jan 3, 2025 · 0 comments · Fixed by #246
Assignees

Comments

@ludorival
Copy link
Owner

ludorival commented Jan 3, 2025

Enhancement: Introduce More Fluent DSL for PactMock Contract Generation

Currently, developers need to use a combination of every and willRespond/willRespondWith to generate Pact contracts from Mockk mocks. While functional, we can make this more intuitive and fluent by introducing a new DSL.

Current Syntax

every {
    restTemplate.getForEntity(match<String> { it.contains("user-service") }, UserProfile::class.java)
} willRespondWith {
    description("get the user profile")
    providerState("The user has a preferred shopping list")
    ResponseEntity.ok(USER_PROFILE)
}

Proposed Syntax

uponReceiving { 
    restTemplate.getForEntity(any(), any(), any()) 
} withDescription "get a user profile" given state("User should exist") returns ResponseEntity.ok(USER_PROFILE)

Benefits

  • More declarative and readable syntax
  • Closer to natural language and Pact's terminology
  • Reduced boilerplate
  • Better alignment with Pact's concepts (upon receiving, given state, etc.)
  • Easier to understand for developers familiar with Pact

Implementation Notes

  • Introduce new extension functions: uponReceiving
  • Maintain all existing functionality (matching rules, provider states, etc.)
@ludorival ludorival self-assigned this Jan 3, 2025
ludorival added a commit that referenced this issue Jan 4, 2025
- Fixes #241

- Added `clearPact` and `getCurrentPact` methods to `PactMock` for better pact management.
- Introduced `state` and `anError` utility functions for improved interaction handling.
- Refactored interaction definitions in test contracts to use `uponReceiving` and `withDescription` for clarity and consistency.
- Updated `willRespondWith` and related methods to streamline response handling in mock setups.
- Improved README documentation to reflect changes in usage patterns with Mockk and Mockito.

These enhancements aim to simplify the mocking process and improve the overall usability of the pact-jvm-mock library.
ludorival added a commit that referenced this issue Jan 4, 2025
* Change the PactMock DSL
- Fixes #241

- Added `clearPact` and `getCurrentPact` methods to `PactMock` for better pact management.
- Introduced `state` and `anError` utility functions for improved interaction handling.
- Refactored interaction definitions in test contracts to use `uponReceiving` and `withDescription` for clarity and consistency.
- Updated `willRespondWith` and related methods to streamline response handling in mock setups.
- Improved README documentation to reflect changes in usage patterns with Mockk and Mockito.

These enhancements aim to simplify the mocking process and improve the overall usability of the pact-jvm-mock library.

* Refactor pact-jvm-mock to use PactConfiguration instead of PactOptions

- Replaced `PactOptions` with `PactConfiguration` throughout the codebase for improved clarity and consistency in configuration management.
- Updated README documentation to reflect the new configuration approach and provide clearer examples.
- Modified the `PactConsumer` annotation to accept `PactConfiguration` types.
- Removed the obsolete `PactOptions` class and related methods, streamlining the pact management process.
- Enhanced test classes to utilize the new configuration structure, ensuring compatibility with existing tests.

These changes aim to simplify the configuration process and improve the overall usability of the pact-jvm-mock library.
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 a pull request may close this issue.

1 participant