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

Proposal: Integration of pact-jvm-mock into pact-jvm #1847

Open
ludorival opened this issue Jan 4, 2025 · 3 comments
Open

Proposal: Integration of pact-jvm-mock into pact-jvm #1847

ludorival opened this issue Jan 4, 2025 · 3 comments

Comments

@ludorival
Copy link

ludorival commented Jan 4, 2025

Overview

I've developed pact-jvm-mock, a library that automatically generates Pact contracts from existing mocks (Mockk, Mockito) in Kotlin/Java applications. The library aims to reduce the barrier to entry for contract testing by leveraging developers' existing mocking code.

Key Features

  1. Seamless Integration with Existing Mocks: Minimal code changes required - primarily replacing every with uponReceiving in existing mock definitions.
fun `should intercept simple stub`() {
    given {
        uponReceiving {
            restTemplate.getForEntity(any<String>(), eq(String::class.java))
        } returns ResponseEntity.ok("Hello World")
    } `when`{
        restTemplate.getForEntity(TEST_API_1_URL, String::class.java)
    } then {
        with(getCurrentPact(API_1)!!) {
            assertEquals(1, interactions.size)
        }
    }
}
  1. Multiple Mock Framework Support: Works with both Mockk (Kotlin) and Mockito (Java), with an extensible adapter system.

  2. Spring Integration: Built-in support for Spring RestTemplate with matching rules and provider states.

fun RestTemplate.willReturnUserProfile() = uponReceiving {
    getForEntity(match<String> { it.contains("user-service") }, UserProfile::class.java, *anyVararg())
} withDescription { 
   "get the user profile" 
} given { 
   state("The user has a preferred shopping list", mapOf("userId" to USER_ID)) 
} returns 
    ResponseEntity.ok(
        USER_PROFILE
    )
  1. Deterministic Mode: Optional strict mode to ensure consistent contract generation.
object DeterministicPact : PactConfiguration("shopping-list", SpringRestTemplateMockAdapter()) {
    override fun getPactDirectory(): String = "./src/test/resources/pacts-deterministic"
    override fun isDeterministic(): Boolean = true
    
    override fun customizeObjectMapper(providerName: String): ObjectMapper? = 
        NonDeterministicPact.customizeObjectMapper(providerName)
}

Benefits

  1. Lower Barrier to Entry: Developers can start with contract testing without learning new syntax, using their existing mock definitions.
  2. Reduced Duplication: No need to maintain separate mock and contract definitions.
  3. Gradual Adoption: Teams can incrementally adopt Pact by converting existing tests.

Current Status

  • Fully functional with comprehensive test coverage
  • MIT licensed
  • Published to Maven Central
  • Being used in production environments

Next Steps

I believe integrating this into the official pact-jvm project would provide significant value to the community. I'm happy to:

  1. Adapt the codebase to match pact-jvm conventions
  2. Add additional documentation
  3. Work on any suggested improvements

Would you be interested in discussing this integration? I'm happy to provide more details or schedule a call to discuss further.

Repository: https://github.com/ludorival/pact-jvm-mock

@rholshausen
Copy link
Contributor

Hi @ludorival, when you say "integrating this into the official pact-jvm project", what do you envision that to be? This becomes a module in the Pact-JVM repo, or the docs for it get incorporated into the pact.io website? Or do you mean integrating it with Pact-JVM code (I notice it has it's own code for generating Pact files)?

@ludorival
Copy link
Author

Hi @rholshausen , thanks for your reply.
Before considering making this part of the Pact-JVM modules, I would like to get your feedback on whether it aligns with your vision for Pact. Specifically, I’d like to know if you see value in integrating it as a module or keeping it as a standalone library. Either way, I believe it could complement Pact-JVM well, and I want to ensure it aligns with the broader goals of the project.

If you think there’s potential, I’d be happy to discuss how we could collaborate further or explore ways to integrate this effectively. Your insights would be greatly appreciated.

@rholshausen
Copy link
Contributor

Pact-JVM already has a lot of modules, I wouldn't want to add more unnecessarily. You are publishing to Maven Central already, so I think a better approach would be to update your repo to use the Pact-JVM modules (so you get all the V3 + V4 formats) and then integrate your docs with docs.pact.io.

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

No branches or pull requests

2 participants